stack implementation through linked list

Sure! Here is an implementation of a stack using a linked list in C++:

#include <iostream>

using namespace std;

// Node class to represent individual elements of the stack
class Node {
public:
    int data;
    Node* next;
};

// Stack class to implement the stack operations
class Stack {
private:
    Node* top;

public:
    // Constructor
    Stack() {
        top = nullptr;
    }

    // Function to check if the stack is empty
    bool isEmpty() {
        return top == nullptr;
    }

    // Function to push an element onto the stack
    void push(int value) {
        Node* newNode = new Node();
        newNode->data = value;
        newNode->next = top;
        top = newNode;
    }

    // Function to pop an element from the stack
    int pop() {
        if (isEmpty()) {
            cout << "Stack is empty!" << endl;
            return -1;
        }

        int poppedValue = top->data;
        Node* temp = top;
        top = top->next;
        delete temp;
        return poppedValue;
    }

    // Function to get the top element of the stack
    int peek() {
        if (isEmpty()) {
            cout << "Stack is empty!" << endl;
            return -1;
        }

        return top->data;
    }
};

int main() {
    Stack stack;

    stack.push(10);
    stack.push(20);
    stack.push(30);

    cout << "Top element: " << stack.peek() << endl;

    cout << "Popped element: " << stack.pop() << endl;
    cout << "Popped element: " << stack.pop() << endl;
    cout << "Popped element: " << stack.pop() << endl;

    cout << "Is stack empty? " << (stack.isEmpty() ? "Yes" : "No") << endl;

    return 0;
}

Explanation:

  • The code begins by including the necessary headers and using the std namespace.

  • The Node class is defined to represent individual elements of the stack. It contains two members: data, which stores the value of the element, and next, which is a pointer to the next element in the stack.

  • The Stack class is defined to implement the stack operations. It has a private member top, which is a pointer to the top element of the stack.

  • The constructor of the Stack class initializes the top pointer to nullptr, indicating an empty stack.

  • The isEmpty() function checks if the stack is empty by comparing the top pointer to nullptr. If they are equal, it returns true; otherwise, it returns false.

  • The push() function is used to push an element onto the stack. It creates a new Node object, assigns the given value to its data member, and sets its next pointer to the current top element. Then, it updates the top pointer to point to the new node.

  • The pop() function is used to pop an element from the stack. It first checks if the stack is empty. If it is, it prints an error message and returns -1. Otherwise, it retrieves the value of the top element, updates the top pointer to point to the next element, deletes the previous top node, and returns the retrieved value.

  • The peek() function returns the value of the top element of the stack without removing it. It performs a similar check for an empty stack and returns -1 if it is empty. Otherwise, it returns the value of the top element.

  • The main() function demonstrates the usage of the Stack class. It creates a Stack object, pushes three elements onto the stack, and then performs a series of pop() and peek() operations to retrieve and display the elements. Finally, it checks if the stack is empty and prints the result.

  • The output of the program will be:

Top element: 30
Popped element: 30
Popped element: 20
Popped element: 10
Is stack empty? Yes

This indicates that the stack is initially filled with elements 10, 20, and 30. The elements are then popped in the reverse order, and at the end, the stack is empty.