1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority 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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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 CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
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.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queue data structure.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues.
Chapter 19: Stacks and Queues.
Queues.
CSC 143 Queues [Chapter 7].
Stacks and Queues CSE 373 Data Structures.
Stacks and Queues 1.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CE 221 Data Structures and Algorithms
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Data Structures – Week #4
Data Structures & Programming
Presentation transcript:

1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from the other end called the front of the queue Alternatively, in a queue the element deleted is the one that stayed in the queue the longest. This is also called first-in-first-out (FIFO) Classical example of queues is a queue of people waiting to pay bills.

2 Queue Concept and Example A queue has a Front and a Rear The insert operation is often called Enqueue The delete operation is often called Dequeue Front of the queue Next person to be served Rear of the queue Next person will join the queue from the rear Queue of people waiting to pay bills

3 Queue ADT A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from the other end called the front of the queue Common queue operations: –Enqueue(item) – Add the item to the end of the Q –Dequeue() – Remove & return the item at the front –isEmpty() – Return true if the Q is empty –isFull() – Return true if the Q is full

4 How do we implement queue ADT? 2 ways to implement a queue –Using an array –Using a linked list

5 Array Implementation of Queues: Operations Use an array int Q[N] front points to the front element of the queue, i.e., holds the index of the array Q that contains the front of the queue rear points to next slot after the last element in the queue noItems contains the current number of items in the queue. An empty queue is one where noItems = 0 A full queue is one where noItems = N

6 Array Implementation of Queues We can implement a queue of at most “N” elements with an array int Q[N] and 3 variables: int front, int rear, int noItems as follows Q front = 3 rear = 3 An Empty Queue Front and rear are equal to each other and noItems = 0 in an empty Queue noItems = Q front = rear = 6 A partially- full Queue Front points to the first element in the Queue Rear points to the next slot after the last element in the Queue 9 noItems = Q front = rear = 3 A Full Queue Front and rear are equal to each other and noItems = N=7 in a full Queue 9 noItems =

7 Array Implementation of Queues Q front = rear = 1 After 20 is inserted Q front = rear = 6 Initial Queue Q front = rear = 0 4 After 4 is inserted Q front = rear = Rear = 1 Front = 4 Conceptual view of the final queue: circular array noItems = 3 noItems = 4 noItems = 5 After 15 is removed noItems = 4

8 Queue Using Arrays: Declarations & Operations class Queue { private: static int N = 100; // size of the queue int Q[]; // Array holding the queue elements (ints) int front; // front of the queue int rear; // rear of the queue int noItems; // # of items in the queue public: Queue(); bool isEmpty(); bool isFull(); int Enqueue(int item); int Dequeue(); };

9 Queue Operations: Constructor, isEmpty, isFull // Constructor Queue(){ Q = new int[N]; front = rear = noOfItems = 0; } //end-Queue // Returns true if the Q is empty bool isEmpty(){ return noOfItems == 0; } //end-isEmpty // Returns true if the Q is full bool isFull(){ return noOfItems == N; } //end-isFull

10 Queue Operations: Enqueue // Inserts a new item into the queue // Returns 0 on success, -1 on failure int Enqueue(int newItem){ if (isFull()){ println(“Queue is full”); return -1; } //end-if Q[rear] = newItem; // Put the new item at the end rear++; if (rear == N) rear = 0; // Now move rear noItems++; // One more item in the queue return 0; } //end-EnQueue

11 Queue Operations: Dequeue // Removes and returns the item at the front of the queue // If the queue is empty, returns -1 (an error) int Dequeue(){ int idx = -1; if (isEmpty()){ println(“Queue is empty”); return -1; } //end-if idx = front; // This is where the first item is. front++; if (front == N) front = 0; // Move front noItems--; // One less item in the Queue return Q[idx]; // Return the item } //end-Dequeue

12 Queue Usage Example main(){ Queue q = new Queue(); if (q.isEmpty()) println(“Queue is empty”); // Q empty now q.Enqueue(49); q.Enqueue(23); println(“Front of the Q is: ” + q.Dequeue()); // prints 49 q.Enqueue(44); q.Enqueue(22); println(“Front of the Q is: ” + q.Dequeue()); // prints 23 println(“Front of the Q is: ” + q.Dequeue()); // prints 44 println(“Front of the Q is: ” + q.Dequeue()); // prints 22. println(“Front of the Q is: ” + q.Dequeue()); // prints -1 if (q.isEmpty()) println(“Queue is empty”); // Q empty now } //end-main

13 Array Implementation of Queues: Last Word We can implement a queue of at most “N-1” elements with an array int Q[N] and 2 variables: int front, int rear Think about how you would define –An empty queue –A full queue Q front = rear = 2 A Full Queue Q front = 3 rear = 3 An Empty Queue Front and rear are equal to each other in an empty queue There is one empty space between rear and front in a full queue

Linked-List implementation of Queues Initial Queue Enqueue(3) Front Rear Dequeue() Front 3 Rear After 3 is enqueued Dequeue() Front 3 Rear After 9 is dequeued 15 6 Front 3 Rear After 2 is dequeued

15 Queue using Linked List: Declarations & Operations struct QueueNode { int item; QueueNode next; QueueNode(int e){item=e; next=null;} }; class Queue{ private: QueueNode front; // Ptr to the front of the Q QueueNode rear; // Ptr to the rear of the Q public: Queue(); bool isEmpty(); void Enqueue(int item); int Dequeue(); };

16 Queue Operations: Constructor, isEmpty // Constructor Queue(){ front = rear = null; } //end-Queue // Returns true if the Q is empty bool isEmpty(){ return front == null; } //end-isEmpty

17 Queue Operations: Enqueue // Inserts a new item into the queue void Enqueue(int newItem){ // Allocate a QueueNode for the item QueueNode node = new QueueNode(newItem); if (front == NULL){ front = rear = node; } else { rear.next = node; rear = node; } //end-else } //end-EnQueue

18 Queue Operations: Dequeue // Removes and returns the item at the front of the queue // If the queue is empty, returns -1 (an error) int Dequeue(){ if (isEmpty()){ println(“Queue is empty”); return -1; } //end-if QueueNode tmp = front; // Keep a ptr to the front node front = front.next; // Remove the front node if (front == null) rear = NULL; // Empty Q? // Return the item return tmp.item; } //end-Dequeue

19 Queue Usage Example main(){ Queue q = new Queue(); if (q.isEmpty()) println(“Queue is empty”); // Q empty now q.Enqueue(49); q.Enqueue(23); println(“Front of the Q is: ” + q.Dequeue()); // prints 49 q.Enqueue(44); q.Enqueue(22); println(“Front of the Q is: ” + q.Dequeue()); // prints 23 println(“Front of the Q is: ” + q.Dequeue()); // prints 44 println(“Front of the Q is: ” + q.Dequeue()); // prints 22. println(“Front of the Q is: ” + q.Dequeue()); // prints -1 if (q.isEmpty()) println(“Queue is empty”); // Q empty now } //end-main

20 Applications of Queues File servers: Users needing access to their files on a shared file server machine are given access on a FIFO basis Printer Queue: Jobs submitted to a printer are printed in order of arrival Phone calls made to customer service hotlines are usually placed in a queue Expected wait-time of real-life queues such as customers on phone lines may be too hard to solve analytically use queue for simulating real-life queues