Create a Queue by Array by R4R Team

Queue implementation by Array:
We implement the queue using the Array in C, in which insertion is done at rear side and deletion is done at front side.

program-

#include< stdio.h>
#include< conio.h>
#define MAX 50

int queue[MAX];
int front=-1, rear=-1;

void insert()
{
int n;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Enter the element in queue\n");
scanf("%d",&n);
rear = rear + 1;
queue[rear] = n;
}
}

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[i]);
}
}

void main()
{
int num,i;
printf("Number of element want to enter in queue\n");
scanf("%d",&num);
for(i=0;i< num;i++){
insert();
}
display();
}

output-

Number of element want to enter in queue
5
Enter the element in queue
1
Enter the element in queue
6
Enter the element in queue
4
Enter the element in queue
3
Enter the element in queue
9
Queue is :
1 6 4 3 9

-In this program, we have two function insert() and display().
- insert() : is used to insert the element in queue at rear side
- display() : is used to display all element of queue from front to rear.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!