Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues Chapter 6 Chapter 6: Queues2 Chapter Objectives Learn how to represent a “waiting line”, i.e., a queue Learn how to use the methods in the Queue.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Elementary Data Structures
Week 4 - Monday CS221.
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.
Queues Queues Queues.
Wednesday, February 28, 2018 Announcements… For Today…
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
THURSDAY, OCTOBER 17 IN LAB
Stacks, Queues, and Deques
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 4 Queues.
Stacks, Queues, and Deques
Chapter 6 – Queues and Deques
Data Structures & Programming
Presentation transcript:

Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the Queue interface using  a single-linked list,  a circular array  double-linked list  Analyze the pros and cons for different Queue implementations  Become familiar with the Deque interface its methods  Simulate the operation of a physical system that has one or more waiting lines CS340 1

Queue The queue, like the stack, is a widely used data structure A queue differs from a stack in one important way A stack is LIFO list – Last-In, First-Out while a queue is FIFO list, First-In, First-Out CS340 2

Queue Abstract Data Type CS340 3

Queue Abstract Data Type  Line of customers waiting for service  Similar to a stack  We do not access the “middle” objects  Pointer in the head as in stack  Additional pointer on tail CS340 4

More Queues  Operating systems use queues to  keep track of tasks  ensure that the tasks are carried out in the order they were generated  Print queue CS340 5

Specification for a Queue Interface  The Queue interface implements the Collection interface CS340 6

Class LinkedList Implements the Queue Interface  LinkedList class implements the Queue interface Queue names = new LinkedList (); CS340 7

Implementing the Queue Interface CS340 8

Using a Double-Linked List to Implement the Queue Interface  Insertion and removal from either end of a double-linked list is ???  Problem: Other LinkedList methods in addition to the ones required and permitted by the Queue interface  Solution: Create a new class with a LinkedList component CS340 9

Using a Single-Linked List to Implement a Queue Insertions are at the rear of a queue and removals are from the front We need a reference to the last list node so that insertions can be performed at O(1) The number of elements in the queue is changed by methods insert and remove CS340 10

Implementing a Queue Using an Array  Single- or double-linked list is time efficient BUT  Space inefficiencies  Storage space increased due to references stored in the nodes  Array Implementation  Insertion at rear of array is ??  Removal from the front is ??  Removal from rear of array is ??  Insertion at the front is ?? CS340 11

