Description: In this program we have to implement the Queue operation by using the arrays. Here they Queue operation are push and pop. Push operation is used to insert the elements into a Queue and pop operation is used to remove the elements in to a Queue.
ALGORITHM FOR INSERTING AN ELEMENT IN TO A QUEUE:
Function QINSERET(Q,F,R,N,Y)
Step 1: [overflow]
If R>=N
Then printf(“ overflow”)
Return
Step 2: [Increment rear pointer]
R=R+1
Step 3: [ Insert element]
Q[R]=y
Step 4: [Is front pointer properly set?]
If F=0
Then f=1
Return
ALGORITHM FOR DELETING AN ELEMENT FROM A STACK:
Function QDELETE(Q,F,R)
Step 1: [Underflow]
If F=0
Then printf(“Queue underflow”)
Return(0)
Step 2: [Delete element]
Y=q[f]
Step 3: [Is Queue Empty?]
If F=R
Then F=R=0
Else
F=F+1
Step 4:[Return element]
Return(r)
Program:
# include <stdio.h>
# define size 4
int front=0,rear=-1,item,choice,a[size];
main()
{
clrscr();
while(1)
{
printf("*** MENU ***\n 1. INSERTION\n 2. DELETION\n
3.TRAVERSE\n 4. EXIT\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:insertion();
break;
case 2:deletion();
break;
case 3:traverse();
break;
case 4:exit();
default:printf("*** wrong choice ***\n");
}
}
getch();
}
insertion()
{
if(rear==size-1)
printf("*** queue is full ***\n");
else
{
printf("enter item into queue:");
scanf("%d",&item);
rear++;
a[rear]=item;
}
}
deletion()
{
if(front==rear+1)
printf("*** queue is empty ***\n");
else
{
item=a[front];
front++;
printf("the deleted item from queue is %d\n",item);
}
}
traverse()
{
int i;
if(front==rear+1)
printf("*** queue is empty ***\n");
else
{
for(i=front;i<=rear;i++)
if(i==front && rear==i)
printf("%d at %d ->front=rear\n",a[i],i);
else
if(i==rear)
printf("%d at %d ->rear\n",a[i],i);
else
if(i==front)
printf("%d at %d ->front\n",a[i],i);
else
printf("%d at %d\n",a[i],i);
}
}
Input/Output:
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:1
enter item into queue:11
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:1
enter item into queue:12
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:1
enter item into queue:13
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:1
enter item into queue:14
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:1
*** queue is full ***
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:3
11 at 0 ->front
12 at 1
13 at 2
14 at 3 ->rear
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:2
the deleted item from queue is 11
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:2
the deleted item from queue is 12
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:2
the deleted item from queue is 13
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:2
the deleted item from queue is 14
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:2
*** queue is empty ***
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:3
*** queue is empty ***
*** MENU ***
1. INSERTION
2. DELETION
3. TRAVERSE
4. EXIT
enter your choice:4
conclusion: the program is error free
VIVA QUESATIONS:
1) Define queue ?
Ans: A queue is a linear, sequential list of that are accessed in the oeder first in first out(FIFO).
2) Define circular queues ?
Ans: A queue can also be circular in which case, it is called as a circular queue
3) What are the various stack oriented notations ?
Ans: i) infix ii) prefix iii) postfix
No comments:
Post a Comment