📖 Scenario: You are building a simple stack to manage tasks in a to-do list app. You want to understand how using a linked list or an array affects your stack's behavior and performance.
🎯 Goal: Create two stacks: one using a linked list and one using an array. Push and pop items to see how each works and compare their outputs.
📋 What You'll Learn
Create a linked list node class called ListNode with value and next properties
Create a LinkedListStack class with push, pop, and printStack methods
Create an ArrayStack class with push, pop, and printStack methods
Push the same items to both stacks and pop one item from each
Print the contents of both stacks after operations
💡 Why This Matters
🌍 Real World
Stacks are used in undo features, expression evaluation, and managing function calls in programming languages.
💼 Career
Understanding stack implementations helps in optimizing memory and performance in software development and system design.
Progress0 / 4 steps
1
Create Linked List Node and LinkedListStack Class
Create a class called ListNode with a constructor that takes a value of type string and a next property initialized to null. Then create a class called LinkedListStack with a top property initialized to null.
DSA Typescript
Hint
Think of ListNode as a box holding a value and a pointer to the next box. The LinkedListStack keeps track of the top box.
2
Add push and pop Methods to LinkedListStack
Add a push method to LinkedListStack that takes a value string, creates a new ListNode, and sets it as the new top. Add a pop method that removes and returns the top node's value or returns null if empty.
DSA Typescript
Hint
Push adds a new node on top. Pop removes the top node and returns its value.
3
Create ArrayStack Class with push and pop Methods
Create a class called ArrayStack with a private array property called items initialized as an empty array. Add a push method that adds a string to items and a pop method that removes and returns the last string or returns null if empty.
DSA Typescript
Hint
ArrayStack uses a simple array to hold items. Push adds to the end, pop removes from the end.
4
Push and Pop Items, Then Print Both Stacks
Create instances of LinkedListStack and ArrayStack. Push the strings "task1", "task2", and "task3" to both stacks. Pop one item from each stack. Then print the remaining items in both stacks using a printStack method you add to each class that prints items from top to bottom separated by ' -> ' and ending with ' -> null'.
DSA Typescript
Hint
Print from top to bottom for linked list by following next. For array, print from last to first element.
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. Dynamic size without needing to resize
B. Faster access to elements by index
C. Uses less memory per element
D. Simpler syntax to implement
Solution
Step 1: Understand stack size behavior
Arrays have fixed size or require resizing, linked lists grow dynamically.
Step 2: Compare memory and access
Linked lists use extra memory for pointers and have slower indexed access.
Final Answer:
Dynamic size without needing to resize -> Option A
Quick Check:
Linked list stacks grow dynamically = A [OK]
Hint: Linked list stacks grow dynamically, arrays need resizing [OK]
Common Mistakes:
Thinking arrays always use less memory
Confusing access speed advantages
Assuming linked lists have simpler syntax
2. Which TypeScript syntax correctly declares an array-based stack of numbers?
easy
A. let stack = new Stack<number>();
B. const stack: number[] = [];
C. const stack: Array = number[];
D. var stack: number = [];
Solution
Step 1: Check array declaration syntax
TypeScript arrays use type[] or Array<type> syntax.
Step 2: Validate each option
const stack: number[] = []; uses correct syntax: number[] = []. Others have syntax errors or wrong types.
Final Answer:
const stack: number[] = []; -> Option B
Quick Check:
Correct array syntax = C [OK]
Hint: Array type syntax is type[] or Array<type> [OK]
Common Mistakes:
Using wrong generic syntax
Assigning array to wrong type variable
Using var instead of const/let incorrectly
3. Consider this TypeScript code using an array stack:
B. push() does not update the top pointer correctly
C. pop() returns null when stack is empty, which is correct behavior
D. Stack class does not initialize top to null
Solution
Step 1: Review pop() method behavior
pop() returns null if stack is empty, which is expected to indicate empty stack.
Step 2: Check push() and initialization
push() correctly updates top; Node has constructor; top initialized to null.
Final Answer:
pop() returns null when stack is empty, which is correct behavior -> Option C
Quick Check:
pop() returns null on empty stack = A [OK]
Hint: pop() returning null on empty stack is expected [OK]
Common Mistakes:
Thinking pop() should throw error instead
Believing push() does not update top
Missing that top is initialized properly
5. You want to implement a stack that can handle millions of push/pop operations efficiently without resizing overhead. Which implementation is better and why?
hard
A. Array stack, because arrays have faster indexed access
B. Linked list stack, because it uses contiguous memory
C. Array stack, because it uses less memory per element
D. Linked list stack, because it avoids costly array resizing
Solution
Step 1: Consider resizing overhead in array stacks
Arrays must resize and copy data when full, causing delays in many operations.
Step 2: Evaluate linked list advantages
Linked lists grow dynamically without resizing, so push/pop remain efficient for large operations.
Final Answer:
Linked list stack, because it avoids costly array resizing -> Option D
Quick Check:
Dynamic growth avoids resizing = B [OK]
Hint: Avoid resizing overhead with linked list stacks for large operations [OK]
Common Mistakes:
Assuming arrays never resize
Confusing memory usage with speed
Thinking linked lists use contiguous memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.