-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingReferenceVariable.c
More file actions
77 lines (70 loc) · 1.67 KB
/
QueueUsingReferenceVariable.c
File metadata and controls
77 lines (70 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define Queue_size 5
// c function to insert an integer item at rear using reference variables
void insert_rear(int item,int *r,int q[])
{
int x=item;
if(*r==Queue_size-1)
{
printf(" Queue overflow \n");
return;
}
*r=*r+1;
q[*r]=x;
}
// c function to delete an integer item from front using reference variable
void delete_front(int q[],int *f,int *r)
{
if(*f>*r)
{
printf(" Queue is empty \n"); //underflow
return;
}
printf(" Item deleted from queue %d\n",q[(*f)++]);
if(*f>*r)
{
*f=0;
*r=-1;
}
}
//after reading an item ,it may so happen that queue may be empty so immediately after deleting an element if q is empty initialize f=0 , r=-1
void display(int q[],int f,int r)
// c function to display queue contents using reference variables
{
int i;
if(f>r)
{
printf(" Queue is empty\n");
return;
}
printf(" The contents of queue are \n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
void main()
{
int choice,item,f,r,q[5];
f=0;
r=-1;
for(;;)
{
printf(" 1.Insert 2.Delete 3.Display 4.Exit \n Enter choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf(" Enter item to be inserted\n");
scanf("%d",&item);
insert_rear(item,&r,q);
break;
case 2:delete_front(q,&f,&r);
break;
case 3:display(q,f,r);
break;
default: exit(0);
} // end of switch
} // end of for loop
} // end of main