1 / 39

CSCE 210 Data Structures and Algorithms

CSCE 210 Data Structures and Algorithms. Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue. The Queue ADT. Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues . 1. introduction to the Queue Data Structure.

judith
Télécharger la présentation

CSCE 210 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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, AUC

  3. 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, AUC

  4. An Illustration Prof. Amr Goneid, AUC

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

  6. Demo http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/QueueAppl.html Prof. Amr Goneid, AUC

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

  8. 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, AUC

  9. 2. Array Based Queue Class Definition • 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. Prof. Amr Goneid, AUC

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

  11. A Queue Class Definition // 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" Prof. Amr Goneid, AUC

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

  13. A Queue Class Implementation // Copy Constructor template <class Type> Queuet <Type> ::Queuet (const Queuet<Type> &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 <class Type> Queuet<Type>::~Queuet() { delete [ ] queue;} Prof. Amr Goneid, AUC

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

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

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

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

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

  19. A Driver Program to Test Class 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; } Prof. Amr Goneid, AUC

  20. A Driver Program to Test Class bool palindrome(string w) { Stackt<char> s; Queuet<char> 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; } Prof. Amr Goneid, AUC

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

  22. 3. Linked Queues • 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 Prof. Amr Goneid, AUC

  23. 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, AUC

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

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

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

  27. Linked Queue Class 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 Prof. Amr Goneid, AUC

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

  29. 4. An Application of Queues • 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. Prof. Amr Goneid, AUC

  30. 4. An Application of Queues • 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… Prof. Amr Goneid, AUC

  31. Simulation of a Waiting Line • Notations: • Tmax Maximum Simulation Time (fixed) • t Clock Time = current time (0 ≤ t < Tmax) • <Ta > Average time between two arrivals (fixed) • Pa Probability of arrival per unit time = 1/ <Ta > • Ts Service time (fixed) • Tr Time remaining to start service • ta Arrival time (random) • Tw Wait time = t - ta Prof. Amr Goneid, AUC

  32. Simulation of a Waiting Line Tw Tmax Clock (t) Service Starts Arrival dequeue enqueue Prof. Amr Goneid, AUC

  33. Simulation of a Waiting Line Algorithm Set Tmax , <Ta> , Ts and compute Pa = 1/<Ta> Initialize waiting line and clock t = 0 set Tr = 0 while (t < Tmax) { Test for arrival. If job arrived, process arrival. Test for server ready. If ready, exit line and start service. If (Tr > 0) decrement Tr Increment clock time t } Compute average wait time Prof. Amr Goneid, AUC

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

  35. Simulation of a Waiting Line 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); } Prof. Amr Goneid, AUC

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

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

  38. Simulation of a Waiting Line 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 A possible tracing of the simulation program Prof. Amr Goneid, AUC

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

More Related