43stack

43stack - 4.3 Stacks and Queues Introduction to Programming...

Info iconThis preview shows pages 1–12. 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 · April 6, 2009 9:40 tt
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 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"
Background image of page 2
3 Stacks
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Array Stack: Trace push pop
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Linked Lists
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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 null first item next special value null terminates list
Background image of page 10
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
Background image of page 11

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

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

This note was uploaded on 02/10/2012 for the course CSE 1002 taught by Professor Marico during the Spring '08 term at FIT.

Page1 / 48

43stack - 4.3 Stacks and Queues Introduction to Programming...

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

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