Stack Implementations
Using Linked Lists
public class LinkedListOfStrings {
private node head = null ;
// Inner class
private class Node
{
String item;
Node next;
}
public boolean isEmpty {
return head == null;
}
public void push(String item) {
Node oldNode = head;
Node newNode = null;
newNode.item = item;
item.next = oldNode;
}
public String pop(){
String item = head.item;
head = head.next;
return item;
}
}Memory Calculation for Node
Using Array - Fixed size
Using Array - dynamic size
Generic Stack Linked List Implementation
Generic Stack Array Implementation
Stack Iteration
Trade offs
Can implement a stack with either resizing array or linked list. Which one is better?
Linked List Implementation:
Every operation takes constant time in the worst case.
Uses extra time and space to deal with the links.
Resizing-array implementation:
Every operation takes constant amortized time.
Less wasted space.
Last updated
Was this helpful?