Queues 074826
Queues 074826
e
Queue Overview
◦Queue ADT
◦Basic operations of queue
◦ Enqueuing
◦ Dequeuing
◦Implementation of queue
◦ Array
◦ Linked list
Queue ADT
◦Like a stack, a queue is also a list.
◦Insertion is done only at one end,
while deletion is performed at the
other end.
◦Accessing the elements of queues
follows a First In, First Out (FIFO)
order.
◦Like customers standing in a check-out
line in a store, the first customer in is
The Queue ADT
◦Another form of restricted list
◦Insertion is done at one end,
whereas deletion is performed at the
other end
◦Basic operations:
◦enqueue: insert an element at the
rear of the list
◦dequeue: delete the element at the
front of the list
◦First-in First-out (FIFO) list
Remove Insert
(Dequeue) front rear (Enqueue)
Queue Implementation
using Array
◦There are several different
algorithms to implement Enqueue
and Dequeue
◦Naïve way
◦When enqueuing, the front index is
always fixed and rear
rear the rear index moves
rear
forward in the array.
3 3 6 3 6 9
6 9 9
bool Queue::IsEmpty() {
if (counter) return false;
else return true;
}
bool Queue::IsFull()
if (counter < maxSize) return false;
else return true;
}
Enqueue
bool Queue::Enqueue(double x) {
if (IsFull()) {
cout << "Error: the queue is full." << endl;
return false;
}
else {
// calculate the new rear position (circular)
rear = (rear + 1) % maxSize;
// insert new item
values[rear] = x;
// update counter
counter++;
return true;
}
}
Dequeue
bool Queue::Dequeue(double & x) {
if (IsEmpty()) {
cout << "Error: the queue is
empty." << endl;
return false;
}
else {
// retrieve the front item
x = values[front];
// move front
front = (front + 1) %
maxSize;
// update counter
counter--;
return true;
}
Printing the
elements
void Queue::DisplayQueue() {
cout << "front -->";
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << values[(front + i) % maxSize];
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
}
}
Sample run
using a
Queue
int main(void) {
Queue queue(5);
cout << "Enqueue 5 items." << endl;
for (int x = 0; x < 5; x++)
queue.Enqueue(x);
cout << "Now attempting to enqueue again..." << endl;
queue.Enqueue(5);
queue.DisplayQueue();
double value;
queue.Dequeue(value);
cout << "Retrieved element = " << value << endl;
queue.DisplayQueue();
queue.Enqueue(7);
queue.DisplayQueue();
return 0;
}
Queue Implementation
using Linked List
class Queue {
public:
Queue() { // constructor
front = rear = NULL;
counter = 0;
}
~Queue() { // destructor
double value;
while (!IsEmpty()) Dequeue(value);
}
bool IsEmpty() {
if (counter) return false;
else return true;
}
void Enqueue(double x);
bool Dequeue(double & x);
void DisplayQueue(void);
private:
Node* front; // pointer to front node
Node* rear; // pointer to last node
int counter; // number of elements
};
Enqueue
void Queue::Enqueue(double x) {
Node* newNode = new Node;
newNode->data = x;
newNode->next = NULL;
if (IsEmpty()) {
front = newNode;
rear = newNode;
}
else {
rear->next = newNode;
rear = newNode; rear
}
counter++; 8 5
} rear
8 5
newNode
Dequeue
bool Queue::Dequeue(double & x) {
if (IsEmpty()) {g
cout << "Error: the queue is empty." << endl;
return false;
}
else {
x = front->data;
Node* nextNode = front->next;
delete front;
front = nextNode;
counter--;
} front
}
3 8 5
front
8 5
Printing the
elements
void Queue::DisplayQueue() {
cout << "front -->";
Node* currNode = front;
for (int i = 0; i < counter; i++) {
if (i == 0) cout << "\t";
else cout << "\t\t";
cout << currNode->data;
if (i != counter - 1)
cout << endl;
else
cout << "\t<-- rear" << endl;
currNode = currNode->next;
}
}
Result
◦Queue implemented using linked list will
never be full