Lecture 8: Stacks, Queues

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Stacks & Queues EECS: Stacks & Queues Stacks April 23, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
STACKS AND QUEUES 1. Outline 2  Stacks  Queues.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Stacks 1/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks (and Queues).
Stacks 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Queues Queues Queues.
Stacks 9/12/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Stacks.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS2013 Lecture 7 John Hurley Cal State LA.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks.
Copyright © Aiman Hanna All rights reserved
Stacks and Linked Lists
Presentation transcript:

Lecture 8: Stacks, Queues CS2013 Lecture 8: Stacks, Queues

More Data Structures So far we have studied arrays and Lists in detail. As you know, this class is called "Programming With Data Structures," and obviously, data structures are the main topic of the course. Over the next few weeks, we will introduce a wide array (ha!) of new data structures. Most of these data structures can be used as collections of a wide range of parameterizing types We will first learn to use some of the data structure types included with the JDK, then implement one or two on our own, then return to using the built-in ones.

Stack A Stack is a data structure from which we access data in the reverse of the order in which it was added to the stack. In other words, a stack is a Last In First Out (LIFO) structure. Picture a stack of pancakes. If you are eating them one at a time, you must eat the last one that went on the stack first.

The Stack ADT The Stack ADT stores arbitrary objects Stacks 4/23/2019 The Stack ADT The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: push(object): inserts an element object pop(): removes and returns the last inserted element Auxiliary stack operations: object top(): returns the last inserted element without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored © 2014 Goodrich, Tamassia, Goldwasser Stacks 4 4

Stack Interface in Java Java interface corresponding to our Stack ADT Assumes null is returned from top() and pop() when stack is empty Different from the built-in Java class java.util.Stack public interface Stack<E> { int size(); boolean isEmpty(); E top(); void push(E element); E pop(); } © 2014 Goodrich, Tamassia, Goldwasser Stacks 5

Example © 2014 Goodrich, Tamassia, Goldwasser Stacks 6

Exceptions vs. Returning Null Attempting the execution of an operation of an ADT may sometimes cause an error condition Java supports a general abstraction for errors, called exception An exception is said to be “thrown” by an operation that cannot be properly executed In our Stack ADT, we do not use exceptions Instead, we allow operations pop and top to be performed even if the stack is empty For an empty stack, pop and top simply return null © 2014 Goodrich, Tamassia, Goldwasser Stacks 7

Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures © 2014 Goodrich, Tamassia, Goldwasser Stacks 8

Method Stack in the JVM The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k) ; } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5 © 2014 Goodrich, Tamassia, Goldwasser Stacks 9

Array-based Stack Algorithm size() return t + 1 Algorithm pop() if isEmpty() then return null else t  t  1 return S[t + 1] A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element … S 1 2 t © 2014 Goodrich, Tamassia, Goldwasser Stacks 10

Array-based Stack (cont.) The array storing the stack elements may become full A push operation will then throw a FullStackException Limitation of the array- based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t = S.length  1 then throw IllegalStateException else t  t + 1 S[t]  o S 1 2 t … © 2014 Goodrich, Tamassia, Goldwasser Stacks 11

Performance and Limitations Let n be the number of elements in the stack The space used is O(n) Each operation runs in time O(1) Limitations The maximum size of the stack must be defined a priori and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception © 2014 Goodrich, Tamassia, Goldwasser Stacks 12

