1) What is a Stack?
A stack is a linear data structure that follows LIFO — Last In, First Out. Push adds an item to the top; Pop removes the most recent item. Linked Lists make stacks dynamic, without a fixed array size.
Why Linked List? No fixed capacity, O(1) push/pop, and no resizing needed.
2) Step 1 — The Node Class
Each node stores a value and a pointer to the next node. New nodes become the new top.
class Node {
int val;
Node next; // pointer to next node
Node(int val) {
this.val = val;
this.next = null; // initially end of list
}
}
3) Step 2 — The stack_linklist Class
We keep a reference to the top node. Push adds a new node on top; Pop removes the top node; Peek returns the top value without removal.
public class stack_linklist {
private Node top; // points to the top of the stack
public stack_linklist() {
this.top = null;
}
// Push: O(1)
public void push(int v) {
Node new_node = new Node(v);
new_node.next = top;
top = new_node;
System.out.println("Pushed " + v + " into stack");
}
// Pop: O(1)
public int pop() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
}
int v = top.val;
top = top.next;
return v;
}
// Peek: O(1)
public int peek() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
}
return top.val;
}
public boolean isEmpty() {
return top == null;
}
public void display() {
if (top == null) {
System.out.println("Stack is empty");
return;
}
System.out.print("Stack elements (top to bottom): ");
Node t = top;
while (t != null) {
System.out.print(t.val + " ");
t = t.next;
}
System.out.println();
}
public static void main(String[] args) {
stack_linklist stk = new stack_linklist();
stk.push(5);
stk.push(7);
stk.push(3);
stk.push(8);
stk.display();
System.out.println("Popped: " + stk.pop()); // 8
System.out.println("Popped: " + stk.pop()); // 3
System.out.println("Popped: " + stk.pop()); // 7
System.out.println("Popped: " + stk.pop()); // 5
System.out.println("Popped: " + stk.pop()); // empty
}
}
Tip: Return
-1 for empty pops in demo code, but in production prefer throwing EmptyStackException for clarity.4) How Push/Pop Works (Mental Model)
- Push: create a node → point its
nextto currenttop→ reassigntopto this node. - Pop: read
top.val→ movetopdown totop.next→ return the value.
Common bug: Forgetting
new_node.next = top will break the chain and lose the rest of the stack.5) Practice Problems
- Reverse a String: push characters, pop to rebuild reversed string.
- Balanced Parentheses: push '(' and pop on ')'; stack must be empty at the end.
- Postfix Evaluation: push numbers, pop two on operator, compute, push result.
- Decimal to Binary: push remainders of division by 2, then pop to print.
- Undo/Redo Simulation: two stacks — one for undo, one for redo.
6) Full Source (Single File)
Copy this single file into stack_linklist.java and run it.
class Node {
int val;
Node next;
Node(int val) { this.val = val; this.next = null; }
}
public class stack_linklist {
private Node top;
public stack_linklist() { this.top = null; }
public void push(int v) {
Node n = new Node(v);
n.next = top;
top = n;
}
public int pop() {
if (top == null) return -1;
int v = top.val;
top = top.next;
return v;
}
public int peek() {
if (top == null) return -1;
return top.val;
}
public boolean isEmpty() { return top == null; }
public static void main(String[] args) {
stack_linklist s = new stack_linklist();
s.push(5); s.push(7); s.push(3); s.push(8);
System.out.println("Popped: " + s.pop());
System.out.println("Popped: " + s.pop());
System.out.println("Popped: " + s.pop());
System.out.println("Popped: " + s.pop());
System.out.println("Popped: " + s.pop());
}
}
7) Next Steps
- Add
size()andclear()methods. - Convert to a generic stack:
class Stack<T>. - Implement a min stack that returns the minimum element in O(1).