cse 143
play

CSE 143 Stacks and Queues Reading: Secs. 25.1 & 25.2 12/2/2004 - PDF document

Typing and Correcting Chars What data structure would you use for this problem? User types characters on the command line Until she hits enter, the backspace key (<) can be used to "erase the previous character" CSE 143


  1. Typing and Correcting Chars • What data structure would you use for this problem? • User types characters on the command line • Until she hits enter, the backspace key (<) can be used to "erase the previous character" CSE 143 Stacks and Queues Reading: Secs. 25.1 & 25.2 12/2/2004 (c) 2001-4, University of Washington 17-1 12/2/2004 (c) 2001-4, University of Washington 17-2 Sample Analysis • Action • Result • We need to store a sequence of characters • type h • h • The order of the characters in the sequence is • type e • he significant • type l • hel • type o • helo • Characters are added at the end of the sequence • type < • hel • We only can remove the most recently entered character • type l • hell • type w • hellw • type < • hell • We need a data structure that is Last in, first out , or LIFO • type < • hel – a stack • type < • he • type < • h • Many examples in real life: stuff on top of your desk, trays in • type i • hi the cafeteria, discard pile in a card game, … 12/2/2004 (c) 2001-4, University of Washington 17-3 12/2/2004 (c) 2001-4, University of Washington 17-4 Stack Terminology Stack Operations • Top : Uppermost element of • push(Object): Add an element to the top of the stack, stack, increasing stack height by one • first to be removed • Object pop( ): Remove topmost element from stack and top • Bottom : Lowest element of return it, decreasing stack height by one stack, • Object top( ): Returns a copy of topmost element of • last to be removed stack, leaving stack unchanged • Elements are always inserted ... and removed from the top • No “direct access” (LIFO – L ast I n, F irst O ut) • cannot index to a particular data item bottom • No convenient way to traverse the collection aStack: 12/2/2004 (c) 2001-4, University of Washington 17-5 12/2/2004 (c) 2001-4, University of Washington 17-6 CSE143 Au04 17-1

  2. Picturing a Stack What is the result of... • Stack pictures are usually s somewhat abstract Stack s; Object v1,v2,v3,v4,v5,v6; • Not necessary to show details s.push(“Yawn”); of object references, names, s.push(“Burp”); etc. v1 = s.pop( ); • Unless asked to do so, or course! s.push(“Wave”); • "Top" of stack can be up, s.push(“Hop”); down, left, right – just label it. v2 = s.pop( ); s.push(“Jump”); v3 = s.pop( ); v4 = s.pop( ); v1 v2 v3 v4 v5 v6 v5 = s.pop( ); v6 = s.pop( ); 12/2/2004 (c) 2001-4, University of Washington 17-7 12/2/2004 (c) 2001-4, University of Washington 17-8 Stack Practice Stack Implementations • Several possible ways to implement • Show the changes to the stack in the following example: • An array • A linked list Stack s; How would you do these? Tradeoffs? Object obj; • Java library does not have a Stack class s.push(“abc”); s.push(“xyzzy”); • Easiest way in Java: implement with some sort of List s.push(“secret”); • push(Object) add(Object) obj = s.pop( ); • top( ) get(size( ) –1) obj = s.top( ); • pop( ) remove(size( ) -1) s.push(“swordfish”); • Precondition for top( ) and pop( ): stack not empty s.push(“terces”); • Cost of operations? O(?) 12/2/2004 (c) 2001-4, University of Washington 17-9 12/2/2004 (c) 2001-4, University of Washington 17-10 An Application: What Model Do We Want? Queue Definition • waiting line at the movie theater... • Queue: Ordered collection, accessed only at the front (remove) and rear • job flow on an assembly line... (insert) • traffic flow at the airport.... • Front: First element in queue • "Your call is important to us. Please stay on the line. Your call • Rear: Last element of queue will be answered in the order received. Your call is important to • FIFO: First In, First Out us... • Footnote: picture can be drawn in any • … direction • Characteristics • Objects enter the line at one end (rear) ... • Objects leave the line at the other end (front) aQueue: front rear • This is a “ first in, first out ” (FIFO) data structure. 12/2/2004 (c) 2001-4, University of Washington 17-11 12/2/2004 (c) 2001-4, University of Washington 17-12 CSE143 Au04 17-2

  3. Abstract Queue Operations Queue Example • insert(Object) – Add an element to rear of a queue • Draw a picture and show the changes to the queue in the following example: • succeeds unless the queue is full (if implementation is bounded) Queue q; Object v1, v2; • often called “enqueue” q.insert(“chore”); • Object front( ) – Return a copy of the front element of a q.insert(“work”); queue q.insert(“play”); • precondition: queue is not empty v1 = q.remove(); v2 = q.front(); • Object remove( ) – Remove and return the front element q.insert(“job”); of a queue q.insert(“fun”); • precondition: queue is not empty • often called “dequeue” 12/2/2004 (c) 2001-4, University of Washington 17-13 12/2/2004 (c) 2001-4, University of Washington 17-14 What is the result of: Queue Implementations Queue q; Object v1,v2,v3,v4,v5,v6 • Similar to stack q.insert(“Sue”); • Array – trick here is what do you do when you run off the end q.insert(“Sam”); • Linked list – ideal, if you have both a first and a last pointer. q.insert(“Sarah”); • No standard Queue class in Java library v1 = q.remove( ); v2 = q. front( ); • Easiest way in Java: use LinkedList class q.insert(“Seymour”); • insert(Object) addLast(Object) [or add(Object)] v3 = q.remove( ); • getFront( ) getFirst( ) v4 = q.front( ); q.insert(“Sally”); • remove( ) removeFirst( ) v5 = q.remove( ); Interesting "coincidence" – a Java LinkedList supports exactly the operations you v6 = q. front( ); would want to implement queues. Internally it uses a doubly-linked list, where each node has a reference to the previous node as well as the next one 12/2/2004 (c) 2001-4, University of Washington 17-15 12/2/2004 (c) 2001-4, University of Washington 17-16 Bounded vs Unbounded Summary • In the abstract, queues and stacks are generally thought of as • Stacks and Queues "unbounded": no limit to the number of items that can be • Specialized list data structures for specific applications inserted. • Stack • In most practical applications, only a finite size can be • LIFO (Last in, first out) accommodated: "bounded". • Operations: push(Object), top( ), and pop( ) • Assume "unbounded" unless you hear otherwise. • Queue • Makes analysis and problem solution easier • FIFO (First in, first out) • Well-behaved applications rarely reach the physical limit • Operations: insert(Object), getFront( ), and remove( ) • When the boundedness of a queue is an issue, it is sometimes • Implementations: arrays or lists are possibilities for called a "buffer" each • People speak of bounded buffers and unbounded buffers • Frequent applications in systems programming E.g. incoming packets, outgoing packets 12/2/2004 (c) 2001-4, University of Washington 17-17 12/2/2004 (c) 2001-4, University of Washington 17-18 CSE143 Au04 17-3

Recommend


More recommend