-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeueUsingGlobalVariable.c
More file actions
114 lines (107 loc) · 2.63 KB
/
DequeueUsingGlobalVariable.c
File metadata and controls
114 lines (107 loc) · 2.63 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//Double Ended Queue or Dequeue
#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 the front end using global variables
void insert_front()
{
if(f==0 && r==-1)
{
q[++r]=item; //case 1: when queue is empty,item can be inserted at front end by incrementing r by 1 and then insert the item
return;
}
if(f!=0)
{
q[--f]=item; //case 2:some items are deleted
return;
}
printf(" Insertion not possible \n"); //case 3:some items are inserted (not deleted)
}
// 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 delete an integer item from rear end using global variables
void delete_rear()
{
if(f>r)
{
printf("Queue is empty \n");
return;
}
printf(" The element deleted is %d\n",q[r--]);
if(f>r)
{
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 at rear 2.Insert at front \n 3.Delete at rear 4.Delete at front \n 5.Display 6.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:printf(" Enter item to be inserted\n");
scanf("%d",&item);
insert_front();
break;
case 3:delete_rear();
break;
case 4:delete_front();
break;
case 5:display();
break;
default: exit(0);
} // end of switch
} // end of for loop
} // end of main