100% found this document useful (1 vote)
377 views11 pages

Queues

The document discusses queues and their implementation using arrays. A queue is a first-in first-out data structure where elements are added at the rear and removed from the front. It can be implemented using a circular array to avoid overflow issues when the queue is full.

Uploaded by

api-3801329
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
377 views11 pages

Queues

The document discusses queues and their implementation using arrays. A queue is a first-in first-out data structure where elements are added at the rear and removed from the front. It can be implemented using a circular array to avoid overflow issues when the queue is full.

Uploaded by

api-3801329
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Queues

• Queue is a data structure that can be used to store data which can
later be retrieved in the first in first out (FIFO) order.
• Queue is an ordered-list in which all the insertions and deletions
are made at two different ends to maintain the FIFO order.
• The operations defined on a Queue are:
1. Add - Store onto a Queue
2. remove - retrieve (delete) from Queue
3. Is_empty - check if the Queue is empty
4. Is_Full - check if the Queue is full
• A Queue can be very easily implemented using arrays.
• Queue is implemented by maintaining one pointer to the front
element in the Queue and another pointer pointing to the rear of
the Queue.
• Insertions are made at the rear and deletions are made from the
front.
Queues – Array Implementation
class Queue {
public:
Queue(int s = 10); // constructor - default size =
10
~Queue() {delete [ ] QueueArray; } // destructor
bool add (int);
bool remove (int &);
bool isFull() {return MaxSize == size;}
bool isEmpty() {return size == 0; }
private:
int MaxSize; // max Queue size
int front, rear;
int *QueueArray;
int size; // no. of elements in the Queue
};
Queue::Queue(int s)
{
if (s <= 0) MaxSize = 10; else MaxSize = s;
QueueArray = new int[MaxSize];
size = 0;
rear = -1; // points to the last element
front = 0; // points to first element
}
bool Queue::add(int n)
{
if (! isFull() ) {
rear++;
QueueArray[rear] = n;
size++;
return true;
}
else return false;
}
bool Queue::remove(int &n)
{
if (! isEmpty() {
n = QueueArray[front];
front++;
size--;
return true;
}
else return false;
}
• Assume MaxSize = 5
• Initial condition
size = 0; front = 0; rear = -1;

• Add 3, remove 1
size = 2; front = 1; rear = 2;
• Add 2 more, remove 1 more
size = 3; front = 2; rear = 4;

• Question: Is the Queue Full?


• Where to add the next element?
• Push everything back
size = 3; front = 0; rear = 2;

• Cost? • O (size)
Circular Implementation

bool Queue::add(int n)
{
if (! isFull() ) {
rear++;
if (rear == MaxSize)
rear = 0; 3
QueueArray[rear] = n;
size++; 2
return true; 1
} MS-1 0
else return false;
}
Circular Implementation

bool Queue::remove(int &n)


{
if (! isEmpty() {
n = QueueArray[front];
front++;
3 if (front == MaxSize)
front = 0;
2 size--;
return true;
1
MS-1 0 }
else return false;
}
3

1
MS-1 0
bool Queue::add(int n) bool Queue::remove(int &n)
{ {
if (! isFull() ) { if (! isEmpty() {
rear++; n = QueueArray[front];
if (rear == MaxSize) front++;
rear = 0; if (front == MaxSize)
QueueArray[rear] = n; front = 0;
size++; size--;
return true; return true;
} }
else return false; else return false;
} }
Circular Implementation

1
MS-1 0

Add  rear = (rear + 1) % MaxSize;

Remove  front = (front + 1) % MaxSize;

You might also like