Download presentation
Presentation is loading. Please wait.
Published byGregory Hubbard Modified over 8 years ago
1
1 Data Structures and Algorithms Queue
2
2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues An Application of Queues
3
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
4
4 An Illustration
5
5
6
6 Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering Some Queue Applications
7
7 Array Implementation of Queue
8
8
9
9
10
10 Array Implementation of Queue oWhen last array element is reached, we move back to start oThe queue is viewed as a circular array oTo enqueue: rear = (rear + 1) % size oTo dequeue: front = (front + 1) % size oBoth rear and front advance clockwise
11
11 Array Implementation of Queue
12
12 Array Implementation of Queue
13
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
14
14 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. Array Implementation of Queue
15
15 // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template class Queuet { public: Queuet (int s = 128);// Constructor ~Queuet ();// Destructor Array Implementation of Queue
16
16 // 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" Array Implementation of Queue
17
17 Array Implementation of Queue //Queue.cpp #include 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) }
18
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--;}
19
19 Array Implementation of Queue // queueIsfull() bool Queuet:: queueIsfull ( ) const { return (count==MaxSize);} // queueLength() const; Int Queuet:: queueLength() const { return count; }
20
20 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
21
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
22
22 Enqueue Operation enqueue(v): NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; rear New pnew 1 2 front
23
23 Dequeue Operation 2 3 cursor front dequeue(v): v = front->e; cursor = front; front = front->next; delete cursor; 1 rear
24
24 // 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
25 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 Linked Queue Class
26
26 typedef node * NodePointer; NodePointer front, rear;// Pointers int count;// length }; #endif // QUEUEL_H #include "QueueL.cpp" Linked Queue Class
27
27 Part of Implementation file template void QueueL :: 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++; }
28
28 Part of Implementation file template void QueueL :: dequeue () { NodePointer cursor; if(queueIsEmpty()) cout<<” Queue is empty “<<endl; else { cursor = front; front =front->next; delete cursor; count--; }}
29
29 Part of Implementation file template void QueueL :: queueFront () const { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else {return front->e; } }
30
Implement Queue using ADT of a circular linked List Exercise
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.