Implementation of queue using array in C
In this post, you will learn how to implement a queue using an array in C programming language.
A queue is a linear data structure where insertion of elements is done from one end and deletion from the other end. The queue is like a line of people at a polling booth where, the first person in the line gets the first vote. This is called First-In-First-Out (FIFO). Insertion of the element is done from the one end, called the FRONT end, and deletion is done from the other end called the REAR. Initially, the value of front and queue is -1 which represents an empty queue.
Queue Operations
These are the operations performed by a queue.
- Enqueue- The element to add in a queue. If the queue is full, then it is said to be an overflow condition.
- Dequeue- The element to delete in a queue. If the queue is empty, then it is said to be an underflow condition.
- Peek- Retrieve the element from the front end of the queue without removing it from the queue.
- IsEmpty- Check if the queue is empty or not.
- Size- Return the total number of elements present in the queue.
- Front- Get the front item from the queue.
- Rear- Get the last item from the queue.
Algorithm to Insert Element in a Queue
Step 1: IF REAR = MAX-1
print('Queue Overflow')
return
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT
In the above algorithm, we have checked the existence of space in the queue. If there is no space, then print 'Queue Overflow' and the code is exited, and if not then, we check the element at the FRONT and REAR, if both are empty, then set both to 0 else increase REAR with one and insert an element from the REAR end and return.
Algorithm to Delete Element in a Queue
Step 1: IF FRONT = -1 OR FRONT > REAR
print('Queue Underflow')
return
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
In the above algorithm, we have checked the existence of elements in the queue. If there is no element, then print 'Queue Underflow' and the code is exited, and if not, then FRONT is increased by one and point to the next element in the queue.
Queue Implementation Program in C using array
In the given example, we have asked the user for operations like insert, delete, display, and exit. As indicated by the choice entered by the user, access its respective function using the switch statement. Utilize the variables front and rear to address the first and last element of the queue.
In the function insert_queue(), initially check if the queue is full. If it is, then print the result as "Queue Overflow". If not, take the number to be inserted as input and store it in the variable add_item. Duplicate the variable add_item to the array queue_array[] and increment the variable rear by 1.
In the function delete_queue(), first check if the queue is empty. If it is, then print the result as "Queue Underflow". Otherwise, print the first element of the array queue_array[] and decrement the variable front by 1.
In the function display(), the for loop prints all the elements of the array, starting from front to rear.
#include <stdio.h>
#define MAX 30
void insert_queue();
void delete_queue();
void display_queue();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("\nEnter 1 to insert element to queue \n");
printf("\nEnter 2 to delete element from queue \n");
printf("\nEnter 3 to display all elements of queue \n");
printf("\nEnter 4 to exit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert_queue();
break;
case 2:
delete_queue();
break;
case 3:
display_queue();
break;
case 4:
exit(1);
default:
printf("Invalid input \n");
}
}
}
void insert_queue()
{
int add_item;
if (rear == MAX - 1)
printf("QUEUE OVERFLOW \n");
else
{
if (front == - 1)
front = 0;
printf("Enter value to inset in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete_queue()
{
if (front == - 1 || front > rear)
{
printf("QUEUE UNDERFLOW \n");
return ;
}
else
{
printf("Element deleted from queue : %d\n", queue_array[front]);
front = front + 1;
}
}
void display_queue()
{
int i;
if (front == - 1)
printf("EMPTY QUEUE \n");
else
{
printf("Displaying Queue : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Output of the above code:
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 1
Enter value to inset in queue : 98
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 1
Enter value to inset in queue : 73
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 1
Enter value to inset in queue : 55
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 3
Displaying Queue :
98 73 55
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 2
Element deleted from queue : 98
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice : 3
Displaying Queue :
73 55
Enter 1 to insert element to queue
Enter 2 to delete element from queue
Enter 3 to display all elements of queue
Enter 4 to exit
Enter your choice :
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative