Queue Implementation Using Array & Linked List in C
This is the C Program to implement queue using array and linked list in c
A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later.
![]()
Using Array:
#include<stdio.h>
int arr[100],front=-1,rear=-1;
main()
{
int ch;
printf("nEnter n1.INSERTn2.DELETEn3.DISPLAYn4ENDn");
scanf("%d",&ch);
while(ch!=4)
{
switch(ch)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:
break;
}
printf("Input Option?n");
scanf("%d",&ch);
}
}
insert()
{
if(front==-1&&rear==-1){ front++; rear++; scanf("%d",&arr[rear]);
return;
}
if(rear==99)
{
printf("Queue Overflown");
return;
}
else
{
rear++;
scanf("%d",&arr[rear]);
return;
}
}
delete()
{
if(front>=0)
{
printf("Deleted Element %dn",arr[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
}
else
printf("Queue Underflown");
}
display()
{
int i;
if(front==-1&&rear==-1)
{
printf("Queue Underflown");
return;
}
else
{
printf("ttQueue");
printf("n___________________________________________________n");
for(i=front;i<=rear;i++)
{
printf("%d | ",arr[i]);
}
printf("n___________________________________________________n");
}
}
Using Linked List:
#include<stdio.h>
typedef struct N
{
int data;
struct N *next;
}QU;
QU *pos,*front,*rear;
main()
{
int ch;
front=NULL;
rear=NULL;
printf("nEnter any Optionn1.ENQUEUEn2.DEQUEUEn3.DISPLAYn4.EXITn");
scanf("%d",&ch);
while(ch!=4)
{
switch(ch)
{
case 1:enqueue(); break;
case 2:dequeue(); break;
case 3:display(); break;
case 4: break;
}
printf("nChoose Any Optionn");
scanf("%d",&ch);
}
}
enqueue()
{
int ele;
printf("n");
scanf("%d",&ele);
QU *newnode;
newnode=(QU*)malloc(sizeof(QU));
newnode->data=ele;
newnode->next=NULL;
if(front==NULL&&rear==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
dequeue()
{
QU *temp;
temp=front;
if(front==0&&rear==0){printf("Queue Emptyn"); return; }
if(front==rear){temp=front;front=NULL,rear=NULL;free(temp);return;}
else printf("%d - DEQUEUEDn",temp->data); front=front->next; free(temp);
}
display()
{
QU *jp;
jp=front;
if(front==NULL&&rear==NULL){printf("Queue Emptyn");}
else while(jp!=NULL){ printf("%dn",jp->data); jp=jp->next; }
}
Recent Comments