📖 Scenario: You are building a simple stack data structure using a linked list. A stack works like a stack of plates: you add plates on top and remove plates from the top only.This stack will help manage tasks in a to-do list app where the most recent task is handled first.
🎯 Goal: Create a stack using a linked list with push and pop operations, then display the stack content.
📋 What You'll Learn
Create a Node class with data and next attributes
Create a Stack class with top attribute initialized to None
Implement push method to add elements to the stack
Implement pop method to remove elements from the stack
Print the stack elements from top to bottom
💡 Why This Matters
🌍 Real World
Stacks are used in undo features, expression evaluation, and backtracking algorithms.
💼 Career
Understanding stack implementation helps in coding interviews and building efficient algorithms.
Progress0 / 4 steps
1
Create Node and Stack classes
Create a class called Node with an __init__ method that takes data and sets self.data = data and self.next = None. Then create a class called Stack with an __init__ method that sets self.top = None.
DSA Python
Hint
Think of Node as a box holding data and a link to the next box. Stack keeps track of the top box.
2
Add push method to Stack
Inside the Stack class, create a method called push that takes self and data. Create a new Node with data. Set the new node's next to self.top. Then update self.top to the new node.
DSA Python
Hint
Push means adding a new node on top. Link the new node to the old top, then update top.
3
Add pop method to Stack
Inside the Stack class, create a method called pop that takes self. If self.top is None, return None. Otherwise, save self.top.data in a variable, update self.top to self.top.next, and return the saved data.
DSA Python
Hint
Pop means removing the top node and returning its data. If stack is empty, return None.
4
Print stack elements from top to bottom
Create a Stack object called stack. Use stack.push to add 10, 20, and 30 in that order. Then create a variable current and set it to stack.top. Use a while loop to print current.data followed by ' -> ' until current is None. After the loop, print None.
DSA Python
Hint
Remember, the last pushed item is on top. Print from top to bottom with arrows.
Practice
(1/5)
1. What is the main advantage of implementing a stack using a linked list instead of an array?
easy
A. The stack can grow dynamically without a fixed size limit
B. It uses less memory than an array
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 a fixed size, so stacks implemented with arrays have a maximum capacity.
Step 2: Linked list dynamic size advantage
Linked lists allocate memory dynamically, so the stack can grow as needed without size limits.
Final Answer:
The stack can grow dynamically without a fixed size limit -> Option A
Quick Check:
Linked list stacks grow dynamically [OK]
Hint: Linked list stacks grow dynamically, arrays do not [OK]
Common Mistakes:
Thinking linked lists use less memory always
Confusing stack with queue behavior
Assuming random access is possible in stacks
2. Which of the following is the correct way to create a new node class for a linked list stack in Python?
easy
A. class Node:\n def __init__(self):\n self.data = None\n self.next = None
B. class Node:\n def __init__(self, data, next):\n self.data = data\n self.next = next
C. class Node:\n def __init__(self, data):\n self.data = data\n self.next = None
D. class Node:\n def __init__(self, data):\n self.next = data\n self.data = None
Solution
Step 1: Check Node class constructor parameters
The node should store data and a pointer to next node, initialized to None.
Step 2: Verify correct attribute assignment
class Node:\n def __init__(self, data):\n self.data = data\n self.next = None correctly assigns data and sets next to None for a new node.
Final Answer:
class Node:\n def __init__(self, data):\n self.data = data\n self.next = None -> Option C
Quick Check:
Node stores data and next pointer [OK]
Hint: Node needs data and next pointer initialized to None [OK]
Common Mistakes:
Swapping data and next assignments
Forgetting to initialize next to None
Adding unnecessary parameters in constructor
3. What will be the output of the following code snippet implementing push and print operations on a linked list stack?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.top = None
def push(self, data):
new_node = Node(data)
new_node.next = self.top
self.top = new_node
def print_stack(self):
current = self.top
while current:
print(current.data, end=' -> ')
current = current.next
print('null')
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.print_stack()
medium
A. 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. null -> 10 -> 20 -> 30
D. 30 -> null -> 20 -> 10
Solution
Step 1: Trace push operations
Push 10: top points to node with 10. Push 20: new node with 20 points to 10, top updated to 20. Push 30: new node with 30 points to 20, top updated to 30.
Step 2: Trace print_stack output
Print starts at top (30), then 20, then 10, then null.
Final Answer:
30 -> 20 -> 10 -> null -> Option A
Quick Check:
Stack top is last pushed [OK]
Hint: Stack prints from top to bottom, last pushed first [OK]
Common Mistakes:
Printing in insertion order instead of stack order
Confusing next pointer direction
Forgetting to print null at end
4. Identify the error in the following pop method of a linked list stack and select the correct fix:
def pop(self):
if self.top is None:
return None
popped_node = self.top
self.top = popped_node.next
return popped_node
medium
A. Raise exception instead of returning null when stack is empty
B. Check if popped_node.next is null before assigning
C. Set popped_node.next to null before returning
D. Return popped_node.data instead of popped_node
Solution
Step 1: Understand pop method return value
Pop should return the data of the popped node, not the node object itself.
Step 2: Fix return statement
Change return popped_node to return popped_node.data to return the value.
Final Answer:
Return popped_node.data instead of popped_node -> Option D
Quick Check:
Pop returns data, not node object [OK]
Hint: Pop returns data, not node object [OK]
Common Mistakes:
Returning node object instead of data
Not updating top pointer
Not handling empty stack case
5. You want to implement a function reverse_stack that reverses the elements of a stack implemented using a linked list without using extra data structures. Which approach correctly achieves this?
hard
A. Traverse the linked list and swap data of nodes pairwise from start to end
B. Recursively pop all elements, then push them back in reverse order
C. Use a temporary array to store elements, then push back in reverse
D. Create a new stack and push elements from the original stack to it
Solution
Step 1: Understand constraints - no extra data structures
Options C and D use extra structures (array or new stack), so they are invalid.
Step 2: Recursive approach to reverse stack
Recursively pop elements until empty, then push them back in reverse order using call stack, reversing the stack without extra storage.
Final Answer:
Recursively pop all elements, then push them back in reverse order -> Option B
Quick Check:
Recursion reverses stack without extra data structures [OK]
Hint: Use recursion to reverse stack without extra storage [OK]
Common Mistakes:
Using extra arrays or stacks despite constraints
Trying to swap node data incorrectly
Not handling base case in recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.