Table of Contents
Implementing a Queue Using Array: Step-by-Step Guide
Like stacks, queues are lists. With a queue, however, insertion is done at one end, whereas deletion is performed at the other end. Implementation of queue using Array is given below.
The basic operations on a queue are enqueue, which inserts an element at the end of the list (called the rear), and dequeue, which deletes (and returns) the element at the start of the list (known as the front).
Function to create queue
/* Function to create queue */
void Insert_queue()
{
printf("\n Input the element :");
scanf("%d", &ch);
if(rear < size)
{
rear ++;
q[rear] = ch ;
if(front == 0)
front = 1;
}
else
printf("\n Queue is overflow");
}
Function to perform delete operation
/* Function to perform delete operation */
void Delete_queue()
{
if (front == 0)
{
printf("\n Underflow");
return ;
}
else
{
ch = q[front];
printf("\n Element deleted : %d", ch);
}
if(front == rear)
{
front = 0;
rear = 0;
}
else
front = front + 1;
}
Output Display function
/* Output function */
void Display_queue() //char q[])
{
int i;
if (front == 0)
return;
for(i = front ; i
INSERTION AND DELETION IN A QUEUE ARRAY IMPLEMENTATION
/* INSERTION AND DELETION IN A QUEUE ARRAY IMPLEMENTATION */
/* queue_a.c */
# include
# include
# include
# include
# define size 10
//int rear, front;
int ch;
int q[size];
int rear = 0;
int front = 0;
void Insert_queue();
void Delete_queue();
void Display_queue();
/* Function to create queue */
void Insert_queue()
{
printf("\n Input the element :");
scanf("%d", &ch);
if(rear < size)
{
rear ++;
q[rear] = ch ;
if(front == 0)
front = 1;
}
else
printf("\n Queue is overflow");
}
/* Function to perform delete operation */
void Delete_queue()
{
if (front == 0)
{
printf("\n Underflow");
return ;
}
else
{
ch = q[front];
printf("\n Element deleted : %d", ch);
}
if(front == rear)
{
front = 0;
rear = 0;
}
else
front = front + 1;
}
/* Output function */
void Display_queue() //char q[])
{
int i;
if (front == 0)
return;
for(i = front ; i
Output:
Insert->i Delete->d Quit->q:
Input the choice : d
Your choice is: d
Underflow
Queue content after deleteion is as follows:
Insert->i Delete->d Quit->q:
Input the choice : i
Your choice is: i
Input the element :12
Queue after inserting 12
Insert->i Delete->d Quit->q:
Input the choice : i
Your choice is: i
Input the element :14
Queue after inserting 12 14
Insert->i Delete->d Quit->q:
Input the choice : d
Your choice is: d
Element deleted : 12
Queue content after deleteion is as follows:
14
Insert->i Delete->d Quit->q:
Input the choice : q
Your choice is: q Press any key to continue . . .