Queues. Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid. Java: Queues / 1

Similar documents
Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

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

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

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

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

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

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

Data Structures Lecture 5

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

Unit 4 Basic Collections

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CSC212:Data Structure

INFO0902 Data Structures and Algorithms

csci 210: Data Structures Stacks and Queues

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

Basic Data Structures

DS L9: Queues

Basic Data Structures 1 / 24

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

Lecture 3 Linear Data Structures

CSC 273 Data Structures

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Lecture 3 Linear Data Structures

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

COMP 250 Midterm #2 March 11 th 2013

Week 11. Abstract Data Types. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

PA3 Design Specification

All the above operations should be preferably implemented in O(1) time. End of the Queue. Front of the Queue. End Enqueue

Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

A queue is a linear collection whose elements are added on one end and removed from the other

Computer Science 136 Exam 2

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

COMP9024: Data Structures and Algorithms

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Apply to be a Meiklejohn! tinyurl.com/meikapply

Announcements Queues. Queues

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

ITI Introduction to Computing II

Solutions to a few of the review problems:

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Dynamic Data Structures

if (Q.head = Q.tail + 1) or (Q.head = 1 and Q.tail = Q.length) then throw F ullqueueexception if (Q.head = Q.tail) then throw EmptyQueueException

1. Introduction. Lecture Content

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

CS/CE 2336 Computer Science II

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

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

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

Foundations of Data Structures

CS302 - Data Structures using C++

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

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

COMP 103. Data Structures and Algorithms

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

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.

ECE Fall st Midterm Examination (120 Minutes, closed book)

CMP-338 Solutions Midterm Spring Version 1


COS 226 Algorithms and Data Structures Fall Midterm

CSC 1052 Algorithms & Data Structures II: Linked Queues

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Ashish Gupta, Data JUET, Guna

Linear Data Structures

No Aids Allowed. Do not turn this page until you have received the signal to start.

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

Abstract Data Types - Lists Arrays implementation Linked-lists implementation

DataStruct 5. Stacks, Queues, and Deques

More Data Structures (Part 1) Stacks

THE UNIVERSITY OF WESTERN AUSTRALIA

CSE 143 SAMPLE MIDTERM

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

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

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

No Aids Allowed. Do not turn this page until you have received the signal to start.

Lists. ordered lists unordered lists indexed lists 9-3

Introduction to Data Structures. Introduction to Data Structures. Introduction to Data Structures. Data Structures

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.

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Ashish Gupta, Data JUET, Guna

Queues Fall 2018 Margaret Reid-Miller

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

ITI Introduction to Computing II

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

Project 2 (Deques and Randomized Queues)

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

CSE 214 Computer Science II Heaps and Priority Queues

1 P age DS & OOPS / UNIT II

CSCA48 Term Test 1 Seminar

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Data structure and algorithm in Python

Transcription:

Queues Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid cdk@it.uc3m.es Java: Queues / 1

Example The queue at the bus stop The printer queue cdk@it.uc3m.es Java: Queues / 2

Characteristics Linear structure Access on one end for insertion and on the other for extraction cdk@it.uc3m.es Java: Queues / 3

