Stack Implementation Using Linked List
What is Stack implementation using Linked List?
A stack implemented using a linked list follows the LIFO (Last In First Out) principle. Unlike array implementation, linked list stacks dynamically allocate memory for each element and don't have size limitations (until memory is exhausted).
Core Stack Operations
push(item)
pop()
peek()
isEmpty()
size()
Algorithmic Steps
Stack Basic Operations
Initialize Stack
- Create a head pointer initialized to null
- Optional: Maintain a size counter initialized to 0
push()
- Create a new node with the given data
- Set new node's next pointer to current head
- Update head to point to the new node
- Increment size counter (if maintained)
pop()
- Check if stack is empty (head is null)
- If empty, return "Stack Underflow"
- Store current head node in a temporary variable
- Update head to point to the next node
- Decrement size counter (if maintained)
- Return data from the temporary node
Stack Helper Operations
peek()
- Check if stack is empty (head is null)
- If empty, return null
- Return data from head node without removal
isEmpty()
- Return true if head is null
- Return false otherwise
size()
- If size counter is maintained, return its value
- Otherwise, traverse the list and count nodes
Visual Representation
Head
8
(top)
17
42
null
Stack after operations: push(42), push(17), push(8)
Time Complexity
Operation | Complexity |
---|---|
push() | O(1) |
pop() | O(1) |
peek() | O(1) |
isEmpty() | O(1) |
size() | O(1) or O(n) |
Key Characteristics
- *Dynamic Size: No fixed capacity (grows as needed)
- *Memory Efficiency: Uses only needed memory
- *No Wasted Space: Unlike array implementation
- *Extra Memory: Requires space for pointers
- *Flexibility: Can grow until memory exhausted
Linked List vs Array Implementation
Feature | Linked List | Array |
---|---|---|
Memory Usage | Extra for pointers | Fixed size, may be wasted |
Dynamic Size | Yes | No (unless resized) |
Memory Allocation | Dynamic | Static (usually) |
Access Time | O(1) for top | O(1) for all |
Implementation Complexity | Slightly more complex | Simpler |
Stack Implementation using Linked List
// Stack Implementation using Linked List (JavaScript)
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedListStack {
constructor() {
this.top = null;
this.size = 0;
}
// Push operation
push(value) {
const newNode = new Node(value);
newNode.next = this.top;
this.top = newNode;
this.size++;
}
// Pop operation
pop() {
if (this.isEmpty()) {
console.log("Stack Underflow");
return null;
}
const value = this.top.value;
this.top = this.top.next;
this.size--;
return value;
}
// Peek operation
peek() {
if (this.isEmpty()) {
console.log("Stack is empty");
return null;
}
return this.top.value;
}
// Check if stack is empty
isEmpty() {
return this.size === 0;
}
// Get stack size
getSize() {
return this.size;
}
// Print stack contents
print() {
if (this.isEmpty()) {
console.log("Stack is empty");
return;
}
let current = this.top;
console.log("Stack contents (top to bottom):");
while (current) {
console.log(current.value);
current = current.next;
}
}
}
// Usage
const stack = new LinkedListStack();
stack.push(10);
stack.push(20);
stack.push(30);
console.log("Top element:", stack.peek()); // 30
console.log("Stack size:", stack.getSize()); // 3
stack.print();
stack.pop();
console.log("After pop, top element:", stack.peek()); // 20
Features: Push, Pop, Peek, isEmpty, Size, and Print operations
Note: Linked list implementation doesn't have fixed size limitation