Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
CHAPTER 7 Queues.
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.
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.
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.
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.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
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,
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Structures Using C++
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
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.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
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.
CSCE 210 Data Structures and Algorithms
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Data Structures Using C++ 2E
CS505 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Queues.
CENG 213 Data Structure Queue 7/2/2018.
Queues.
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues.
Queues Definition of a Queue Examples of Queues
Getting queues right … finally (?)
Data Structures & Programming
Presentation transcript:

Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue

Prof. Amr Goneid, AUC2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues

Prof. Amr Goneid, AUC3 1. introduction to the Queue Data Structure  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  Insertions at one end (rear), deletions at another end (front)  First In First Out (FIFO) structure  Two basic operations: enqueue: add to rear, complexity is O(1) dequeue: remove from front, complexity is O(1)

Prof. Amr Goneid, AUC4 An Illustration

Prof. Amr Goneid, AUC5 Enqueue and Dequeue o When last array element is reached, we move back to start o The queue is viewed as a circular array o To enqueue: rear = (rear + 1) % size o To dequeue: front = (front + 1) % size o Both rear and front advance clockwise

Prof. Amr Goneid, AUC6 mukundan/dsal/QueueAppl.htmlDemo

Prof. Amr Goneid, AUC7 Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering Multiprogramming Some Queue Applications

Prof. Amr Goneid, AUC8 Queue Class Operations construct: construct an empty queue queueIsEmpty  bool : return True if queue is empty queueIsFull  bool : return True if queue is full enqueue(el) : add element (el) at the rear dequeue(el): retrieve and remove the front element queueFront(el): retrieve front without removing it queueLength  int : return the current queue length

Prof. Amr Goneid, AUC9 The queue may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor (default is 128) The queue ADT will be implemented as a template class to allow for different element types. 2. Array Based Queue Class Definition

Prof. Amr Goneid, AUC10 // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template class Queuet { public: Queuet (int nelements = 128);// Constructor Queuet (const Queuet &);// Copy Constructor ~Queuet ();// Destructor A Queue Class Definition

Prof. Amr Goneid, AUC11 // Member Functions void enqueue(Type );// Add to rear void dequeue(Type &);// Remove from front void queueFront(Type &) const;// Retrieve front bool queueIsEmpty() const;// Test for Empty queue bool queueIsFull() const;// Test for Full queue int queueLength() const;// Queue Length private: Type *queue;// pointer to dynamic array int front, rear, count, MaxSize; }; #endif // QUEUET_H #include "Queuet.cpp" A Queue Class Definition

Prof. Amr Goneid, AUC12 // File: Queuet.cpp // Queue template class implementation #include using namespace std; // Constructor with argument, size is nelements, default is 128 template Queuet ::Queuet(int nelements) { MaxSize = nelements; queue = new Type[MaxSize]; front = 1; rear = 0; count = 0; } A Queue Class Implementation

Prof. Amr Goneid, AUC13 // Copy Constructor template Queuet ::Queuet (const Queuet &original) :MaxSize(original.MaxSize), front(original.front), rear(original.rear), count(original.count) { queue = new Type[MaxSize]; for (int i = 0; i < MaxSize; i++) queue[i] = original.queue[i]; } // Destructor template Queuet ::~Queuet() { delete [ ] queue;} A Queue Class Implementation

Prof. Amr Goneid, AUC14 // Add to rear template void Queuet ::enqueue(Type v) { if(queueIsFull()) cout << "Queue is Full"; else {rear = (rear + 1) % MaxSize; queue[rear] = v; count++; } } A Queue Class Implementation

Prof. Amr Goneid, AUC15 // Remove from front template void Queuet ::dequeue(Type &v) { if(queueIsEmpty()) cout << "Queue is Empty"; else {v = queue[front]; front = (front + 1) % MaxSize; count--; } } A Queue Class Implementation

Prof. Amr Goneid, AUC16 // Retrieve front without removing it template void Queuet ::queueFront(Type &v) const { if(queueIsEmpty()) cout << "Queue is Empty" << endl; else v = queue[front]; } A Queue Class Implementation

Prof. Amr Goneid, AUC17 // Test for Empty queue template bool Queuet ::queueIsEmpty() const { return (count == 0); } // Test for Full queue template bool Queuet ::queueIsFull() const { return (count == MaxSize); } // Queue Length template int Queuet ::queueLength() const { return count; } A Queue Class Implementation

Prof. Amr Goneid, AUC18 // File: QueuetAppl.cpp // Test if a string is a palindrome #include using namespace std; #include "Stackt.h" #include "Queuet.h" bool palindrome(string w); A Driver Program to Test Class

Prof. Amr Goneid, AUC19 int main() { string w; cout << "Enter a string:" << endl; getline(cin,w); cout << w << endl; if (palindrome(w)) cout << "Palindrome" << endl; else cout << "NOT Palindrome" << endl; return 0; } A Driver Program to Test Class

Prof. Amr Goneid, AUC20 bool palindrome(string w) { Stackt s; Queuet q; int L = w.length(); char c,v; for (int i = 0; i < L; i++) { c = w.at(i); s.push(c); q.enqueue(c); } while(!q.queueIsEmpty()) { q.dequeue(c); s.pop(v); if(c != v) return false; } return true; } A Driver Program to Test Class

