Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Similar documents
csci 210: Data Structures Stacks and Queues

Linked Structures. See Section 3.2 of the text.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Lecture 3: Stacks & Queues

8. Fundamental Data Structures

CSC212:Data Structure

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

infix expressions (review)

Stacks and Queues. David Greenstein Monta Vista

Interfaces & Generics

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

.:: UNIT 4 ::. STACK AND QUEUE

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Your Topic Goes Here Queue

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

Data Structures and Algorithms

CS24 Week 4 Lecture 2

Lecture 2: Stacks and Queues

Data Abstraction and Specification of ADTs

Queues Fall 2018 Margaret Reid-Miller

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Summer Final Exam Review Session August 5, 2009


COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

LIFO : Last In First Out

Abstract Data Types. Abstract Data Types

CMSC 132: Object-Oriented Programming II. Stack and Queue

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CSC 1052 Algorithms & Data Structures II: Queues

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

DNHI Homework 3 Solutions List, Stacs and Queues

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

More Data Structures (Part 1) Stacks

CS 206 Introduction to Computer Science II

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Introduction to Software Design

Standard ADTs. Lecture 19 CS2110 Summer 2009

1. Introduction. Lecture Content

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

CS2012 Programming Techniques II

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

1. Stack Implementation Using 1D Array

Basic Data Structures

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

[2:3] Linked Lists, Stacks, Queues

Stack and Queue. Stack:

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Computer Science 62. Midterm Examination

Basic Data Structures 1 / 24

Chapter 18: Stacks And Queues

Data Structure. Recitation VII

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Stacks Goodrich, Tamassia, Goldwasser Stacks

CSC 1052 Algorithms & Data Structures II: Linked Queues

Stacks and Queues. CSE 373 Data Structures Lecture 6

STACKS AND QUEUES. Problem Solving with Computers-II

CE204 Data Structures and Algorithms Part 2

Chapter 2. Stack & Queues. M hiwa ahmad aziz

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

15. Stacks and Queues

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

CS171 Midterm Exam. October 29, Name:

Stacks and Queues

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Stacks and queues (chapters 6.6, 15.1, 15.5)

Lecture Data Structure Stack

PA3 Design Specification

COMP 250 Winter stacks Feb. 2, 2016

Chapter 18: Stacks And Queues

DS L9: Queues

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Week 6. Data structures

CSC 321: Data Structures. Fall 2012

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Laboratorio di Algoritmi e Strutture Dati

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS2113 Lab: Collections 10/29/2018

CSC148 Week 2. Larry Zhang

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Queues. Stacks and Queues

Building Java Programs

Week 4 Stacks & Queues

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Stacks and Queues. CSE Data Structures April 12, 2002

Transcription:

Keeping Order:! Stacks, Queues, & Deques 1

Stacks 2

Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek() return the top item but don t remove it isempty() return True iff the stack is empty java.util.stack is similar to our stack implementation but more fully featured The textbook s version of Stack is similar but has different naming. 3 3

Stacks: Why? Reversing strings/lists is easy with stacks! push everything on in order then pop them all off. Depth-first search (DFS) later Maze and graph searching Thank back to lab 1! :) Matching parenthesis, braces, open/close tags for HTML, etc. think about how java expects matching { } and ( ) 4 4

Demo: MatchParens.java Find open paren push() Find close paren check top of stack for friend (a.k.a. partner) 5 5

Implementing Stacks Q: How do we implement a stack? Simple option: fixed-size array. instance variables (int[] stack and int top) push() top++ stack[top] = pop() peek() top-- Pros: fast (operations are O(1)); space efficient Cons: array can fill up stack[8] 7 6 5 4 3 2 1 0 top -1 6 6

Implementing Stacks: ArrayList Better option: dynamic array. instance variables (ArrayLists<T> stack) push() use add() to add to end pop() use remove() to remove from end Pros: ArrayList never becomes full ; no need to keep track of top since ArrayList does this for us! Cons: push() could be non-constant time op. when the ArrayList has to grow to make more room. 7 7

Implementing Stacks: SLL Great option: Singly-Linked List. top instance variables (Element<T> top) top of the stack is the head of the list push() adds item to front of list pop() removes item from front of list Pros: SLL never becomes full ; all operations are O(1) Cons: need extra space for links in list but there is never wasted space for unused spaces in array/arraylist. / 8 8

Queues 9

Queues A queue is a first in, first out (FIFO) data structure Primary Operations: enqueue() add at rear of the queue dequeue() remove and return the first item in the queue front() return the first item but don t remove it isempty() return True iff the queue is empty Java has a Queue interface but it uses non-conventional names (add()/offer(), element(), peek(), remove()/poll()). The 2 different alternatives for enqueue()/dequeue() are for one that uses exceptions to handle failed operations while the other doesn t 10 10