[ 0 ] An array of integers to implement a queue of integers 486 We don't care what's in this part of the array. [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 Implementing a Queue Using an Array CS340 12

dequeue() [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 4 [ 0 ] 86 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 2 first 0 last 1 Implementing a Queue Using an Array CS340 13

dequeue() [ 0 ] 86 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 4 [ 0 ] 6 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 2 first 0 last 1 8 Implementing a Queue Using an Array CS dequeue() takes O(n)!

dequeue() size 3 first 0 last 2 [ 0 ] 4 [ 1 ] 8 [ 2 ] 6 [ 3 ] [ 4 ] Solution: Cyclic Array CS340 15

size 2 first 1 last 2 [ 0 ] [ 1 ] 8 [ 2 ] 6 [ 3 ] [ 4 ] enqueue(2) dequeue() enqueue(12) enqueue(5 ) Cyclic Array (cont.) CS340 16

size 3 first 3 last 0 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] Cyclic Array (cont.) CS340 17

Implementing a Queue Using a Circular Array (cont.) 18

Implementing a Queue Using a Circular Array (cont.) CS340 19

Implementing a Queue Using a Circular Array (cont.) CS size = 0 front = 0 rear = 4 public ArrayQueue(int initCapacity) { capacity = initCapacity; theData = (E[])new Object[capacity]; front = 0; rear = capacity – 1; size = 0; } ArrayQueue q = new ArrayQueue(5); capacity = 5

Implementing a Queue Using a Circular Array (cont.) CS size = 0 front = 0 rear = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('*'); capacity = 5 1 rear = 0 *

Implementing a Queue Using a Circular Array (cont.) CS size = 1 front = 0 rear = 1 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('+'); capacity = 5 2 rear = 0 * +

Implementing a Queue Using a Circular Array (cont.) CS size = 2 front = 0 rear = 1 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('/'); capacity = 5 3 * + rear = 2 /

Implementing a Queue Using a Circular Array (cont.) CS size = 3 front = 0 rear = 3 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('-'); capacity = 5 4 * + rear = 2 / -

Implementing a Queue Using a Circular Array (cont.) CS size = 4 front = 0 rear = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('A'); capacity = 5 5 * + rear = 3 / A -

CS size = 5 front = 0 public E poll() { if (size == 0) { return null } E result = theData[front]; front = (front + 1) % capacity; size--; return result; } next = q.poll(); capacity = 5 4 * + / - result = '*' front = 1 A rear = 4 Implementing a Queue Using a Circular Array (cont.)

CS size = 4 front = 1 public E poll() { if (size == 0) { return null } E result = theData[front]; front = (front + 1) % capacity; size--; return result; } next = q.poll(); capacity = 5 3 * + / - result = '+' front = 2 A rear = 4 Implementing a Queue Using a Circular Array (cont.)

CS size = 3 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('B'); capacity = 5 4 * + / - front = 2 A rear = 4 rear = 0 B Implementing a Queue Using a Circular Array (cont.)

CS size = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('C'); capacity = 5 5 B + / - front = 2 A rear = 0 rear = 1 C Implementing a Queue Using a Circular Array (cont.)

CS size = 5 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C Implementing a Queue Using a Circular Array (cont.)

CS size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C B + / - front = 2 A rear = 1 C newCapacity = 10 theData Implementing a Queue Using a Circular Array (cont.)

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 0 newData theData

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 0 / / j = 3 i = 1 newData theData

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 3 i = j = 4 i = 2 / newData theData

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 0 i = 2 A A j = 4 i = 3 / - newData theData

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 1 i = 3 B B j = 0 i = 4 / - A newData theData

CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 4 C C j = 1 i = 5 / - A B newData theData

newData CS Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 front = 2 rear = 1 newCapacity = 10 C i = 5 / - A B B + / - A C j = 2 C theData front = 0 rear = 4 10

CS Implementing a Queue Using a Circular Array (cont.) size = 5 q.offer('D'); capacity = 5 C / - A B newData front = 0 rear = 4 10 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } 6 rear = 5 D

Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; public boolean hasNext() { return count < size; }.... CS Just as for class ListQueue, we must implement the missing: Queue methods class Iter

Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; public boolean hasNext() { return count < size; }.... CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface index stores the subscript of the next element to be accessed

Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; public boolean hasNext() { return count < size; }.... CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface The constructor initializes index to front when a new Iter object is created

Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; public boolean hasNext() { return count < size; }.... CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface count keeps track of the number of items accessed so far

Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; public boolean hasNext() { return count < size; }.... CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface hasNext() returns true if count is less than size

Implementing Class ArrayQueue.Iter public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E returnValue = theData[index]; index = (index + 1) % capacity; count+; return returnValue; public void remove { throw new UnsupportedOperationException(); } CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface next() returns the element at position index and increments Iter 's fields index and count

Implementing Class ArrayQueue.Iter public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E returnValue = theData[index]; index = (index + 1) % capacity; count+; return returnValue; public void remove { throw new UnsupportedOperationException(); } CS Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface remove() throws an exception because removing an item other than the first item violates the queue's contract

Comparing the Three Implementations Computation time Comparable in terms of computation time All operations are O(1) Although reallocating an array is O(n), its is amortized over n items, so the cost per item is O(1) CS340 47

Comparing the Three Implementations (cont.)  Storage  Linked-list implementations require more storage due to the extra space required for the links  A double-linked list requires 1.5 times the storage of a single- linked list  A circular array requires half the storage of a single-linked list to store the same number of elements  But a recently reallocated circular array is half empty CS340 48

The Deque Interface Section 4.4 CS340 49

Deque Interface Deque: double-ended queue Allows insertions and removals from both ends The Java Collections Framework provides two implementations of the Deque interface ArrayDeque LinkedList CS340 50

Deque Example CS340 51

Deque Interface (cont.) The Deque interface can be used as a Queue Stack 52

Simulating Waiting Lines Using Queues CS340 53

Simulating Waiting Lines Using Queues  Simulation is used to study the performance of a physical system by using  a physical,  mathematical, or  computer model of the system CS340 54

Simulating Waiting Lines Using Queues (cont.) A branch of mathematics called queuing theory studies such problems CS340 55

Simulating Google webserver You are working for google and need to estimate how many servers you need for the new data center The servers receive requests with an exponential arrival process with average 1,000 requests/sec The servers serve with average rate 1,800 requests /sec You need to provision for peak and low times. During peak the rate increases to 1,700 req/seq for duration1 hr Overnight the server falls to 500 rer/sec for duration 6 hrs Simulate 1000 requests on the server. CS340 56

Simulating Google webserver Find: The average waiting time of a request To decrease the waiting to half time, would it be better to use two servers or double the rate of the e- commerce server to 2x? How many servers do you need to have 0 sec delay? CS340 57