4.3 Stacks and Queues

4.3 Stacks and Queues

4.3 Stacks and Queues 1 Data Structures and Data Types Collections Data types Fundamental data types. • Set of values. • Set of operations (add, remove, test if empty) on generic data. • Set of operations on those values. • Intent is clear when we insert. • Some are built in to Java: int, double, char, String, . • Which item do we remove? Most are not: Complex, Picture, Charge, Stack, Queue, Graph, . • LIFO = "last in first out" Stack. (this lecture) this lecture Data structures. • Remove the item most recently added. • Represent data. • Ex: cafeteria trays, Web surfing. Represent relationships among data. • FIFO = "first in first out" • Some are built in to Java: arrays, . Queue. (see text) • Most are not: linked list, circular list, tree, sparse array, graph, . • Remove the item least recently added. • Ex: Registrar's line. this lecture TSP next lecture (assignment 8) Symbol Table. (next lecture) Design challenge for every data type: What data structure to use? • Remove item with a given key. • Requirement 1: Space usage. • Ex: Phone book • Requirement 2: Time usage for data-type methods 3 4 FIFO Queue API FIFO Queues public class QueueOfStrings QueueOfStrings() create an empty queue int length() size of the queue void put(String item) put a string onto the queue String get() get a string from the queue it it was put was get was get the the the the length best best best best 3 of of of 5 6 Queue Client Code Example: Read from input stream into an array from previous lecture public class WhiteFilter { public static void main(String[] args) Pushdown Stacks { In in = new In(args[0]); String[] words; // Fill words[] with strings from In (stay tuned). while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (search(key, words) != -1) StdOut.println(key); QueueOfStrings q = new QueueOfStrings(); } while (!in.isEmpty()) } q.put(in.readString()); } int N = q.length(); words = new String[N]; for (int i = 0; i < N; i++) words[i] = q.get(); Solves basic problem • Can’t store strings in array until it is created. • Can’t create array without knowing how many strings in input stream. • Can’t know how many strings in input stream without reading them all. • Solution: keep them in a Queue See text for implementation/applications (after learning about Stacks). 7 8 Stack API Stack Client Example 1: Reverse public class Reverse { public static void main(String[] args) { StackOfStrings stack = new StackOfStrings(); while (!StdIn.isEmpty()) stack.push(StdIn.readString()); while (!stack.isEmpty()) StdOut.print(stack.pop()); *: we will consider more than one implementation StdOut.println(); } } % more tiny.txt it was the best of times times % java Reverse tiny.txt times of of of times of best the was it of best push best pop best pop best the the the the best stack contents when StdIn is empty was was was was the it it it it was it 9 10 Stack Client Example 2: Test Client Stack Client Example 3: Balanced Parentheses public static void main(String[] args) { StackOfStrings stack = new StackOfStrings(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); ( ( ( a + b ) * d ) + ( e * f ) ) if (item.compareTo("-") != 0) stack.push(item); ( ) else ( ) ( ) System.out.print(stack.pop()); ( ) } push pop System.out.println(); push pop push pop } % more test.txt push pop to be or not to - be - - that - - - is % java StackOfStrings < test.txt to be not that or be to not stack contents just before first pop() operation or be to 11 12 Stack Client Example 3: Balanced Parentheses Stack: Array Implementation public class Balanced { Array implementation of a stack. public static void main(String[] args) • Use array a[] to store N items on stack. PROBLEM: How big to make array? (Stay tuned.) { push StackOfStrings stack = new StackOfStrings(); • push() add new item at a[N]. while (!StdIn.isEmpty()) pop() remove item from a[N-1]. { • pop String item = StdIn.readString(); if (item.compareTo("(") == 0) Strawman solution: Make client provide capacity. stack.push(item); public class ArrayStackOfStrings NOTE: This ‘solution’ violates the API! if (item.compareTo(")") == 0) { { private String[] a; if (stack.isEmpty()) private int N = 0; { StdOut.println(“Not balanced”); return; } stack.pop(); public ArrayStackOfStrings(int max) } { a = new String[max]; } } not stack contents after if (!stack.isEmpty()) StdOut.println(“Not balanced”); public boolean isEmpty() 4th push() operation else StdOut.println(“Balanced”); or } { return (N == 0); } } be % java Balanced public void push(String item) ( ( ( a + b ) * d ) + ( e * f ) ) { a[N++] = item; } to N Balanced public String pop() % java Balanced { return a[--N]; } to be or not ( ( a + b ) * d ) + ( e * f ) ) Not balanced } a[0] a[1] a[2] a[3] 13 14 Array Stack: Trace TEQ on Stacks Q. Can we always insert pop commands (-) to make strings come out sorted? Ex 1: 6 5 4 3 2 1 - - - - - push Ex 2: 1 - 2 - 3 - 4 - 5 - 6 - Ex 3: 4 1 - 3 2 - - - 6 5 - - pop 15 16 Array Stack: Performance Example: potential stack client Possible implementation of Java memory management system (sketch) Running time. Push and pop take constant time. ✓ Maintain N stacks stack i: blocks of contiguous 2i byte chunks of memory Memory. Proportional to client-supplied capacity, not number of items. ✗ • • new: pop from stack t, where 2t is smallest block that will hold new object stack t empty? pop from t+1, split in half, push 2 blocks on stack t Problem. • garbage collector: periodically finds unused memory blocks How? See COS 226. Original API does not call for capacity (never good to change API) • • and pushes onto appropriate stack. • Client might have multiple stacks • Client might not know what capacity to use (depends on its client) Properties • many stacks • stack size unpredictable . 2 4 8 16 Stack implementation without capacity restriction (as in API) is a requirement Challenge. Stack implementation where space use is not fixed ahead of time. 17 18 Sequential vs. Linked Data Structures Linked Lists Sequential data structure. Put object one next to another. • TOY: consecutive memory cells. • Java: array of objects. Linked data structure. Include in each object a link to the another one. • TOY: link is memory address of next object. • Java: link is reference to next object. addr value addr value C0 "Alice" C0 "Carol" C1 "Bob" C1 null th Key distinctions. get i element C2 "Carol" C2 - Array: arbitrary access, fixed size. C3 - C3 - • C4 - C4 "Alice" Linked list: sequential access, variable size. • C5 - C5 CA C6 - C6 - get next element C7 - C7 - C8 - C8 - C9 - C9 - Linked structures. CA - CA "Bob" CB - CB C0 • Not intuitive, overlooked by naive programmers linked list • Flexible, widely used method for organizing data array 19 20 Singly-linked data structures Linked Lists From the point of view of a particular object, all of these structures Linked list. look the same: • Simplest linked structure. A recursive data structure. Sequential list (this lecture) Rho • • An item plus a pointer to another linked list (or empty list). • Unwind recursion: linked list is a sequence of items. Tree Circular list (TSP) Node data type. public class Node { A reference to a String. • private String item; • A reference to another Node. private Node next; } Confusing point: General case Purpose of data structure is to represent data in a data type but, we also use data types to implement data structures Example: The data type Node acts behind the scenes to implement the linked list data structure. It is not visible to the client. first Alice Bob Carol item next Multiply linked structures: many more possibilities! special pointer value null terminates list 21 22 Building a Linked List Traversing a List Iteration. Idiom for traversing a null-terminated linked list. addr Value Node third = new Node(); third.item = "Carol"; C0 "Carol" third.next = null; C1 null Node x = first; Node second = new Node(); C2 - while (x != null) second.item = "Bob"; C3 - { second.next = third; StdOut.println(x.item); first C4 C4 "Alice" x = x.next; Node first = new Node(); } first.item = "Alice"; second CA C5 CA first.next = second; third C0 C6 - shorthand version C7 - for (Node x = first; x != null; x = x.next) C8 - StdOut.println(x.item); C9 - CA "Bob" CB C0 first second third StdOut CC - first CD - Alice Bob Carol Alice Bob Carol CE - item next item next null CF - main memory 23 24 Stack Push: Linked List Implementation Stack Pop: Linked List Implementation first "of" best the was it first of best the was it item = first.item; first second best the was it second = first; first of best the was it first = first.next; first second garbage-collected best the was it first = new Node(); first first second best the was it return item; of best the was it first.item = item; first.next = second; 25 26 Stack: Linked List Implementation Linked List Stack: Trace public class LinkedStackOfStrings { private Node first = null; push private class Node not stack contents after { 4th push() operation private String item; or in test client private Node next; } be public boolean isEmpty() to { return first == null; } pop public void push(String item) { first Node second = first; first = new Node(); not first.item = item; first.next = second; } or public String pop() be { String item = first.item; to first = first.next; Note difference between first and second: return item; first: an instance variable that retains state } second: a local variable that goes out of scope } 27 28 Linked-List Stack: Performance Stack Data Structures: Tradeoffs Two data structures to implement the Stack data type. Running time. Push and pop take constant time. ✓ Memory. Always proportional to number of items in stack. ✓ Array. • Every push/pop operation take constant time. • But does not implement API… (must fix max capacity ahead of time).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us