1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.

Slides:



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

Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
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.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
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.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Stacks and Queues.
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 and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
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.
Queues CSI 1101 N. El Kadri. 2 Definitions A queue is a linear abstract data type such that insertions are made at one end, called the rear, and removals.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 7 Queues Introduction Queue applications Implementations.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
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.
CHAPTER 5 QUEUE v2 by Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
 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.
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.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
CSCE 210 Data Structures and Algorithms
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
Algorithms and Data Structures
Chapter 19: Stacks and Queues.
Queues.
Revised based on textbook author’s notes.
Queues.
Queues Definition of a Queue Examples of Queues
Queue.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Presentation transcript:

1 Queues

2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out counter Books on a book shelf Clients before ATM machine Pile of bath towels on linen closet shelf

3 Queue Is a linear data structure with removal of items at one end (front) and insertion of items at the opposite end (back) FIFO – first-in-first-out frontback

4 Queue Operations Enqueue(item) Insert item at back Dequeue() Remove item at front Clear() Make queue empty IsEmpty() Is queue empty? IsFull() Is queue full?

5 Queue Implementation (1)

6 Problem with Implementation (1) frontrear  With frequent queueing and dequeing, end of array can be reached quickly.  Can we somehow use the empty spaces?

7 Queue Implementation (2)

8 Suppose, currently, front = 5 rear = 9 For next values, front = (front + 1) mod MAX_SIZE = 6 Rear = (rear + 1) mod MAX_SIZE = 0 frontrear MAX_SIZE

9 queue.h (Declarations) #define MAX_SIZE 5 #define EMPTY -1 typedef char elemType; class Queue { public: Queue(); void enqueue(elemType item); elemType dequeue(); bool isEmpty(); bool isFull(); void clear(); void print();...

10 queue.h (Declarations)... private: elemType data[MAX_SIZE]; int front; int back; int count; // for isFull, isEmpty }; // Note ‘;’

11 Queue() (Constructor) Queue::Queue(){ front = 0; back = -1; count = 0; }

12 Enqueue() void Queue::enqueue(elemType item){ if (!isFull()){ back = (back + 1) % MAX_SIZE; data[back] = item; count++; }

13 Dequeue() elemType Queue::dequeue(){ int result = EMPTY; if (!isEmpty()){ result = data[front]; front = (front + 1) % MAX_SIZE; count--; } return result; }

14 Clear() void Queue::clear(){ front = 0; back = -1; count = 0; }

15 IsEmpty() bool Queue::isEmpty(){ return count == 0; }

16 Driver Program #include #include "queue.h" using namespace std; void showMenu(); int main(){ // Declare variables int choice; elemType item; Queue q; showMenu(); cin >> choice;...

17 Driver Program while (choice != 0){ switch (choice){ case 1: cout << "Enter item." << endl; cin >> item; q.enqueue(item); cout << "Queue Contents:"<< endl; q.print(); cout << endl; break; case 2: item = q.dequeue(); cout << item << " was dequeued." << endl;...

18 Driver Program cout << "Queue Contents:"<< endl; q.print(); cout << endl; break; case 3: q.clear(); cout << "Queue Contents:"<< endl; q.print(); cout << endl; } showMenu(); cin >> choice; } return 0; }

19 showMenu() Function void showMenu(){ cout << " \n" << "1. Enqueue\n" << "2. Dequeue\n" << "3. Clear\n" << "0. Quit\n" << " \n" << "Please enter your choice." << endl; }

20 Your Turn Write the code to implement Queue::print(), which displays the contents of a queue, from front to back. Hint: consider two cases 10 front rear MAX_SIZE frontrear