-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingGlobalVariable.c
More file actions
73 lines (70 loc) · 1.6 KB
/
QueueUsingGlobalVariable.c
File metadata and controls
73 lines (70 loc) · 1.6 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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define Queue_size 5
int choice,item,f,r,q[5];
// c function to insert an integer item at rear using global variables
void insert_rear()
{
if(r==Queue_size-1)
{
printf(" Queue overflow \n");
return;
}
r=r+1;
q[r]=item;
//line 13,14 can also be written as q[++r]=item;
}
// c function to delete an integer item from front using global variable
void delete_front()
{
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
// c function to display queue contents using global variables
void display()
{
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()
{
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();
break;
case 2:delete_front();
break;
case 3:display();
break;
default: exit(0);
} // end of switch
} // end of for loop
} // end of main