-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueueUsingGlobalVariable.c
More file actions
71 lines (68 loc) · 1.48 KB
/
CircularQueueUsingGlobalVariable.c
File metadata and controls
71 lines (68 loc) · 1.48 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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define Queue_size 5
int choice,item,f,r,q[5],count;
// c function to insert an integer item at rear using global variables
void insert_rear()
{
if(count==Queue_size)
{
printf(" Queue overflow \n");
return;
}
r=(r+1)%Queue_size;
q[r]=item;
count++;
}
// c function to delete an integer item from front using global variable
void delete_front()
{
if(count==0)
{
printf(" Queue is empty \n"); //underflow
return;
}
printf(" Item deleted from queue %d\n",q[f]);
f=(f+1)%Queue_size;
count--;
}
// c function to display queue contents using global variables
void display()
{
int i;
if(count==0)
{
printf(" Queue is empty\n");
return;
}
printf(" The contents of queue are \n");
for(i=0;i<count;i++)
{
printf("%d\n",q[f]);
f=(f+1)%Queue_size;
}
}
void main()
{
f=0;
r=-1;
count=0;
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