43stack - Stacks and Queues 4.3 Stacks and Queues...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
4.3 Stacks and Queues Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October 30, 2008 11:08 AM 2 Stacks and Queues Fundamental data types. ! Set of operations ( add , remove , test if empty ) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack. ! Remove the item most recently added. ! Ex: cafeteria trays, Web surfing. Queue. ! Remove the item least recently added. ! Ex: Registrar's line. FIFO = "first in first out" LIFO = "last in first out" 3 Stacks 4 Stack API pop push public class Reverse { public static void main ( String [] args ) { StackOfStrings stack = new StackOfStrings (); while (! StdIn . isEmpty ()) stack . push ( StdIn . readString ()); while (! stack . isEmpty ()) StdOut . println ( stack . pop ()); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 Stack: Array Implementation Array implementation of a stack. ! Use array a[] to store N items on stack. ! push() add new item at a[N] . ! pop() remove item from a[N-1] . it was the best 0 1 2 3 4 5 6 7 8 9 a[] N public class ArrayStackOfStrings { private String [] a ; private int N = 0 ; public ArrayStackOfStrings ( int max ) { a = new String [ max ]; } public boolean isEmpty () { return ( N == 0 ); } public void push ( String item ) { a [ N ++] = item ; } public String pop () { return a [ -- N ]; } } max capacity of stack 6 Array Stack: Trace push pop 7 Array Stack: Performance Running time. Push and pop take constant time. Memory. Proportional to max . Challenge. Stack implementation where size is not fixed ahead of time. 8 Linked Lists
Background image of page 2
9 Sequential vs. Linked Allocation Sequential allocation. Put object one after another. ! TOY: consecutive memory cells. ! Java: array of objects. Linked allocation. Include in each object a link to the next one. ! TOY: link is memory address of next object. ! Java: link is reference to next object. Key distinctions. ! Array: random access, fixed size. ! Linked list: sequential access, variable size. "Carol" null C0 C1 - - C2 C3 "Alice" CA C4 C5 - - C6 C7 - - C8 C9 "Bob" C0 CA CB value addr "Alice" "Bob" C0 C1 "Carol" - C2 C3 - - C4 C5 - - C6 C7 - - C8 C9 - - CA CB value addr array linked list get i th element get next element 10 Linked list. ! A recursive data structure. ! A item plus a pointer to another linked list (or empty list).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

43stack - Stacks and Queues 4.3 Stacks and Queues...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online