-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementing_double_ended_queue_using_arrays.c
121 lines (121 loc) · 2.29 KB
/
implementing_double_ended_queue_using_arrays.c
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
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<stdlib.h>
#define max 7
void enqueuef(int q[],int*f,int*r)
{
if(*f==(*r+1)%max)
{
printf("OVERFLOW!!!\n");
return;
}
int a;
printf("Enter the item to be inserted : ");
scanf("%d",&a);
if(*f==-1)
{
*f=*r=0;
}
else
{
*f=((*f-1)+max)%max;
}
q[*f]=a;
}
void enqueuer(int q[],int*f,int*r)
{
if(*f==(*r+1)%max)
{
printf("OVERFLOW!!!!\n");
return;
}
int a;
printf("Enter the element to be inserted : ");
scanf("%d",&a);
if(*f==-1)
{
*f=*r=1;
}
else
{
*r=(*r+1)%max;
}
q[*r]=a;
}
void dequeuef(int q[],int*f,int*r)
{
int a;
if(*f==-1)
{
printf("UNDERFLOW!!!!!\n");
return;
}
else if(*f==*r)
{
a=q[*f];
*f=*r=-1;
}
else
{
a=q[*f];
*f=(*f+1)%max;
}
printf("The element %d is dequeued from front.\n",a);
}
void dequeuer(int q[],int*f,int*r)
{
int a;
if(*r==-1)
{
printf("UNDERFLOW!!!!\n");
return;
}
if(*r==*f)
{
a=q[*r];
*r=*f=-1;
}
else
{
a=q[*r];
*r=((*r-1)%max +max)%max;
}
printf("The element %d is dequeued from rear.\n",a);
}
void traverse(int q[],int*f,int*r)
{
printf("The elements of the queue are : \n");
for(int i=*f;i!=*r;i=(i+1)%max)
{
printf("%d\t",q[i]);
}
printf("%d\n",q[*r]);
}
int main()
{
int *q,c,f=-1,r=-1;
q=(int *)malloc(sizeof(int)*max);
do
{
printf("1-enqueue at front\n2-enqueue at rear\n3-dequeue at front\n4-dequeue at rear\n5-traverse\nEnter any other number for exiting\nEnter your choice :");
scanf("%d",&c);
switch (c)
{
case 1:
enqueuef(q,&f,&r);
break;
case 2:
enqueuer(q,&f,&r);
break;
case 3:
dequeuef(q,&f,&r);
break;
case 4:
dequeuer(q,&f,&r);
break;
case 5:
traverse(q,&f,&r);
break;
}
}
while(c>0&&c<6);
}