IMPLEMENTATION OF LINEAR QUEUE USING ARRAYS
Aim
To write a C-program to implement linear
queue data structure using arrays.
Algorithm
STEP 1: Start
STEP 2: [Include all header files]
STEP 3: [Declare the variables]
STEP 4: [If n->1 call the function
Enqueue( )]
STEP 5: [If n->2 call the function
Dequeue( )]
STEP 6: [If n->3 call the function
Peep( )]
STEP 7: [If n->4 call the function
Size( )]
STEP 8: [If n->5 call the function
View( )]
STEP 9: [else Exit( )]
STEP 10: Stop
Algorithm for Enqueue( )
STEP 1: If[front= =rear]
Initialize front=rear=0
STEP 2: else rear=(rear+1)% qsize
Set queue[rear] =value
[return]
Algorithm for Dequeue( )
STEP 1: If[front = =rear]
1.1: temp=queue[front]
1.2: Initialize front=rear=-1
STEP 2:else
2.1: front=(front+1)% qsize
[return]
Algorithm for Peep( )
STEP 1:If [front= =rear]
STEP 1.1: temp=queue[front]
[return]
Algorithm for Size( )
STEP 1:If [front= =rear]
1.1: Set f=front
1.2: Set count=1
STEP 2: If [front!=rear]
2.1: front=(front+1)%qsize
2.2: set count=count+1
[return]
Algorithm for View( )
STEP 1: If [front = =rear]
Write (“Queue is empty”)
STEP 2: else
[display elements]
Coding:
#include<stdio.h>
#include<conio.h>
#define size 15
int queue[size],front=0,rear=0,b;
int res;
void enqueue();
void dequeue();
void display();
void main()
{
int
c;
clrscr();
printf("\n1.Insertion\n2.Deletion\n3.Display");
do
{
printf("\n\nEnter
your Choice :: ");
scanf("%d",&c);
switch(c)
{
case
1:
enqueue();
break;
case
2:
dequeue();
break;
case
3:
printf("\n\nContents
of queue is \t");
display();
break;
default:
printf("\nInvalid
Choice......");
exit(0);
}
}while(c<4);
getch();
}
void enqueue()
{
if(rear>=size)
{
printf("\nOverflow");
return;
}
else
{
printf("\nEnter
the number to be entered :: ");
scanf("%d",&b);
rear++;
queue[rear]=b;
printf("\nNumber
pushed is %d",queue[rear]);
if(front==0)
front=1;
return;
}
}
void dequeue()
{
if(front==0)
{
printf("\nUnderflow");
return;
}
else
{
res=queue[front];
if(front==rear)
{
front=0;
rear=0;
}
else
front++;
}
printf("\nDeleted
element is %d",res);
return;
}
void display()
{
int
i;
if(front==0)
{
printf("\nUnderflow");
return;
}
for(i=front;i<=rear;i++)
printf("%d
, ",queue[i]);
}
Output:
1.Insertion
2.Deletion
3.Display
Enter your Choice :: 1
Enter the number to be entered :: 12
Number pushed is 12
Enter your Choice :: 1
Enter the number to be entered :: 2
Number pushed is 2
Enter your Choice :: 3
Contents of queue is 12 , 2 ,
Enter your Choice :: 2
Deleted element is 12
Enter your Choice :: 3
Contents of queue is 2,
No comments:
Post a Comment