Prof. Amr Goneid, AUC21 Output: Enter a string: Palindrome Press any key to continue Enter a string: NOT Palindrome Press any key to continue A Driver Program to Test Class

Prof. Amr Goneid, AUC22 A Queue can be implemented as a linked structure. Requires more space than array implementations, but more flexible in size. Two pointers are needed: front for dequeue and rear for enqueue 3. Linked Queues

Prof. Amr Goneid, AUC23 Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main queue class. class node// Hidden from user { public: Type e;// stack element node *next;// pointer to next node }; // end of class node declaration typedef node * NodePointer; NodePointer front, rear;// pointers

Prof. Amr Goneid, AUC24 Enqueue Operation pnew New enqueue(v): NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; rear front

Prof. Amr Goneid, AUC25 Dequeue Operation 2 3 cursor front dequeue(v): v = front->e; cursor = front; front = front->next; delete cursor; 1 rear

Prof. Amr Goneid, AUC26 // File: QueueL.h // Linked List Queue class definition #ifndef QUEUEL_H #define QUEUEL_H template class QueueL { public: QueueL(); // Constructor ~QueueL(); // Destructor void enqueue(Type ); // Add to rear Linked Queue Class

Prof. Amr Goneid, AUC27 void dequeue(Type &); // Remove from front void queueFront(Type &) const;// retrieve front bool queueIsEmpty() const;// Test for Empty queue int queueLength() const;// Queue Length private: // Node Class class node { public: Type e;// queue element node *next;// pointer to next node }; // end of class node declaration Linked Queue Class

Prof. Amr Goneid, AUC28 typedef node * NodePointer; NodePointer front, rear;// Pointers int count;// length }; #endif // QUEUEL_H #include "QueueL.cpp" Linked Queue Class

Prof. Amr Goneid, AUC29 Queues can simulate waiting lines A waiting line is a queue of jobs waiting to be served by a server on FCFS (First Come First Serve) basis. 4. An Application of Queues

Prof. Amr Goneid, AUC30 Time of arrival of a job is essentially random, but with a fixed probability per unit time. A clock registers arrival time, the simulation is Time-Driven Simulation tries to answer the question: What happens if… 4. An Application of Queues

Prof. Amr Goneid, AUC31 Notations: T max Maximum Simulation Time (fixed) tClock Time = current time (0 ≤ t < T max ) Average time between two arrivals (fixed) P a Probability of arrival per unit time = 1/ T s Service time (fixed) T r Time remaining to start service t a Arrival time (random) T w Wait time = t - t a Simulation of a Waiting Line

Prof. Amr Goneid, AUC32 Clock (t) Simulation of a Waiting Line 0123tata t T max Arrival Service Starts tata tata enqueue dequeue TwTw

Prof. Amr Goneid, AUC33 Algorithm Set T max,, T s and compute P a = 1/ Initialize waiting line and clock t = 0 set T r = 0 while (t < T max ) { Test for arrival. If job arrived, process arrival. Test for server ready. If ready, exit line and start service. If (T r > 0) decrement T r Increment clock time t } Compute average wait time Simulation of a Waiting Line

Prof. Amr Goneid, AUC34 Test for Arrival & Arrival Processing arrival (Q) { Generate Random number (R) between 0 and 1.0 if ( R < P a ) // job arrived if (Q.queueIsFull()) report error: Line is full else { t a = t ; Q.enqueue (t a ); } } Simulation of a Waiting Line

Prof. Amr Goneid, AUC35 Test for server ready. If ready, exit line and start service. if((Tr is 0) and (not Q.queueIsEmpty())) { exitLine; start service (set Tr = Ts); } Simulation of a Waiting Line

Prof. Amr Goneid, AUC36 Exit Line exitLine (Q) { if ( Q.queueIsEmpty()) Report: Line is Empty else { Q.dequeue (t a ); T w = t – t a ; // wait time waitTotal = waitTotal + T w ; // Total wait time jobcount = jobcount + 1;// jobs serviced } Simulation of a Waiting Line

Prof. Amr Goneid, AUC37 Compute average wait time averagewait (Q) { if ( jobcount is 0) return 0; else return (waitTotal / jobcount); } Simulation of a Waiting Line

Prof. Amr Goneid, AUC38 Arrival# 5 at: 31 Job# 4 Service Started at: 33 wait = 9 Job# 5 Service Started at: 39 wait = 8 Arrival# 6 at: 46 Job# 6 Service Started at: 46 wait = 0 Arrival# 7 at: 48 Arrival# 8 at: 50 Arrival# 9 at: 52 Job# 7 Service Started at: 52 wait = 4 Job# 8 Service Started at: 58 wait = 8 Arrival# 10 at: 64 Job# 9 Service Started at: 64 wait = Arrival# 28 at: 278 Job# 28 Service Started at: 278 wait = 0 Average wait Time is Simulation of a Waiting Line A possible tracing of the simulation program

Prof. Amr Goneid, AUC39 Learn on your own about: Using Queues in buffering and scheduling The Deque (Double-Ended Queue) container