Queue interface public interface Queue { public int size(); public boolean isempty(); public void enqueue(object o) throws QueueOverflowException; public Object dequeue() throws EmptyQueueException; public Object front() throws EmptyQueueException; cdk@it.uc3m.es Java: Queues / 4

One interface and several implementations Queue ArrayQueue LinkedQueue cdk@it.uc3m.es Java: Queues / 5

Array-based implementation top tail 1 2 3 4 5 0 1 2 3 4 5 N-1 tail top 8 9 5 6 7 0 1 2 N-1 3 4 5 cdk@it.uc3m.es Java: Queues / 6

Implementation based on linked lists top tail Madrid Miami Munich cdk@it.uc3m.es Java: Queues / 7

Implementation based on linked lists public class LinkedQueue implements Queue { private Node top = null; private Node tail = null; private int size = 0; ( ) cdk@it.uc3m.es Java: Queues / 8

Insertion (enqueue) top tail n Madrid Miami Munich Moscow cdk@it.uc3m.es Java: Queues / 9

Implementation based on linked lists public void enqueue(object info) { Node n = new Node(info, null); if (top == null) top = n; else tail.setnext(n); tail = n; size++; cdk@it.uc3m.es Java: Queues / 10

Extraction (dequeue) top tail Madrid Miami Munich Moscow cdk@it.uc3m.es Java: Queues / 11

Implementation based on linked lists public Object dequeue() throws EmptyQueueException { Object info; if (top == null) throw new EmptyQueueException(); info = top.getinfo(); top = top.getnext(); if (top == null) tail = null; size--; return info; cdk@it.uc3m.es Java: Queues / 12

Activity View queue animations: http://courses.cs.vt.edu/csonline/datastr uctures/lessons/queuesimplementationview/ applet.html cdk@it.uc3m.es Java: Colas / 13

Activity Try the applet DepthBreadth.java that can be found here: http://www.faqs.org/docs/javap/c11/s3.html cdk@it.uc3m.es Java: Queues / 14

Other kinds of queues (not queues any more) Double-ended queues Priority queues cdk@it.uc3m.es Java: Queues / 15

Deques (Double-ended queues) insertfirst first last removelast removefirst insertlast cdk@it.uc3m.es Java: Queues / 16

Interface for deques public interface Deque { public int size(); public boolean isempty(); public void insertfirst(object info); public void insertlast(object info); cdk@it.uc3m.es Java: Queues / 17

Interface for deques public Object removefirst() throws EmptyDequeException; public Object removelast() throws EmptyDequeException; public Object first() throws EmptyDequeException; public Object last() throws EmptyDequeException; cdk@it.uc3m.es Java: Queues / 18

Stacks and queues as deques Stack Deque Queue Deque size() size() size() size() isempty() isempty() isempty() isempty() top() last() front() first() push(o) insertlast(o) enqueue(o) insertlast(o) pop() removelast() dequeue() removefirst() cdk@it.uc3m.es Java: Queues / 19

Definition of stacks from deques public class DequeStack implements Stack { private Deque deque; public DequeStack() { deque = new Deque(); public int size() { return deque.size(); public boolean isempty() { return deque.isempty(); cdk@it.uc3m.es Java: Queues / 20

Definition of stacks from deques public void push(object info) { deque.insertlast(info); public Object pop() throws EmptyStackException { try { return deque.removelast(); catch (EmptyDequeException ede) { throw new EmptyStackException(); cdk@it.uc3m.es Java: Queues / 21

Definition of stacks from deques public Object top() throws EmptyStackException { try { return deque.last(); catch (EmptyDequeException ede) { throw new EmptyStackException(); cdk@it.uc3m.es Java: Queues / 22

Implementation of deques based on lists Singly-linked lists are not appropriate because removelast requires the whole list to be traversed, in order to get the reference of the last-but-one node Solution: doubly-linked lists cdk@it.uc3m.es Java: Queues / 23

Doubly Linked Lists Linked lists in which each node has an additional reference pointing to the previous node in the list. Can be traversed both from the beginning to the end and vice-versa RemoveLast does not need the whole list to be traversed top info info info tail null prev next prev next prev next null 24

The DLNode class Public class DLNode { private Object info; private DLNode next; private DLNode prev; public DLNode(Object info) { public DLNode(Object info, DLNode prev, DLNode next) { public DLNode getnext() { public void setnext(dlnode next) { public DLNode getprev() { public void setprev(dlnode prev) { public Object getinfo() { public void setinfo(object info) { 25

Inserting a node top prev tail null node null null null DLNode node = new DLNode(data); 26

Inserting a node top prev tail null node null node.setprev(prev); if (prev!= null) { node.setnext(prev.getnext()); prev.setnext(node); 27

Inserting a node top prev tail null null node if (prev == null) { node.setnext(top); top = node; if (node.getnext()!= null) { node.getnext().setprev(node); else { tail = node; 28

Inserting a node /** * Inserts data after the prev node. If prev * is null, data is inserted at the first position of * the list. */ public void insert(dlnode prev, Object data) { DLNode node = new DLNode(data); node.setprev(prev); if (prev!= null) { node.setnext(prev.getnext()); prev.setnext(node); else { node.setnext(top); top = node; if (node.getnext()!= null) { node.getnext().setprev(node); else { tail = node; 29

Removing a node top removed data tail null null Object data = removed.getinfo(); 30

Removing a node top removed data tail null null if (removed.getprev()!= null) { removed.getprev().setnext(removed.getnext()); else { top = removed.getnext(); 31

Removing a node top removed data tail null null if (removed.getnext()!= null) { removed.getnext().setprev(removed.getprev()); else { tail = removed.getprev(); 32

Removing a node top data tail null null 33

Removing a node /** * Removes a node from the list and returns * the information it holds. */ public Object remove(dlnode removed) { Object data = removed.getinfo(); if (removed.getprev()!= null) { removed.getprev().setnext(removed.getnext()); else { top = removed.getnext(); if (removed.getnext()!= null) { removed.getnext().setprev(removed.getprev()); else { tail = removed.getprev(); return data; 34

Alternate implementation Checking that the previous and next nodes are not null makes the previous implementation complex and error-prone Possible simplification: Create two special nodes, without associated info, so that one is always at the beginning of the list and the other one is always at the end: An empty list contains only those two empty nodes. For insertions and removals, it is guaranteed that the previous and next nodes exists, so there is no need to check them. References top and tail do not need to be updated 35

Alternate implementation top tail null null null null null 36

Implementation based on lists public class DLDeque implements Deque { private DLNode top, tail; private int size; public DLDeque() { top = new DLNode(); tail = new DLNode(); tail.setprev(top); top.setnext(tail); size = 0; cdk@it.uc3m.es Java: Queues / 37

Insertion top second tail first Madrid Miami Munich Moscow cdk@it.uc3m.es Java: Queues / 38

Implementation based on lists public void insertfirst(object info) { DLNode second = top.getnext(); DLNode first = new DLNode(info, top, second); second.setprev(first); top.setnext(first); size++; cdk@it.uc3m.es Java: Queues / 39

Extraction top first second tail Moscow Madrid Miami Munich cdk@it.uc3m.es Java: Queues / 40

Implementation based on lists public Object removefirst() throws EmptyDequeException { if (top.getnext() == tail) throw new EmptyDequeException(); DLNode first = top.getnext(); Object info = first.getinfo(); DLNode second = first.getnext(); top.setnext(second); second.setprev(top); size--; return info; cdk@it.uc3m.es Java: Queues / 41

Activity Review how queues are implemented in http://java.sun.com/docs/books/tutorial /collections/interfaces/queue.html http://java.sun.com/javase/6/docs/api /java/util/queue.html cdk@it.uc3m.es Java: Queues / 42

Priority queue A priority queue is a linear data structure where elements are retuned according to a value associated to them (priority) (and not necessarily to the order of insertion). The priority might be the value of the element itself, but it might also differ from it. cdk@it.uc3m.es Java: Queues / 43

Interface for priority queues public interface PriorityQueue { public int size(); public boolean isempty(); public void insertitem(comparable priority, Object info); public Object minelem() throws EmptyPriorityQueueException; public Object removeminelem() throws EmptyPriorityQueueException; public Object minkey() throws EmptyPriorityQueueException; cdk@it.uc3m.es Java: Queues / 44

Example 3 2 4 1 5 3 2 4 1 5 2 1 3 + + + + + cdk@it.uc3m.es Java: Queues / 45

Example 3 2 4 1 5 3 2 4 1 5 2 1 3 min + + + + + cdk@it.uc3m.es Java: Queues / 46

Example insert in order 3 2 4 1 5 21 3 4 5 2 1 3 + + + + + cdk@it.uc3m.es Java: Queues / 47

Implementations With an unsorted sequence Easy insertion Comparison needed for extraction With a sorted sequence Comparison needed for insertion Easy extraction cdk@it.uc3m.es Java: Queues / 48

Activity Try http://www.akira.ruc.dk/~keld/algoritmik_e99/ Applets/Chap11/PriorityQ/PriorityQ.html cdk@it.uc3m.es Java: Queues / 49