1 / 37

Queues

Queues. Queues. Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue) are done at the rear only Removals (deletions or dequeue) are made from the front only. Queue ADT. AbstractDataType queue { instances

vsauer
Download Presentation

Queues

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

  2. Queues • Like a stack, special kind of linear list • One end is called front • Other end is called rear • Additions (insertions or enqueue) are done at the rear only • Removals (deletions or dequeue) are made from the front only

  3. Queue ADT AbstractDataType queue { instances ordered list of elements; one end is the front; the other is the rear; operations empty(): Return true if queue is empty, return false otherwise size(): Return the number of elements in the queue pop(): Remove an element from the queue // dequeue push(x): Add element x to the queue // enqueue } It is also possible to represent Queues using • Array-based representation • Linked representation

  4. . x y z Front Rear A pointer Implementation of Queues Keep two pointers: • FRONT: A pointer to the first element of the queue. • REAR: A pointer to the last element of the queue.

  5. A pointer Implementation of Queues MAKENULL(Q) NULL Q.front Q.Rear ENQUEUE(x,Q) . x Q.front Q.Rear

  6. A pointer Implementation of Queues ENQUEUE(y,Q) . x y Q.front Q.Rear DEQUEUE(Q) . y Q.front Q.Rear

  7. A class for Dynamic Queue implementation class DynIntQueue{private: struct QueueNode { int value; QueueNode *next; }; QueueNode *front; QueueNode *rear; int numItems; public: DynIntQueue(void); ~DynIntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); void makeNull(void);};

  8. Implemenaton //************************// Constructor *//************************DynIntQueue::DynIntQueue(void){ front = NULL; rear = NULL; numItems = 0;}//************************// Destructor *//************************DynIntQueue::~DynIntQueue(void){ makeNull();}

  9. //********************************************// Function enqueue inserts the value in num *// at the rear of the queue. *//********************************************void DynIntQueue::enqueue(int num){ QueueNode *newNode; newNode = new QueueNode; newNode->value = num; newNode->next = NULL; if (isEmpty()) { front = newNode; rear = newNode; } else { rear->next = newNode; rear = newNode; } numItems++;}

  10. //**********************************************// Function dequeue removes the value at the *// front of the queue, and copies it into num. *//**********************************************int DynIntQueue::dequeue(void){ QueueNode *temp; int num; if (isEmpty()) cout << "The queue is empty.\n"; else { num = front->value; temp = front->next; delete front; front = temp; numItems--; } return num; }

  11. //*********************************************// Function isEmpty returns true if the queue *// is empty, and false otherwise. *//*********************************************bool DynIntQueue::isEmpty(void){ if (numItems) return false; else return true;}

  12. //********************************************// Function makeNull dequeues all the elements *// in the queue. *//********************************************void DynIntQueue::makeNull(void){ while(!isEmpty()) dequeue();}

  13. Program // This program demonstrates the DynIntQeue classvoid main(void){ DynIntQueue iQueue; cout << "Enqueuing 5 items...\n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:\n"; while (!iQueue.isEmpty()) { int value;value =iQueue.dequeue(); cout << value << endl; }}

  14. Program Ouput Enqueuing 5 items...The values in the queue were:01234

  15. Array Implementation When queue is empty both front and rear are set to -1 While enqueueing increment rear by 1, and while dequeueing increment front by 1 When there is only one value in the Queue, both rear and front have same index Front First Element Second Element . . Rear Last Element maxlength

  16. 5 4 6 7 8 8 7 7 7 6 6 6 12 67 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Front=0 Rear=6 Front=5 Rear=8 Front=4 Rear=6 Array Implementation How can we insert more elements? Rear index can not move beyond the last element….

  17. Solution: Using circular queue • Allow rear to wrap around the array. if(rear == queueSize-1) rear = 0; else rear++; • Or use modular arithmetic rear = (rear + 1) % queueSize;

  18. 39 7 7 6 6 12 12 67 67 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Front=5 Rear=8 Front=5 Rear=0 Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0

  19. Custom Array Queue • Use a 1D array queue • Circular view of array

  20. Custom Array Queue • Possible configurations with three elements.

  21. 39 39 39 40 40 41 41 42 42 43 7 7 7 6 6 6 12 12 12 67 67 67 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 Front=5 Rear=0 Front=5 Rear=4 Front=5 Rear=3

  22. How to determine empty and full Queues? • It is tricky • Number of approaches • A counter indicating number of values in the queue can be used • We will see another approach as well at the end

  23. Implementation class IntQueue{private: int *queueArray; int queueSize; int front; int rear; int numItems; public: IntQueue(int); ~IntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); bool isFull(void); void clear(void);}; Note, the member function clear, which clears the queue by resetting the front and rear indices, and setting the numItems to 0.

  24. IntQueue::IntQueue(int s) //constructor{ queueArray = new int[s]; queueSize = s; front = -1; rear = -1; numItems = 0;} IntQueue::~IntQueue(void) //destructor { delete [] queueArray; }

  25. //********************************************// Function enqueue inserts the value in num *// at the rear of the queue. *//********************************************void IntQueue::enqueue(int num){ if (isFull()) cout << "The queue is full.\n"; if (IsEmpty() Front=Rear=0; else { // Calculate the new rear position rear = (rear + 1) % queueSize; } // Insert new item queueArray[rear] = num; // Update item count numItems++;}

  26. //*********************************************// Function dequeue removes the value at the *// front of the queue, and copies t into num. *//*********************************************int IntQueue::dequeue(void){ if (isEmpty()) cout << "The queue is empty.\n"; else { // Retrieve the front item int num = queueArray[front]; // Move front front = (front + 1) % queueSize; // Update item count numItems--; } return num; }

  27. //*********************************************// Function isEmpty returns true if the queue *// is empty, and false otherwise. *//*********************************************bool IntQueue::isEmpty(void){ if (numItems) return false; else return true;}

  28. //********************************************// Function isFull returns true if the queue *// is full, and false otherwise. *//********************************************bool IntQueue::isFull(void){ if (numItems < queueSize) return false; else return true;}

  29. //*******************************************// Function clear resets the front and rear *// indices, and sets numItems to 0. *//*******************************************void IntQueue::clear(void){ front = - 1; rear = - 1; numItems = 0;}

  30. //Program demonstrating the IntQueue class void main(void){ IntQueue iQueue(5); cout << "Enqueuing 5 items...\n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Attempt to enqueue a 6th item. cout << "Now attempting to enqueue again...\n"; iQueue.enqueue(5); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:\n"; while (!iQueue.isEmpty()) { int value; iQueue.dequeue(value); cout << value << endl; }}

  31. Program Output Enqueuing 5 items...Now attempting to enqueue again...The queue is full.The values in the queue were:0 1 2 3 4

  32. Another implementation of Queues using Arrays class CQueue { int Data*,QueueSize,Front,Rear; public: CQueue(int size); ~CQueue(int size); bool IsFull(); bool IsEmpty(); void Enqueue(int num); int Dequeue(); void MakeNull; };

  33. CQueue::CQueue(int size) { Front=Rear=-1; Data=new int[size]; } void CQueue ::Enqueue(int num); { if (IsFull()) { cout<<“Overflow” return; } if (IsEmpty() Rear=Front=0; else Rear=(Rear+1) % QueueSize; Data[Rear]=num; }

  34. int CQueue ::Dequeue(int num); { if (IsEmpty()) { cout<<“Underflow”; return; } int ReturnValue=Data[Front]; if (Front==Rear) //only one element in the queue Front=Rear=-1; else Front=(Front+1) % QueueSize; return ReturnValue; }

  35. bool CQueue::IsEmpty() { if (Front==-1) return true; else return false; } bool CQueue::IsFull() { If (((Rear+1)%QueueSize)==Front) return true; else return false; }

More Related