QUEUE IMPLEMENTATION USING ARRAY IN C [QUEUE IMPLEMENTATION IN C WITH ALGORITHM AND EXPLANATION]
WHAT IS QUEUE?
Queue is an abstract data structure, somewhat similar to Stacks. 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.
Queue Representations
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.
QUEUE Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues −
- enqueue() − add (store) an item to the queue.
- dequeue() − remove (access) an item from the queue.
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.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear poin
Algorithm for Delete insertion
Step 1: If REAR >= SIZE – 1 then
Write “Queue is Overflow”
Step 2: REAR = REAR + 1
Step 3: QUEUE [REAR] = X
Step 4: If FRONT = -1 then
FRONT = 0
Algorithm for Delete Operation
Step 1: If FRONT = -1 then
Write “Queue is Underflow”
Step 2: Return QUEUE [FRONT]
Step 3: If FRONT = REAR then
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1
C Program to Implement a Queue using an Array
This is a C Program to Implement a queue using array.
- About program
This C program implements the queue operations using array, here we are using array to implement queue , queue work in the principal of[FIFO] {FIRST IN FIRST OUT} , so in this program we implement the queue program in some easy steps
Program Solving:-
1. here we are using three functions for three operations like insert, delete and display, and when user presses the respective digit it will call automatically and then perform it's task.
2.here programs Use switch statement to access these functions,and it executes automatically when user presses the appropriate number.
3. Exit.
2.here programs Use switch statement to access these functions,and it executes automatically when user presses the appropriate number.
3. Exit.
Program code:-
Here is source code of the C Program to implement a queue using array.
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
int queue[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.push \n");
printf("2.pop \n");
printf("3.print\n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
print();
break;
case 4:
exit(1);
default:
printf("please choose right option \n");
} /*switch end*/
} /*while has been end*/
} /*main() has been end*/
push()
{
int item;
if (rear == MAX - 1)
printf("Queue Overflow occurs\n");
else
{
if (front == - 1)
/*If there is no element in queue initially */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue[rear] = item;
}
} /*push() has been end*/
pop()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow occurs \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue[front]);
front = front + 1;
}
} /*pop() has been end */
print()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
} /*End of print() */
output:-
LET'S UNDERSTAND PROGRAM:-