Docsity

Linked List Implementation of a Stack in Java, Exams of Electronics

Partial preview of the text

Download Linked List Implementation of a Stack in Java and more Exams Electronics in PDF only on Docsity!

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms F O U R T H E D I T I O N

h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u

Algorithms ROBERT^ SEDGEWICK^ |^ KEVIN^ WAYNE

1.3 BAGS, QUEUES, AND STACKS

‣ stacks

‣ resizing arrays

‣ queues

‣ generics

‣ iterators

‣ applications

Fundamental data types. Value: collection of objects.

Operations: Intent is clear when we insert. insert, remove, iterate, test if empty.

Which item do we remove?

Stack. Queue. Examine the item most recently added.Examine the item least recently added.

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

Warmup API. Stack of strings data type.

Warmup client. Reverse sequence of strings from standard input. 5

Stack API

push pop

publicpublicpublic classclassclass StackOfStringsStackOfStringsStackOfStrings

StackOfStrings() create an empty stack

void push(String item) insert a new string onto stack

String pop() remove and return the string most recently added

boolean isEmpty() is the stack empty?

int size() number of strings on the stack

Maintain pointer to first node in a linked list; insert/remove from front.

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

be

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)

Proposition. Every operation takes constant time in the worst case.

Proposition. A stack with N items uses ~ 40 N bytes.

Remark. (but not the memory for strings themselves, which the client owns). This accounts for the memory for the stack

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

Overflow and underflow. Underflow: throw exception if pop from an empty stack.

Overflow: use resizing array for array implementation. [stay tuned]

Null items. We allow null items to be inserted.

Loitering. Holding a reference to an object when it is no longer needed.

Stack considerations

garbage collector can reclaim memory^ this version avoids "loitering": only if no outstanding references

public { String pop()

String s[N] = itemnull; = s[--N];

loitering } return item;

public { return String s[--N]; pop() }

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

Problem. Q. How to grow and shrink array? Requiring client to provide capacity does not implement API!

First try. push(): increase size of array s[] by 1.

pop(): decrease size of array s[] by 1.

Too expensive. Need to copy all items to a new array.

Inserting first N items takes time proportional to 1 + 2 + … + N ~ N^2 / 2.

Challenge. Ensure that array resizing happens infrequently.

infeasible for large N

Cost of inserting first N items. N + (2 + 4 + 8 + … + N ) ~ 3 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)

Amortized cost of adding to a Stack

cost (array accesses) number of push() operations

for each operation^ one gray dot

64 red dots give cumulative average 3

Q. How to shrink array?

First try. push(): double size of array s[] when array is full.

pop(): halve size of array s[] when array is one-half full.

Too expensive in worst case. Consider push-pop-push-pop-… sequence when array is full.

Each operation takes time proportional to N.

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