Array-based Stack in Java public class ArrayStack<E> implements Stack<E> { // holds the stack elements private E[ ] S; // index to top element private int top = -1; // constructor public ArrayStack(int capacity) { S = (E[ ]) new Object[capacity]); } public E pop() { if isEmpty() return null; E temp = S[top]; // facilitate garbage collection: S[top] = null; top = top – 1; return temp; } … (other methods of Stack interface) © 2014 Goodrich, Tamassia, Goldwasser Stacks 13

Example Use in Java public class Tester { // … other methods public intReverse(Integer a[]) { Stack<Integer> s; s = new ArrayStack<Integer>(); … (code to reverse array a) … } public floatReverse(Float f[]) { Stack<Float> s; s = new ArrayStack<Float>(); … (code to reverse array f) … } © 2014 Goodrich, Tamassia, Goldwasser Stacks 14

Stack A simple generic stack class, with the stack implemented using an ArrayList: package stackdemo; //http://cs.fit.edu/~ryan/java/programs/generic/GenericStack-java.html import java.util.*; public class GenericStack<T> { private ArrayList<T> stack = new ArrayList<T>(); private int top = 0; public int size() { return top; } public void push(T item) { stack.add(top++, item); public T pop() { return stack.remove(--top); What is the complexity of each operation? How would we implement peek?

package stackdemo; public class GenericStackDriver { public static void main(String[] args) { GenericStack<String> stringStack = new GenericStack<String>(); stringStack.push("Moe"); stringStack.push("Curly"); stringStack.push("Larry"); System.out.println(stringStack.pop()); GenericStack<Integer> integerStack = new GenericStack<Integer>(); integerStack.push(1); integerStack.push(2); System.out.println(integerStack.pop()); }

17 The JDK Stack Class The Stack class represents a last-in-first-out stack of objects. The elements are accessed only from the top of the stack. You can retrieve, insert, or remove an element from the top of the stack.

Parentheses Matching Each “(”, “{”, or “[” must be paired with a matching “)”, “}”, or “[” correct: ( )(( )){([( )])} correct: ((( )(( )){([( )])} incorrect: )(( )){([( )])} incorrect: ({[ ])} incorrect: ( © 2014 Goodrich, Tamassia, Goldwasser Stacks 18

Parenthesis Matching (Java) public static boolean isMatched(String expression) { final String opening = "({["; // opening delimiters final String closing = ")}]"; // respective closing delimiters Stack<Character> buffer = new LinkedStack<>( ); for (char c : expression.toCharArray( )) { if (opening.indexOf(c) != −1) // this is a left delimiter buffer.push(c); else if (closing.indexOf(c) != −1) { // this is a right delimiter if (buffer.isEmpty( )) // nothing to match with return false; if (closing.indexOf(c) != opening.indexOf(buffer.pop( ))) return false; // mismatched delimiter } return buffer.isEmpty( ); // were all opening delimiters matched? © 2014 Goodrich, Tamassia, Goldwasser Stacks 19

20 Queues A queue is a first-in/first-out data structure. Elements are appended to the end of the queue and removed from the beginning. The name queue suggests a group of people waiting in line. Elements are added (offered) at the back of the queue In a standard queue, elements are removed (polled or dequeued) from the beginning of the queue. In a priority queue, elements are assigned priorities. When accessing elements, the element with the most urgency is removed first.

Applications of Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures © 2014 Goodrich, Tamassia, Goldwasser Queues 21

The Queue ADT Auxiliary queue operations: Boundary cases: Queues 4/23/2019 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: object first(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Boundary cases: Attempting the execution of dequeue or first on an empty queue returns null © 2014 Goodrich, Tamassia, Goldwasser Queues 22 22

Example Operation Output Q enqueue(5) – (5) enqueue(3) – (5, 3) dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) first() 7 (7) dequeue() 7 () dequeue() null () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() 9 (7, 3, 5) © 2014 Goodrich, Tamassia, Goldwasser Queues 23

wrapped-around configuration Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and size f index of the front element sz number of stored elements Recall that we remove elements from the front. When the queue has fewer than N elements, array location r = (f + sz) mod N is the first empty slot past the rear of the queue normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues 24

Queue Operations We use the modulo operator (remainder of division) Algorithm size() return sz Algorithm isEmpty() return (sz == 0) Q 1 2 r f Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues 25

Queue Operations (cont.) Operation enqueue throws an exception if the array is full This exception is implementation- dependent Algorithm enqueue(o) if size() = N  1 then throw IllegalStateException else r  (f + sz) mod N Q[r]  o sz  (sz + 1) Q 1 2 r f Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues 26

Queue Operations (cont.) Note that operation dequeue returns null if the queue is empty Algorithm dequeue() if isEmpty() then return null else o  Q[f] f  (f + 1) mod N sz  (sz - 1) return o Q 1 2 r f Q 1 2 f r © 2014 Goodrich, Tamassia, Goldwasser Queues 27

Queue Interface in Java Java interface corresponding to our Queue ADT Assumes that first() and dequeue() return null if queue is empty public interface Queue<E> { int size(); boolean isEmpty(); E first(); void enqueue(E e); E dequeue(); } © 2014 Goodrich, Tamassia, Goldwasser Queues 28

Array-based Implementation © 2014 Goodrich, Tamassia, Goldwasser Queues 29

Array-based Implementation (2) © 2014 Goodrich, Tamassia, Goldwasser Queues 30

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) Queue Dequeue Enqueue Shared Service © 2014 Goodrich, Tamassia, Goldwasser Queues 31

32 The Queue Interface

Queue Implemented With LinkedList 33 Queue Implemented With LinkedList LinkedList implements the Queue interface, so you can use the queue methods with a LL. Declare your reference variable as a Queue to prevent confusing results from using non-queue methods.

34 Queue Suppose eight children will take turns on two swings. At each round, the next child in the queue gets swing # (round % 2). public static void main(String[] args) throws InterruptedException { int[] swings = { 1, 2, 3 }; Queue<String> queue = new LinkedList<String>(); queue.offer("Brian"); queue.offer("Diana"); queue.offer("Al"); queue.offer("Mary"); queue.offer("Mike"); queue.offer("Florence"); queue.offer("Carl"); queue.offer("Dennis"); int round = 0; while (!queue.isEmpty()) { System.out.println(queue.remove() + " uses swing # " + round % swings.length); Thread.sleep(500); round++; }