📖 Scenario: Imagine you are building a simple undo feature for a text editor. You want to store the history of actions so you can go back step-by-step. You will create two types of stacks: one using an array and one using a linked list. This will help you understand the differences and trade-offs between these two ways to build a stack.
🎯 Goal: Build two stacks: one using an array and one using a linked list. Push some actions onto both stacks, then pop one action from each. Finally, print the current state of both stacks to see how they work differently.
📋 What You'll Learn
Create an array-based stack with fixed size 5
Create a linked list-based stack
Push exactly these actions in order: "Type A", "Type B", "Type C"
Pop one action from each stack
Print the remaining stack contents in order from top to bottom
💡 Why This Matters
🌍 Real World
Stacks are used in undo features, expression evaluation, and backtracking algorithms. Understanding different implementations helps choose the right one for your app.
💼 Career
Knowing how to implement and compare data structures like stacks is essential for software development roles, especially when optimizing for memory or speed.
Progress0 / 4 steps
1
Create Array Stack and Linked List Stack Classes
Create a class called ArrayStack with a fixed size array of strings called items of size 5 and an integer top initialized to -1. Also create a class called LinkedListStack with a private nested class Node that has a string data and a Node reference next. The LinkedListStack should have a Node reference called head initialized to null.
DSA C#
Hint
Define the two classes with the exact field names and types as described.
2
Add Push and Pop Methods to Both Stacks
Add a Push(string item) method to ArrayStack that adds item to items if there is space, increasing top. Add a Pop() method that returns the top item and decreases top. For LinkedListStack, add a Push(string item) method that creates a new Node and sets it as the new head. Add a Pop() method that removes and returns the head data, updating head.
DSA C#
Hint
Implement push and pop methods exactly as described, using the field names given.
3
Push Actions onto Both Stacks and Pop One Action
Create a Main method. Inside it, create an ArrayStack instance called arrayStack and a LinkedListStack instance called linkedListStack. Push these exact strings in order onto both stacks: "Type A", "Type B", "Type C". Then pop one item from each stack and store them in variables poppedFromArray and poppedFromList.
DSA C#
Hint
Follow the exact variable names and method calls to push and pop the actions.
4
Print Remaining Stack Contents from Top to Bottom
Add a method PrintStack() to ArrayStack that prints items from top down to 0, each on a new line. Add a method PrintStack() to LinkedListStack that prints each node's data from head down to the last node, each on a new line. In Main, after popping, call PrintStack() on both stacks to print their current contents.
DSA C#
Hint
Print the stacks from top to bottom after popping one item each. The output should show the remaining items in order.
Practice
(1/5)
1. Which of the following is a key advantage of using a linked list to implement a stack compared to using an array?
easy
A. Accessing elements by index is faster
B. The stack can grow dynamically without a fixed size limit
C. Memory usage is always less
D. It uses less pointer overhead
Solution
Step 1: Understand stack growth in arrays
Arrays have a fixed size, so stacks implemented with arrays have a maximum capacity.
Step 2: Understand linked list flexibility
Linked lists can grow dynamically by adding 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 = B [OK]
Hint: Linked list stacks grow dynamically, arrays have fixed size [OK]
Common Mistakes:
Thinking arrays can grow dynamically without resizing
Assuming linked lists use less memory always
Confusing access speed advantages
2. Which of the following is the correct way to declare an array-based stack of integers with size 10 in C#?
easy
A. int[] stack = new int[10];
B. int stack = new int[10];
C. int[] stack = int[10];
D. int stack[] = new int(10);
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared with type[], then new type[size].
Step 2: Check each option
int[] stack = new int[10]; matches correct syntax: int[] stack = new int[10]; others have syntax errors.
Final Answer:
int[] stack = new int[10]; -> Option A
Quick Check:
C# array syntax = D [OK]
Hint: Use type[] name = new type[size]; for arrays in C# [OK]
Common Mistakes:
Missing [] after type
Using parentheses instead of brackets for size
Declaring array without new keyword
3. Consider this C# code using a linked list stack implementation:
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;
Console.WriteLine(popped);
What is the output?
medium
A. NullReferenceException
B. 10
C. 20
D. 0
Solution
Step 1: Trace push operations
First push 10: top points to node with 10. Then push 20: top points to node with 20, next points to 10.
Step 2: Trace pop operation
Pop reads top.Data = 20, then top moves to next node (10).
Final Answer:
20 -> Option C
Quick Check:
Last pushed is first popped = A [OK]
Hint: Last pushed node is popped first in linked list stack [OK]
Common Mistakes:
Confusing push order and pop order
Assuming pop returns bottom element
Forgetting to update top pointer
4. The following C# code attempts to push an element onto an array-based stack but causes a runtime error:
int[] stack = new int[2];
int top = 2;
stack[top] = 5;
What is the main issue causing the error?
medium
A. NullReferenceException due to uninitialized array
B. Stack overflow exception because stack is full
C. Syntax error in array declaration
D. Array index out of bounds because 'top' equals array size
Solution
Step 1: Understand array indexing in C#
Array indices go from 0 to length-1, so valid indices are 0 and 1 for size 2.
Step 2: Check 'top' value usage
Variable 'top' is 2, which is outside valid index range, causing index out of bounds error.
Final Answer:
Array index out of bounds because 'top' equals array size -> Option D
Quick Check:
Index must be less than array size = A [OK]
Hint: Array indices start at 0, max index is size-1 [OK]
Common Mistakes:
Confusing stack overflow with index error
Assuming array indices start at 1
Ignoring that array is initialized properly
5. You need to implement a stack that will store a very large number of elements, but memory is limited and resizing arrays is costly. Which implementation is better and why?
Array-based stack with resizing
Linked list-based stack
hard
A. Linked list stack, because it uses memory only as needed and avoids resizing overhead
B. Array stack, because arrays always use less memory than linked lists
C. Array stack, because resizing is fast and memory is unlimited
D. Linked list stack, because it allows random access to elements
Solution
Step 1: Consider memory usage and resizing cost
Array stacks need resizing which copies all elements, costly in time and memory.
Step 2: Consider linked list memory behavior
Linked lists allocate memory per element, no resizing needed, so better for large unknown sizes with limited memory.
Final Answer:
Linked list stack, because it uses memory only as needed and avoids resizing overhead -> Option A
Quick Check:
Linked list avoids resizing overhead = C [OK]
Hint: Linked list stacks avoid costly resizing for large data [OK]
Common Mistakes:
Assuming arrays always use less memory
Ignoring resizing cost in arrays
Thinking linked lists allow random access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.