Download presentation
Presentation is loading. Please wait.
1
CS505 Data Structures and Algorithms
Queue 1 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 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) , deletions at another end (front) First In First Out (FIFO) structure Two basic operations: enqueue: add to rear dequeue: remove from front 3 3
4
An Illustration 4 4
5
Enqueue and Dequeue Problem of Array representation
Solution using Circular Array. 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 5 5
6
Some Queue Applications
Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering 6 6
7
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 7 7
8
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. 8 8
9
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 9 9
10
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" 10 10
11
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 11 11
12
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 12 12
13
Enqueue Operation 2 1 pnew rear front New enqueue(v):
NodePointer pnew = new node; pnew->e = v; pnew->next = NULL; rear->next = pnew; rear = pnew; 13 13
14
Dequeue Operation 1 rear cursor front 3 2 dequeue(v): v = front->e;
front = front->next; delete cursor; 14 14
15
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 15 15
16
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 16 16
17
Linked Queue Class typedef node * NodePointer;
NodePointer front , rear; // Pointers int count; // length }; #endif // QUEUEL_H #include "QueueL.cpp" 17 17
18
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++; } 18 18
19
Part of Implementation file
template <class Type> void QueueL<Type>::dequeue(Type &v) { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else v = front->e; cursor = front; front = front->next; delete cursor; count--; } 19 19
20
Part of Implementation file
template <class Type> void QueueL<Type>::queueFront(Type &v) const { NodePointer cursor; if(queueIsEmpty()) cout << "Queue is Empty" << endl; else { v = front->e; } } 20 20
21
Implement Queue using ADT of linked List
Exercise Implement Queue using ADT of linked List
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.