Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue."— Presentation transcript:

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

2 Prof. Amr Goneid, AUC2 The Queue ADT Introduction to the Queue data structure Array Models Array Based Queue ADT Linked Queues Simulation of a Waiting Line

3 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)

4 Prof. Amr Goneid, AUC4 An Illustration

5 Prof. Amr Goneid, AUC5 http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/QueueAppl.htmlDemo

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

7 Prof. Amr Goneid, AUC7 Model 1: Variable Front and Rear Problem: Not possible to enqueue although there is space. 2. Array Models 0 n-1 QFR Dequeue FR FR Enqueue FR FR

8 Prof. Amr Goneid, AUC8 Model 2: Fixed Front, variable Rear Problem: To empty a queue of n elements we have to shift the elements to the left each time we dequeue. Total number of shifts = (n-1) + (n-2) + …..+ 2 + 1 = n(n-1)/2 = O(n 2 ) Too Expensive Array Models 0 n-1 Q Dequeue

9 Prof. Amr Goneid, AUC9 Array Models Model 3: Ring Model o The queue is viewed as a circular array o When last array element is reached, we move back to start o To enqueue: rear = (rear + 1) % size o To dequeue: front = (front + 1) % size o Both rear and front advance clockwise o Keep a count of the number of elements in the queue o Initially, the queue is empty: front = 1rear = 0 count = 0 No need to do any shifts !

10 Prof. Amr Goneid, AUC10 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. 3. Array Based Queue ADT

11 Prof. Amr Goneid, AUC11 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 queueRear(el): retrieve rear without removing it queueLength  int : return the current queue length

12 Prof. Amr Goneid, AUC12 // 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 ();// Destructor A Queue Class Definition

13 Prof. Amr Goneid, AUC13 // Member Functions void enqueue(Type );// Add to rear void dequeue(Type &);// Remove from front void queueFront(Type &) const;// Retrieve front void queueRear(Type &) const;// Retrieve rear 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

14 Prof. Amr Goneid, AUC14 // 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

15 Prof. Amr Goneid, AUC15 Implement the other member functions as in: http://www.cse.aucegypt.edu/~csci210/210 CE2.pdf A Queue Class Implementation

16 Prof. Amr Goneid, AUC16 // 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

17 Prof. Amr Goneid, AUC17 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

18 Prof. Amr Goneid, AUC18 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

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

20 Prof. Amr Goneid, AUC20 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 4. Linked Queues

21 Prof. Amr Goneid, AUC21 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

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

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

24 Prof. Amr Goneid, AUC24 // 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

25 Prof. Amr Goneid, AUC25 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

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

27 Prof. Amr Goneid, AUC27 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. 5. Simulation of a Waiting Line

28 Prof. Amr Goneid, AUC28 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… Simulation of a Waiting Line

29 Prof. Amr Goneid, AUC29 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

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

31 Prof. Amr Goneid, AUC31 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

32 Prof. Amr Goneid, AUC32 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

33 Prof. Amr Goneid, AUC33 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

34 Prof. Amr Goneid, AUC34 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

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

36 Prof. Amr Goneid, AUC36 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 = 12............... Arrival# 28 at: 278 Job# 28 Service Started at: 278 wait = 0 Average wait Time is 2.89286 Simulation of a Waiting Line A possible tracing of the simulation program

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


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

Similar presentations


Ads by Google