CHAPTER 7 Queues.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Advertisements

Ceng-112 Data Structures I Chapter 5 Queues.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
Topic 9 The Queue ADT.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
CS Data Structures II Review COSC 2006 April 14, 2017
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Chapter 3: Abstract Data Types Queues Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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.
Stacks, Queues, and Deques
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 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
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,
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stacks And Queues Chapter 18.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Chapter 7 Queues Introduction Queue applications Implementations.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
The Queue ADT.
Linked Data Structures
Review Array Array Elements Accessing array elements
CC 215 Data Structures Queue ADT
Queue.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks, Queues, and Deques
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
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,
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Data Structures & Programming
Presentation transcript:

CHAPTER 7 Queues

Queues A queue is a linear collection whose elements are added on one end and removed from another Elements are removed in the same order they arrive A queue is FIFO – first in, first out

A conceptual view of a queue

Basic operations Enqueue: Dequeue: store a data item at the rear end of the queue make rear to be the new end of the queue Dequeue: retrieve and remove a data item from the front of the queue make front to be the element that was after the removed element

Example enqueue Item 1 enqueue Item 2 enqueue Item 3 dequeue

More operations on queues

ADT definition of Queue Notation: Q queue e item of same type as the elements of Q b boolean value

Operations InitQueue(Q) Procedure to initialize Q to an empty queue Preconditions: none Postconditions: Q empty

Operations Enqueue(Q,e) Dequeue(Q)  e size of Q decreased by 1 Procedure to place an item e into Q Preconditions: Q not full Postconditions: size of Q increased by 1 Dequeue(Q)  e Procedure to remove and return the front item in Q if Q is not empty Preconditions: Q not empty Postconditions: front element removed, size of Q decreased by 1

Operations first(Q)  e Procedure to return (without removing) the front item in Q if Q is not empty Preconditions: Q not empty Postconditions: Q not changed

Operations IsEmpty(Q)  b Boolean function that returns TRUE if Q is empty Preconditions: none Postconditions: Q not changed

Queue AXIOMS q.InitQueue().IsEmpty() = true q.MakeEmpty().IsEmpty() = true Note: MakeEmpty is not listed in the textbook q.Enqueue(g).IsEmpty() = false q.First() = q

Queue applications Wait line simulations Radix sorting Breadth-first search in a tree/graph

Linked implementation Array implementation Queue implementation The interface class Linked implementation Array implementation

The interface class public interface QueueADT<T> { // Adds one element to the rear of this queue public void enqueue (T element); // Removes and returns the front element from this queue public T dequeue(); // Returns without removing the front element of this queue public T first(); // Returns true if this queue contains no elements public boolean isEmpty(); // Returns the number of elements in this stack public int size(); // Returns a string representation of this queue public String toString(); }

Linked implementation Internally, a queue is represented as a linked list of nodes, with a reference to the front of the queue, a reference to the rear end of the queue, and an integer count of the number of nodes in the queue LinearNode class is reused to define the nodes

Linked implementation

Linked Implementation: Enqueue Create a new node If queue is empty Make front equal to the new node Make rear equal to the new node Otherwise Attach the new node to the rear end of the queue Make rear to be the new node Increment count

Linked Implementation: Enqueue public void enqueue (T element) { LinearNode<T> node = new LinearNode<T>(element); if (isEmpty()) front = node; else rear.setNext (node); rear = node; count++; }

Linked Implementation: Dequeue Get the contents of the front node Make front to be the node after the first node, or null if this was the only node Decrement count If queue is empty Make rear equal to null Return the contents of the retrieved node

Linked Implementation: Dequeue public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = front.getElement(); front = front.getNext(); count--; rear = null; return result; }

Array implementation A queue can be managed using an array in which index 0 represents one end An integer value rear represents the next open slot in the array and the number of elements currently in the queue The challenge with this approach is that a queue operates on both ends, so the elements in the array must be shifted to keep one end at index 0

Array implementation Not efficient

Circular Array Implementation If we don't fix one end of the queue at index 0, we won't have to shift the elements A circular queue is an implementation of a queue using an array that conceptually loops around on itself

Circular arrays

A queue straddling the end of a circular array

Changes in a circular array implementation of a queue

Enqueue in circular array When an element is enqueued, the value of rear is incremented But it must take into account the need to loop back to 0: rear = (rear+1) % queue.length; Note that this array implementation can also reach capacity and may need enlarging

Dequeue in Circular Array When an element is dequeued, the value of front is incremented But it must take into account the need to loop back to 0: front = (front+1) % queue.length; The queue is empty when front becomes equal to rear

Complexity of the queue operations The enqueue operation is O(1) for all implementations The dequeue operation is O(1) for linked and circular array implementations, but O(n) for the noncircular array version

Examples 1a Problem 1: AppendQueue(Q,P): A procedure to append a queue P onto the end of a queue Q, leaving P empty. Pre: queue P and queue Q, initialized (possibly empty) Post: Q contains all elements originally in Q, followed by the elements that were in P in same order. P is empty.

Examples 1b Algorithm: while not isEmpty(P) e  dequeue(P) enqueue(Q,e) Complexity of the algorithm: O(N), N - the number of elements in P.

Examples 2a Problem 2: ReverseQueue(Q): A procedure to reverse the elements in a queue Q, using a stack Pre: queue Q, initialized (possibly empty) Post: Q contains all elements re-written in reverse order

Examples 2b Algorithm: Create a new stack S while not isEmpty(Q) push(S, dequeue(Q)) while not isEmpty(S) enqueue(Q,pop(S))