17. DSA with C++ - Stack using Linked List

This is an implementaon of a stack using a linked list in C++, including all the common operaons:


#include <iostream>

// Node structure for the stack
struct Node {
    int data;
    Node* next;
};

// Stack class
class Stack {
private:
    Node* top; // Pointer to the top node of the stack

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

    // Destructor
    ~Stack() {
        // Free memory by deleting all nodes in the stack
        while (!isEmpty()) {
            pop();
        }
    }

    // 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;
        std::cout << "Pushed element: " << value << std::endl;
    }

    // Function to pop the top element from the stack
    void pop() {
        if (isEmpty()) {
            std::cout << "Stack is empty. Cannot pop an element." << std::endl;
            return;
        }
        Node* temp = top;
        int poppedValue = temp->data;
        top = top->next;
        delete temp;
        std::cout << "Popped element: " << poppedValue << std::endl;
    }

    // Function to get the top element of the stack without removing it
    int peek() {
        if (isEmpty()) {
            std::cout << "Stack is empty. No element to peek." << std::endl;
            return -1;
        }
        return top->data;
    }

    // Function to display the elements of the stack
    void display() {
        if (isEmpty()) {
            std::cout << "Stack is empty. No elements to display." << std::endl;
            return;
        }
        std::cout << "Stack elements: ";
        Node* temp = top;
        while (temp != nullptr) {
            std::cout << temp->data << " ";
            temp = temp->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    Stack stack;

    stack.push(1);
    stack.push(2);
    stack.push(3);

    stack.display(); // Output: Stack elements: 3 2 1

    std::cout << "Top element: " << stack.peek() << std::endl; // Output: Top element: 3

    stack.pop();
    stack.display(); // Output: Stack elements: 2 1

    stack.push(4);
    stack.display(); // Output: Stack elements: 4 2 1

    return 0;
}
  
Explation:
  • The Stack class is implemented with member funcons to perform various operaons on the stack.
  • The isEmpty funcon checks if the stack is empty by checking if the top pointer is nullptr.
  • The push funcon inserts a new element onto the stack by creang a new node and updang the top pointer.
  • The pop funcon removes the top element from the stack by deleng the corresponding node and updang the top pointer.
  • The peek funcon returns the value of the top element without removing it.
  • The display funcon traverses the stack and prints the elements from top to boom.
  • In the main funcon, we create a stack object and perform various stack operaons such as pushing elements, popping elements, peeking the top element, and displaying the stack.
Output:

  Pushed element: 1 
  Pushed element: 2 
  Pushed element: 3 
  Stack elements: 3 2 1 
  Top element: 3 
  Popped element: 3 
  Stack elements: 2 1 
  Pushed element: 4 
  Stack elements: 4 2 1