15_stacks - 2/3/08 Stacks and Queues Stacks, Linked Lists,...

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

View Full Document Right Arrow Icon
2/3/08 1 Stacks, Linked Lists, and Queues 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
2/3/08 2 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
2/3/08 3 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). Unwind recursion: linked list is a sequence of items. Node data type. A reference to a String . A reference to another Node . Linked Lists public class Node { private String item ; private Node next ; } Alice Bob Carol first item next special value null terminates list 11 Building a Linked List Node third = new Node (); third . item = "Carol" ; third . next = null ; Node second = new Node second . item = "Bob" ; second . next = third ; Node first = new Node first . item = "Alice" ; first . next = second ; "Carol" null C0 C1 - - C2 C3 "Alice" CA C4 C5 - - C6 C7 - - C8 C9 "Bob" C0 CA CB - - CC CD - - CE CF Value addr Carol null item next third C0 third main memory Bob second CA second Alice first C4 first 12 Traversing a Linked List Iteration. Idiom for traversing a null-terminated linked 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 / 12

15_stacks - 2/3/08 Stacks and Queues Stacks, Linked Lists,...

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