📖 Scenario: Imagine you are building a simple system to manage browser history using a stack. Each page visited is added to the stack, and when the user clicks back, the last visited page is removed.
🎯 Goal: You will create a stack using a linked list in C++. You will add nodes, manage the top pointer, and print the stack content.
📋 What You'll Learn
Create a Node struct with an integer data and a pointer to the next Node
Create a Stack class with a private Node pointer called top
Implement a push function to add elements to the stack
Implement a pop function to remove elements from the stack
Implement a printStack function to display the stack from top to bottom
💡 Why This Matters
🌍 Real World
Stacks are used in many places like undo features, browser history, and expression evaluation.
💼 Career
Understanding stack implementation helps in interviews and building efficient software that manages data in last-in-first-out order.
Progress0 / 4 steps
1
Create Node struct and Stack class with top pointer
Create a struct Node with an int data and a Node* next. Then create a class Stack with a private Node* top pointer initialized to nullptr.
DSA C++
Hint
Think of Node as a box holding a number and a link to the next box. The Stack keeps track of the top box.
2
Add push function to Stack class
Inside the Stack class, add a public void push(int value) function. It should create a new Node, set its data to value, link its next to the current top, and update top to this new node.
DSA C++
Hint
Push means adding a new box on top. Make sure the new box points to the old top, then update top.
3
Add pop function to Stack class
Inside the Stack class, add a public int pop() function. It should check if top is nullptr and return -1 if empty. Otherwise, save top->data, move top to top->next, delete the old top node, and return the saved data.
DSA C++
Hint
Pop means removing the top box and returning its number. Remember to free memory of the removed node.
4
Add printStack function and test the stack
Add a public void printStack() function in the Stack class. It should print all data values from top to the last node separated by -> and end with nullptr. Then in main(), create a Stack object, push 10, 20, 30, pop once, and print the stack.
DSA C++
Hint
Print each node's data followed by '->' until you reach the end, then print 'nullptr'.
In main, after popping, print the stack to see the current elements.
Practice
(1/5)
1. What is the main advantage of implementing a stack using a linked list instead of an array?
easy
A. It uses less memory than an array
B. The stack can grow dynamically without a fixed size limit
C. It allows random access to elements
D. It is easier to sort the stack elements
Solution
Step 1: Understand stack size limitation in arrays
Arrays have fixed size, so stack implemented with arrays has a maximum capacity.
Step 2: Linked list dynamic size property
Linked lists can grow or shrink dynamically by adding or removing nodes, so stack size is not fixed.
Final Answer:
The stack can grow dynamically without a fixed size limit -> Option B
Quick Check:
Linked list stack size = dynamic growth [OK]
Hint: Linked list stacks grow dynamically, arrays have fixed size [OK]
Common Mistakes:
Thinking linked list uses less memory always
Assuming stack allows random access
Confusing sorting with stack operations
2. Which of the following is the correct syntax to create a new node in a linked list stack in C++?
easy
A. Node* newNode = new Node(value);
B. Node newNode = Node(value);
C. Node newNode = new Node(value);
D. Node* newNode = Node(value);
Solution
Step 1: Understand dynamic memory allocation in C++
To create a node dynamically, use 'new' keyword and pointer type.
Step 2: Check syntax correctness
Node* newNode = new Node(value); uses pointer and 'new' correctly: Node* newNode = new Node(value);
Final Answer:
Node* newNode = new Node(value); -> Option A
Quick Check:
Dynamic node creation = new + pointer [OK]
Hint: Use 'new' with pointer to create nodes dynamically [OK]
Common Mistakes:
Omitting pointer (*) when using new
Using stack allocation instead of dynamic
Assigning new object to non-pointer variable
3. What will be the output after executing the following code snippet for a stack implemented using linked list?