Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

A queue is a useful data structure in programming.

It is similar to the ticket queue


outside a cinema hall, where the first person entering the queue is the first person
who gets the ticket. Queue follows the First In First Out(FIFO) rule - the item that goes
in first is the item t hat comes out first too.

Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the
other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data
item stored first will be accessed first.

A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits
first. More real-world examples can be seen as queues at the ticket windows and bus-stops.

Call Center phone systems uses Queues to hold people


calling them in an order

Queue Representation
As we now understand that in queue, we access both ends for different reasons. The following
diagram given below tries to explain queue representation as data structure
Basic Operations on Queue
 Enqueue: Add an element to the end of the queue

 Dequeue: Remove an element from the front of the queue

Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively
difficult to implement than that of stacks.

The following steps should be taken to enqueue (insert) data into a queue −

 Step 1 − Check if the queue is full.

 Step 2 − If the queue is full, produce overflow error and exit.

 Step 3 − If the queue is not full, increment rear pointer to point the next empty space.

 Step 4 − Add data element to the queue location, where the rear is pointing.

 Step 5 − return success.

Algorithm for enqueue operation


procedure enqueue(data)

if queue is full
return overflow
endif

rear ← rear + 1
queue[rear] ← data
return true
end procedure

Dequeue Operation

Accessing data from the queue is a process of two tasks − access the data where front is pointing
and remove the data after access. The following steps are taken to perform dequeue operation −

 Step 1 − Check if the queue is empty.

 Step 2 − If the queue is empty, produce underflow error and exit.

 Step 3 − If the queue is not empty, access the data where front is pointing.

 Step 4 − Increment front pointer to point to the next available data element.

 Step 5 − Return success.

Algorithm for dequeue operation


procedure dequeue

if queue is empty
return underflow
end if

data = queue[front]
front ← front + 1
return true
end procedure
Few more functions are required to make the above-mentioned queue operation efficient. These
are −

 peek() − Gets the element at the front of the queue without removing it.

 isfull() − Checks if the queue is full.

 isempty() − Checks if the queue is empty.

peek()

This function helps to see the data at the front of the queue. The algorithm of peek() function is
as follows −

Algorithm

begin procedure peek


return queue[front]
end procedure

Implementation of peek() function in C programming language −

Example

int peek() {
return queue[front];
}

isfull()

As we are using single dimension array to implement queue, we just check for the rear pointer to
reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a
circular linked-list, the algorithm will differ. Algorithm of isfull() function −

Algorithm

begin procedure isfull

if rear equals to MAXSIZE


return true
else
return false
endif

end procedure

Implementation of isfull() function in C programming language −

Example

bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}

isempty()

Algorithm of isempty() function −

Algorithm

begin procedure isempty

if front is less than MIN OR front is greater than rear


return true
else
return false
endif

end procedure

If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence
empty.

Here's the C programming code −

Example

int isempty() {
if(front < 0 || front > rear)
return 1;
else
return 0;
}

Example program on Queue operations using Array


#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
}
}
}
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow n");
else
{
if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}

You might also like