Program - Create Circular
queue using Array
Algorithm
: Insertion in a queue
Step-1 : [Check for overflow]
if front=0 and rear=size-1
Output : “ Queue is Overflow”
return
Step-2: [check for overflow when rear is less than
front]
else if rear=front-1
Output : “ Queue is Overflow”
return
Step-3 : [insert element in queue]
else if front<0
front=0;
rear=0;
q[rear]=no;
Step-4 : [Check if rear at the end of
the queue)
else if rear=size-1
rear=0;
q[rear]=no;
Step-5 : [Insert element]
else
rear=rear+1;
if rear=front
Output
“Queue is overflow “
else
q[rear]=no;
Step-6 : return
Algorithm
: Deletion in a queue
Step -1 : [Check for underflow]
if
front<0
Output”Underflow”
return
Step-2 : [Remove the element]
no=q[front];
q[front]=NULL;
Output
“Element deleted : ",no
Step-3 : [Check whether the queue is empty or
not]
if front=rear
front=-1;
rear=-1;
Step-4 : [Check for the front pointer position
else if front = size-1
front=0;
else
front=front+1;
Step- 5 : Return
Program
//Circular
Queue
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define size 6
int rear, front;
int no;
int q[size];
int rear=-1;
int front=-1;
/* Function to create queue */
void Insert_q()
{
printf("\n Input the Element
: ");
scanf("%d",&no);
if((front==0) && (rear==size-1))
{
printf("\n Queue is Overflow");
return;
}
else if(rear==front-1)
{
printf("\n Queue is overflow");
return;
}
else if(front<0) /* Insert First Element */
{
front=0;
rear=0;
q[rear]=no;
}
else if(rear==size-1)
{
rear=0;
q[rear]=no;
}
else
{
rear++;
if(rear==front)
{
printf("\n Queue is overflow
");
return;
}
else
{
q[rear]=no;
}
}
}
/* Function to perform delete
operation */
void Delete_q()
{
if(front<0)
{
printf("\n Queue is Underflow");
return;
}
no=q[front];
q[front]=NULL;
printf("\n Element deleted : ",no);
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==size-1)
{
front=0;
}
else
{
front++;
}
}
/* Output function */
void Display_q()
{
int i;
if(front<0)
{
printf("\n Queue is underflow");
return;
}
if(rear>=front)
{
for(i=front; i<=rear; i++)
{
printf("\n q[%d] = %d",i,q[i]);
}
}
else
{
for(i=front;i<size;i++)
{
printf("\n l-2 q[%d] =
%d",i,q[i]);
}
for(i=0;i<=rear;i++)
{
printf("\n l-3 q[%d] =
%d",i,q[i]);
}
}
}
/* Function main */
void main()
{
int choice;
do
{
clrscr();
printf("\t Menu");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display ");
printf("\n 4. Exit");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
Insert_q();
Display_q();
getch();
break;
case 2:
Delete_q();
Display_q();
getch();
break;
case 3:
Display_q();
getch();
break;
case 4:
printf("End
of Program");
getch();
exit(0);
}
}while(choice!=4);
}
Posted By : Ruchita Pandya
No comments:
Post a Comment