1 / 30

Data Structures and Algorithms

Data Structures and Algorithms. Queue. 1. The Queue ADT. Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues. 2. 1. introduction to the Queue Data Structure.

gcharlene
Download Presentation

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. Data Structures and Algorithms Queue 1

  2. The Queue ADT • Introduction to the Queue data structure • Designing a Queue class using dynamic arrays • Linked Queues • An Application of Queues 2

  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)(back) , deletions at another end (front). • You cannot add/extract entry in the middle of the queue. • A queue is open at two ends. • First In First Out (FIFO) structure • Two basic operations: enqueue: add to rear dequeue: remove from front 3

  4. An Illustration 4

  5. An Illustration 5

  6. Some Queue Applications • Simulation of waiting lines • Simulation of serviceable events • Job scheduling • Input/Output Buffering 6

  7. Array Implementation of Queue 7

  8. Array Implementation of Queue 8

  9. Array Implementation of Queue 9

  10. Array Implementation of Queue • 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 10

  11. Array Implementation of Queue 11

  12. Array Implementation of Queue 12

  13. Array Implementation of Queue • 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 13

  14. Array Implementation of Queue • 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. 14

  15. Array Implementation of Queue // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template <class Type> class Queuet { public: Queuet (int s = 128); // Constructor ~Queuet (); // Destructor 15

  16. Array Implementation of Queue // Member Functions void enqueue(Type ); // Add to rear void dequeue(); // Remove from front Type queueFront() 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" 16

  17. Array Implementation of Queue //Queue.cpp #include <iostream> Using names pace std; #include " QUEUET_H.h“ Queuet: : Queuet (int s) : Front(0) , rear(0), count(0) { MaxSize=s ; queue=new int[s] ; } ~ Queuet() {delete [] queue ;} //queueIsEmpty() bool Queue: :queueIsEmpty() const { return (count==0);// if(front == rear) } 17

  18. Array Implementation of Queue // enqueue() void Queuet::enqueue( Type alue) { Type newBack = (rear+ 1) % MaxSize;// circular if (newBack != front) // queue Isn't full { queue[rear] = value; rear = newBack; count++;} } // Type queueFront() Type Queuet:: queueFront() const { if ( ! queueIsEmpty() ) return (queue[front]); } //dequeue void Queuet::dequeue() { i f ( ! queueIsEmpty() ) front = (front + 1) % MaxSize; count--;} 18

  19. Array Implementation of Queue // queueIsfull() bool Queuet:: queueIsfull ( ) const { return (count==MaxSize);} //queueLength() const; Int Queuet::queueLength() const { return count; } 19

  20. 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 20

  21. 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 21

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

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

  24. 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 24

  25. Linked Queue Class void dequeue(); // Remove from front void queueFront() 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 25

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

  27. Part of Implementation file template <class Type> void QueueL<Type>::enqueue(Type v) { NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; if(queueIsEmpty()) { front = pnew; rear = pnew;} else {rear->next = pnew; rear = pnew;} count++; } 27

  28. Part of Implementation file template <class Type> void QueueL<Type>::dequeue() { NodePointer cursor; if(queueIsEmpty()) cout<<” Queue is empty “<<endl; • else { • cursor = front; • front =front->next; • delete cursor; • count--; • }} 28

  29. Part of Implementation file template <class Type> void QueueL<Type>::queueFront() const { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else {return front->e; } } 29

  30. Exercise • Implement Queue using ADT of a circular linked List

More Related