Queues Using Arrays
Queues Using Arrays
Data Structures
Using C
Queue
Storesa set of elements in a particular order
Queue principle: FIRST IN FIRST OUT = FIFO
It means: the first element inserted is the first one to
be removed
Example
Rear Front
The Queue Operations
Front
Rear
The Queue Operations
Front
Rear
The Queue Operations
Rear
Front
Array Implementation
4 8 6
An array of integers
to implement a We don't care what's in
queue of integers this part of the array.
Array Implementation
4 8 6
A Dequeue Operation
dequeue(); 2 last
4 8 6
An Enqueue Operation
enqueue(2); 3 last
8 6 2
An Enqueue Operation
enqueue(7); 4 last
8 6 2 7
An Enqueue Operation
enqueue(1); 5 last
8 6 2 7 1
An Enqueue Operation
enqueue(5); 6 last
8 6 2 7 1 5
An Dequeue Operation
dequeue(); 6 last
8 6 2 7 1 5
An Dequeue Operation
dequeue(); 6 last
8 6 2 7 1 5
Implementing a Queue
Enqueue at data[rear+1]
Dequeue at data[front]
The rear variable always contains the
index of the last item in the queue.
The front variable always contains the
index of the first item in the queue.
When we reach the end of the array, wrap
around to the front again.
17
Implementing a Queue
Conditions for
Queue FULL?
3 first
Queue EMPTY? 3
66 last
8 6 2 7 1 5
18
Initialization of front and rear?
int front = -1, rear = -1;
How do we know if a queue is full or
empty?
Testing for a full or empty queue.
if(rear == maxsize-1) if (front == -1 || front > rear)
{ {
printf("\nOVERFLOW\n"); printf("\nUNDERFLOW\n");
return; return;
} }
Implementing a Queue
// enqueue function
// dequeue function
dequeue() { // check for underflow
item = data[front];
if (front == rear)
{ front = -1;
rear = -1 ; }
else
{ front = front + 1;
totaltems--; }
Issue?
At the End of the Array
2 6 1
Circular Queue - At the End of
the Array
The new element goes at the front of 4 TE
the array (if that spot isn’t already
used): 3 first
0 last
4 2 6 1
Circular Queue
0 last
4 2 6 1
Implementing a Circular Queue
// enqueue function
// dequeue function
dequeue() {// check for underflow
if(totaltems==0)
{
printf("Queue is empty\n");
}
Easy to implement 3 TE
But it has a limited capacity with a fixed array
Special behaviour is needed when the rear 0 first
reaches the end of the array.
2 last
4 8 6
Array Implementation of Linear Queue
#include<stdio.h> switch(choice)
#include<stdlib.h> {
void insert(); case 1:
void dequeue(); insert();
void display(); break;
int front = -1, rear = -1 ,maxsize; case 2:
int queue[100]; dequeue();
int main () break;
{ case 3:
int choice; display();
printf("\n Enter the size of QUEUE : "); break;
scanf("%d",&maxsize); case 4:
printf("\n QUEUE OPERATIONS USING exit(0);
ARRAY"); break;
printf("\n1.insert an element\n2.Delete an default:
element\n3.Display the queue\n4.Exit"); printf("\nEnter valid choice??\n");
while(choice != 4) }
{ }
printf("\nEnter your choice : "); return 0;
scanf("%d",&choice); }
Array Implementation of Linear Queue
void enqueue()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1 && front==0)
{
printf("\nOVERFLOW\n");
return; State the issue in this enqueue function.
} Include the solution to overcome the issue.
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
Array Implementation of Linear Queue
void dequeue()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
Array Implementation of Linear Queue
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\n Elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
}
}
Write the complete program to implement the circular queue.