Queues: Why? Useful for simulations of lines (banks, toll booths, etc.) Useful for computer print queues submit a document to be printed enqueue() print the document when it reaches the front dequeue() Round-robin scheduling for threads/processes to run dequeue() process run for fixed period of time or until it blocks enqueue() processes in the order they arrive to ensure fair sharing of CPU processes that finish don t get enqueued again Used when searching (similar to stack) 11 11

Implementing Queues See: SLLQueue.java Typical option: Linked Lists. instance variables (Element<T> head, tail) front of the queue is the head of the list; tail is the back. enqueue() adds item to back of list dequeue() removes item from front of list Pros: Never becomes full ; all operations are Θ(1). Cons: need extra space for links in list but there is never wasted space for unused spaces in array/arraylist. tail head / 12 12

Implementing Queues Basic setup + front() & isempty() public class SLLQueue<T> implements SimpleQueue<T> { private Element<T> head; // front of the linked list private Element<T> tail; // tail of the linked list!!! }! public SLLQueue() { head = null; tail = null; } //... public T front() throws Exception { if (isempty()) throw new Exception("empty queue"); return head.data; } public boolean isempty() { return head == null; } tail head / 13 13

Implementing Queues enqueue()! public void enqueue(t item) { if (isempty()) { // first item head = new Element(item); tail = head; } else { tail.next = new Element(item); tail = tail.next; } } dequeue() public T dequeue() throws Exception { if (isempty()) throw new Exception("empty queue"); T item = head.data; head = head.next; return item; } tail head / 14 14

More on Queues 15

More on Queues There are many implementations of a Queue in Java ArrayBlockingQueue, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, and LinkedList LinkedList is probably the most common 16 16

Blocking Queues A blocking queue is a bounded buffer used for interprocess communication (IPC). Think back to Producer/Consumer we used a message box with 1 item capacity with a queue, we could hold more items! Producer calls enqueue(); sleep if not enough space Consumer calls dequeue(); sleep if nothing to consume. Many ways to implement blocking queues Producer Consumer enqueue() dequeue() 17 17

Blocking Queues Why not an array? Array-based queues might seem an odd choice Why not use an ArrayList? enqueue at end, dequeue from front dequeue then requires us to shift all items left (forward) enqueue at front, dequeue at end enqueue then requires us to shift all items right (back) In array, keep track of indices, f and r (front and rear). For efficiency, when dequeuing at front, leave space empty, and remember that the front of the queue is now actually one space back 0 1 2 3 4 5 6 7 8 9 f r 18 18

Blocking Queues 0 1 2 3 4 5 6 7 8 9 a f r 19 19

Blocking Queues 0 1 2 3 4 5 6 7 8 9 a b f r 20 20

Blocking Queues Skip ahead a bit 0 1 2 3 4 5 6 7 8 9 a b c f r 21 21

Blocking Queues Now maybe some dequeue() calls are made 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i f r 22 22

Blocking Queues Now maybe some dequeue() calls are made 0 1 2 3 4 5 6 7 8 9 b c d e f g h i f r 23 23

Blocking Queues Now maybe some dequeue() calls are made 0 1 2 3 4 5 6 7 8 9 c d e f g h i f r 24 24

Blocking Queues Now maybe some dequeue() calls are made 0 1 2 3 4 5 6 7 8 9 d e f g h i f r 25 25

Blocking Queues Q: What happens when we reach the end of the array and want to enqueue()?! stuck? no! There is likely free space at the front of the array wrap around! 0 1 2 3 4 5 6 7 8 9 e f g h i f r 26 26

Blocking Queues If N is the size of the array, wrap by computing r to be: r = (r+1) % N Same logic works for wrapping f. If r < f this indicates that the queue has wrapped around. (do a couple more enqueue() ops going into next slide) 0 1 2 3 4 5 6 7 8 9 e f g h i j r f 27 27

Blocking Queues Book shows more details about how to implement this. Notice that: f holds index of the first item in the queue r contains the index of the space beyond the last item in the queue (i.e., where the next new item should be added) 0 1 2 3 4 5 6 7 8 9 k l e f g h i j r f 28 28

Blocking Queues Recall: queue is empty if: r == f empty 0 1 2 3 4 5 6 7 8 9 f r But this is actually the same for a full queue! Therefore we always need to leave one empty space OR keep track of the size 0 1 2 3 4 5 6 7 8 9 full k l m n e f g h i j f r 29 29

Deques 30

Deques Deque deck (double-ended queue) head Add/delete from either end Deque can be used as stack OR queue Implementation: DLL with head/tail pointers All operations are Θ(1) It is possible to use an array w/ wrapping to implement a deque (similar to how it is done w/ a queue) tail / 31 31