11. Write a C program to simulate the working of linear Queue using an array.
Provide the operations QINSERT, QDELETE and QDISPLAY. Check the queue
status for empty and full.
#include<stdio.h>
#include<conio.h>
void qinsert();
void qdelete();
void qdisplay();
int queue[10],front=0,rear=-1;
int max=5;
void main()
{
int ch;
do
{
printf("\nLINEAR QUEUE OPERATIONS \n");
printf("1.Insert \n");
printf("2.Delete \n");
printf("3.Display \n");
printf("4.Exit \n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:qdisplay();
break;
case 4:exit(0);
default:printf("\n WRONG CHOICE");
}
}
while(ch!=4);
}
void qinsert()
{
int item;
if(rear==max-1)
printf("Queue is full");
else
{
printf("Enter the value to insert\n");
scanf("%d",&item);
rear++;
queue[rear]=item;
}
}
void qdelete()
{
int item;
if(front==rear+1)
printf("Queue is empty");
else
{
item=queue[front];
printf("%d is deleted\n",item);
front++;
}
}
void qdisplay()
{
int item;
int p=front;
if(p==rear+1)
printf("Queue is empty");
else
{
printf("\nQueue Elements\n");
while(p<=rear)
{
printf("%d\t",queue[p]);
p++;
}
}
}
Algorithm : Linear Queue
queue [MAX] is an integer array. MAX gives the maximum capacity of the queue. front is the
index of the first element .Rear is the index of the last element.
Step1: Start
Step2: Display menu and accept user choice
Step3: If choice=1
Call the function qinsert() to insert a new element to the circular queue
End if
Step4: If choice=2
Call the function qdelete() to delete an element from the circular queue
End if
Step5: If choice=3
Call the function qdisplay() to print the elements in the circular queue.
End if
Step6: If choice=4
Exit the program
End if
Step 7: Stop
Algorithm of qinsert()
Step 1:if(rear==max-1)
Display"Queue is full");
Step 2:else Input the value to insert(item)
Step 3:rear++;
Step 4 :queue[rear]=item;
End if in step 1
Algorithm of qdelete()
Step 1:if(front==rear+1)
Output “Queue is empty"
Step 2:else
item=queue[front];
Output “Deleted item “
Step 3:front++
End if in step 1
Algorithm of qdisplay()
Step 1:p=front;
Step 2:if(p==rear+1)
Display ”Queue is empty"
Step 3:else while(p<=rear)
Step 4:Output queue[p]
Step 5: p++
End if of step1.