📖 Scenario: Imagine you are building a simple undo feature for a text editor. Each action the user performs is saved on a stack. You will create a stack using a linked list to store these actions.
🎯 Goal: Build a stack data structure using a linked list in C#. You will create nodes, push actions onto the stack, pop actions off, and finally display the current stack.
📋 What You'll Learn
Create a Node class with a string data field and a Node next field
Create a Stack class with a Node top field
Implement Push method to add a node to the top
Implement Pop method to remove the top node
Print the stack 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 software development roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create Node class and Stack class with top
Create a class called Node with a public string field data and a public Node field next. Then create a class called Stack with a public Node field called top initialized to null.
DSA C#
Hint
Think of Node as a box holding the action and a pointer to the next box. The Stack keeps track of the top box.
2
Add Push method to Stack
Inside the Stack class, add a public method called Push that takes a string parameter action. This method should create a new Node with action as data, set its next to the current top, and then update top to this new node.
DSA C#
Hint
Push adds a new action on top by linking the new node to the current top and then updating top.
3
Add Pop method to Stack
Inside the Stack class, add a public method called Pop that returns a string. This method should check if top is null and return null if so. Otherwise, it should save the data from top, update top to top.next, and return the saved data.
DSA C#
Hint
Pop removes the top action and returns its data. If the stack is empty, return null.
4
Print the stack from top to bottom
Create a PrintStack method inside the Stack class that prints each node's data from top to the bottom, separated by -> , ending with null. Then, in Main, create a Stack object, push these actions in order: "Type A", "Type B", "Type C", pop one action, and finally call PrintStack.
DSA C#
Hint
PrintStack walks through the linked list from top to bottom, printing each action followed by an arrow, ending with null.
In Main, push three actions, pop one, then print the stack.
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 automatically sorts the 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
Linked lists can grow or shrink dynamically by adding or removing nodes, so the stack size is limited only by memory.
Final Answer:
The stack can grow dynamically without a fixed size limit -> Option B
Quick Check:
Linked list stacks grow dynamically [OK]
Hint: Linked list stacks grow dynamically, arrays do not [OK]
Common Mistakes:
Thinking linked list uses less memory always
Assuming linked list allows random access
Confusing stack sorting with implementation
2. Which of the following is the correct way to push an element onto a stack implemented using a linked list in C#?
easy
A. Create a new node, set its next to current top, then update top to new node
B. Create a new node, set its previous to current top, then update top to new node
C. Add the new element at the end of the linked list
D. Replace the top node's value with the new element
Solution
Step 1: Understand push operation in linked list stack
Push adds a new node at the beginning (top) of the linked list.
Step 2: Correct pointer update
The new node's next points to the current top, then top is updated to new node.
Final Answer:
Create a new node, set its next to current top, then update top to new node -> Option A
Quick Check:
Push adds node at front [OK]
Hint: Push adds node at front, update top pointer [OK]
Common Mistakes:
Using previous pointer instead of next
Adding node at end instead of front
Overwriting top node value instead of adding new node
3. What will be the output after the following C# code executes?
StackNode top = null;
// Push 10
StackNode node1 = new StackNode(10);
node1.next = top;
top = node1;
// Push 20
StackNode node2 = new StackNode(20);
node2.next = top;
top = node2;
// Pop
int popped = top.data;
top = top.next;
// Print top data
Console.WriteLine(top.data);
medium
A. NullReferenceException
B. 20
C. 10
D. 0
Solution
Step 1: Trace push operations
Push 10: top points to node with data 10. Push 20: top points to node with data 20, next points to node 10.
Step 2: Trace pop operation
Pop removes top node (20), top updated to next node (10).
Step 3: Print top data
Top now points to node with data 10, so output is 10.
Final Answer:
10 -> Option C
Quick Check:
After pop, top data = 10 [OK]
Hint: Pop removes top, next node becomes new top [OK]
Common Mistakes:
Printing popped value instead of new top
Confusing push order
Not updating top after pop
4. Identify the error in this pop method for a stack implemented using a linked list:
public int Pop() {
if (top == null) {
Console.WriteLine("Stack Underflow");
}
int value = top.data;
top = top.next;
return value;
}
medium
A. It does not return a value when stack is empty
B. It does not update the top pointer
C. It incorrectly prints "Stack Overflow" instead of "Underflow"
D. It removes the bottom node instead of the top
Solution
Step 1: Check behavior when stack is empty
If top is null, method prints message but continues to access top.data causing error or returns no value.
Step 2: Missing return for empty stack case
Method should return or throw exception when stack is empty to avoid runtime error.
Final Answer:
It does not return a value when stack is empty -> Option A
Quick Check:
Empty stack must return or throw [OK]
Hint: Always return or throw on empty stack pop [OK]
Common Mistakes:
Ignoring empty stack case
Confusing overflow with underflow
Not updating top pointer
5. You want to implement a method ReverseStack that reverses the elements of a stack implemented using a linked list without using extra data structures. Which approach is correct?
hard
A. Use a queue to dequeue and enqueue elements to reverse order
B. Traverse the linked list and swap data values of nodes pairwise
C. Create a new linked list and push elements from old stack to new stack
D. Use recursion to pop all elements, then push them back in reverse order
Solution
Step 1: Understand constraints
No extra data structures allowed, so using recursion (call stack) is allowed but no explicit new lists or queues.
Step 2: Recursion approach
Recursively pop all elements, then insert them back at bottom to reverse order without extra storage.
Step 3: Other options use extra structures
Swapping data or using new lists or queues violates constraints or is incorrect for stack reversal.
Final Answer:
Use recursion to pop all elements, then push them back in reverse order -> Option D
Quick Check:
Recursion reverses stack without extra structures [OK]
Hint: Use recursion to reverse stack without extra data structures [OK]
Common Mistakes:
Using extra linked lists or queues
Trying to swap node data incorrectly
Ignoring no extra structure constraint
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.