
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The implementation of a stack using a linked list in Java. It includes methods for pushing and popping elements from the stack, as well as an analysis of the performance and memory usage of the linked list implementation compared to an array implementation.
Typology: Exams
1 / 64
This page cannot be seen from the preview
Don't miss anything!

























































ROBERT SEDGEWICK | KEVIN WAYNE
h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u
pop
stack push
Stacks and queues
LIFO = "last in first out" FIFO = "first in first out"
enqueue^ queue dequeue
h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u ROBERT^ SEDGEWICK^ | KEVIN^ WAYNE
Algorithms
‣ stacks ‣ resizing arrays ‣ queues ‣ generics ‣ iterators ‣ applications
1.3 BAGS, QUEUES, AND STACKS
Stack API
push pop
to be to or be null to null
null
not or be to to not or^ null be to not or be be not or be not or be not or be null to that or be
StdIn to StdOut be or not to
that
null^ to null^ to null^ to
null^ to be (^) null to
7
Stack: linked-list representation
insert at front^ first of linked list
remove from front of linked list
Stack pop: linked-list implementation
first or be to
first = first.next;
first or be to
null null
Removing the first node in a linked list
save item to return String item = first.item; delete first node
return saved item return item;
inner class private class Node { String item; }^ Node^ next;
Stack: linked-list implementation in Java public { class LinkedStackOfStrings private Node first = null; private { class Node String Node next; item; } public { return boolean first isEmpty() == null; } public { void push(String item) Node first oldfirst = new Node(); = first; first.item first.next == item;oldfirst; } public { String pop() String first =item first.next; = first.item; }^ return^ item; }
private inner class (access modifiers don't matter)
Stack: linked-list implementation performance
8 bytes (reference to String) 8 bytes (reference to Node)
16 bytes (object overhead)
40 bytes per stack node
public { String class item; Node ... }^ Node^ next;
node object ( inner class ) 40 bytes
references
overhead^ object overhead^ extra item next^ 8 bytes (inner class extra overhead)
inner class private class Node { String item; }^ Node^ next;
public { class FixedCapacityStackOfStrings private private String[]int N = 0;s; public { s = FixedCapacityStackOfStrings(new String[capacity]; } int capacity) public { return boolean N == isEmpty()0; } public { s[N++] void = push(Stringitem; } item) public { return String s[--N]; pop() } }
Stack: array implementation
decrement N; then use to index into array
(stay tuned)^ a cheat
use to index into array; then increment N
Stack considerations
garbage collector can reclaim memory^ this version avoids "loitering": only if no outstanding references
h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u ROBERT^ SEDGEWICK^ | KEVIN^ WAYNE
Algorithms
‣ stacks ‣ resizing arrays ‣ queues ‣ generics ‣ iterators ‣ applications
1.3 BAGS, QUEUES, AND STACKS
Stack: resizing-array implementation
infeasible for large N
Stack: amortized cost of adding to a stack
1 array access per push k array accesses to double to size k (ignoring cost to create new array)
cost (array accesses) number of push() operations
for each operation^ one gray dot
Stack: resizing-array implementation
N = (^5) to be or not to null null null N = (^4) to be or not N = (^5) to be or not to null null null N = (^4) to be or not