26stack - Stacks and Queues 2.6 Stacks and Queues...

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

View Full Document Right Arrow Icon
Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 2.6 Stacks 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 . ! Analogy: cafeteria trays, Web surfing. Queue. ! Remove the item least recently added . ! Analogy: Registrar's line. FIFO = "first in first out" LIFO = "last in first out" enqueue dequeue pop push 3 Client, Implementation, Interface Separate interface and implementation so as to: ! Build layers of abstraction. ! Reuse software. ! Ex: stack, queue, symbol table. Interface: description of data type, basic operations. Client: program using operations defined in interface. Implementation: actual code implementing operations. 4 Client, Implementation, Interface Benefits. ! Client can't know details of implementation ! client has many implementation from which to choose. ! Implementation can't know details of client needs ! many clients can re-use the same implementation. ! Design: creates modular, re-usable libraries. ! Performance: use optimized implementation where it matters. Interface: description of data type, basic operations. Client: program using operations defined in interface. Implementation: actual code implementing operations.
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 Stack operations. ! push() Insert a new item onto stack. ! pop() Delete and return the item most recently added. ! isEmpty() Is the stack empty? pop push public static void main ( String [] args ) { StringStack stack = new StringStack (); while (! StdIn . isEmpty ()) { String s = StdIn . readString (); stack . push ( s ); } while (! stack . isEmpty ()) { String s = stack . pop (); System . out . println ( s ); } } a sample stack client 6 Stack Pop: Linked List Implementation best the was it best the was it first = first.next; best the was it return item; first first first of item = first.item; 7 Stack Push: Linked List Implementation best the was it second best the was it best the was it first of second = first; first.item = item; first.next = second; best the was it second first = new Node(); first second first first 8 Stack: Linked List Implementation public class StringStack { private Node first = null ; private class Node { String item ; Node next ; } public boolean isEmpty () { return first == null ; } public void push ( String item ) { Node second = first ; first = new Node (); first . item = item ; first . next = second ; } public String pop () { String item = first . item ; first = first . next ; return item ; } } "inner class"
Background image of page 2
9 Stack: Array Implementation Array implementation of a stack. ! Use array
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.

This note was uploaded on 06/10/2011 for the course COS 226 taught by Professor Kevinwayne during the Spring '08 term at Princeton.

Page1 / 10

26stack - Stacks and Queues 2.6 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