Let's Code C++ class for Stack using Linked List

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 233:- Let's Code C++ class for Stack using Linked List

Here's the C++ implementation of a stack using a singly linked list:

cppCopy code

#include <iostream> class Node { public:    int data;    Node* next;    Node(int data) : data(data), next(nullptr) {} }; class Stack { private:    Node* top; public:    Stack() : top(nullptr) {}    ~Stack() {        while (!isEmpty()) {            pop();        }    }    bool isEmpty() {        return top == nullptr;    }    void push(int value) {        Node* newNode = new Node(value);        newNode->next = top;        top = newNode;    }    void pop() {        if (isEmpty()) {            std::cout << "Stack Underflow! Cannot pop element." << std::endl;            return;        }        Node* temp = top;        top = top->next;        delete temp;    }    int peek() {        if (isEmpty()) {            std::cout << "Stack is empty. Cannot peek." << std::endl;            return -1;        }        return top->data;    } }; int main() {    Stack stack;    stack.push(1);    stack.push(2);    stack.push(3);    std::cout << "Top element: " << stack.peek() << std::endl; // Output: 3    stack.pop();    stack.push(4);    while (!stack.isEmpty()) {        std::cout << "Popped element: " << stack.peek() << std::endl;        stack.pop();    }    return 0; }

In this implementation, the Node class is used to represent each node in the singly linked list. The Stack class uses the top pointer to keep track of the top element of the stack.

The push method adds an element to the top of the stack by creating a new node and updating the top pointer. The pop method removes the top element from the stack, and the peek method returns the value of the top element without removing it.

In the main function, we demonstrate how to use the Stack class by creating a stack. We push elements onto the stack, peek at the top element, and then pop elements until the stack becomes empty. The output will show the elements being pushed and popped, following the Last In First Out (LIFO) principle.

12. Stack

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support

Sciaku (सियाकु)

Sciaku (सियाकु) provides you a technical and programming content like Java Programming, Python Programming, C Programming,Android Development, Web Development, etc. Learn how to make software, website, and applications here and also we have industrial internship for you.

Contact

G20, Gopal Vihar Colony, Noida Sector 2, Uttar Pradesh, India, 201301

info@sciaku.com

Copyright © 2022-2025 Created by ❤️ Sciaku

Privacy Policy | Terms & Conditions | Refunds Policy