11. Program for
implementing queue in an array?
Algorithm
Globally,define MAX as 3 and
declare variables front=-1,rear=-1,queue[MAX].
Step 1:Start
Step 2:Declare variables
ch,i,c.
Step 3:While c==’y’ repeat
steps 3.1 to 3.7
3.1)Print the menu
1.Insertion 2.Deletion 3.Display
3.2)Input and read the
choice,ch.
3.3)If ch=1,call the
function insert().
3.4)Else if ch=2,call the
function del().
3.5)Else if ch=3,call the
function display().
3.6)Else print wrong choice.
3.7)Input and read the
character of continuing.
Step 4:End while.
Step 5:Stop.
Function insert()
Step 1:Declare variable,item.
Step 2: If rear =MAX-1 then
2.1)Print Queue Overflow.
Step 3:Else
3.1)If front=-1 and rear=-1
then
a)Increment front
by 1.
3.2)End if.
3.3)Increment rear by 1.
3.4)Input and read the
element to be inserted,item.
3.5)Assign queue[rear]=item.
Step 4:End if.
Step 5:Return.
Function del()
Step 1:If (front=-1 and
rear=-1) or (front>rear) then
1.1)Print Queue Underflow.
Step 2:Else
2.1)Print the deleted
element,queue[front].
2.2)Increment front by 1.
Step 3:End if.
Step 4:Return.
Function display()
Step 1:Declare variable,i.
Step 2:Initialize i as front.
Step 3:While(i<=rear)
repeat steps 3.1 to 3.2
3.1)Print elements in the
queue,queue[i].
3.2)Increment i by 1.
Step 4:End while.
Step 5:Return.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 3
int queue[MAX],front=-1,rear=-1;
void insert()
{
int item;
if(rear==MAX-1)
printf("\tQueue Overflow\n");
else
{
if((front==-1)&&(rear==-1))
front=front+1;
rear++;
printf("Enter the element to be inserted\n");
scanf("%d",&item);
queue[rear]=item;
}
}
void del()
{
if((front==-1)&&(rear==-1)||(front>rear))
printf("\tQueue underflow\n");
else
{
printf("The deletd element is %d\n",queue[front]);
front=front+1;
}
}
void display()
{
int i;
printf("The elements of the queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
void main()
{
int ch,i;
char c;
clrscr();
do
{
printf("Enter the choice\t1.Insertion\t2.Deletion\t3.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
default:
printf("\nWRONG CHOICE\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
getch();
}
Output
Enter the choice 1.Insertion 2.Deletion 3.Display
1
Enter the element to be inserted
11
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
1
Enter the element to be inserted
12
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
1
Enter the element to be inserted
13
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
3
The elements of the queue are
11 12 13
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
1
Queue Overflow
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
2
The deletd element is 11
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
3
The elements of the queue are
12 13
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
2
The deletd element is 12
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
3
The elements of the queue are
13
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
2
The deletd element is 13
Do you want to continue:(y/n)?
y
Enter the choice 1.Insertion 2.Deletion 3.Display
2
Queue underflow
Do you want to continue:(y/n)?
n
No comments:
Post a Comment