-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDE_queue1.c
More file actions
150 lines (134 loc) · 3.13 KB
/
Copy pathDE_queue1.c
File metadata and controls
150 lines (134 loc) · 3.13 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Output restricted DE_Queue
#include<stdio.h>
#include<stdlib.h>
struct DE_Queue{
int size;
int front;
int rear;
int *arr;
};
// struct DE_Queue*q=NULL;
int isEmpty(struct DE_Queue*q){
if(q->front==-1 || q->front>q->rear) return 1;
return 0;
}
int isFullfront(struct DE_Queue*q){
if((q->front)==-1) return 1;
else return 0;
}
int isFullrear(struct DE_Queue*q){
if(q->rear==q->size-1) return 1;
else return 0;
}
void enqueuefront(struct DE_Queue*q,int val){
if(isFullfront(q)){
printf("Insertion not posssible at front\n");
}
else{
// if(q->front==-1 && q->rear==-1){
// q->rear=0;
// q->front++;
// q->arr[q->front]=val;
// }
// if(q->front!=q->size-1){
// q->front
// }
// else{
q->front--;
q->arr[q->front]=val;
// }
}
}
void enqueuerear(struct DE_Queue*q,int val){
if(isFullrear(q)){
printf("Insertion not possible at rear end\n");
}
else{
if(q->front==-1){
q->front=0;
}
q->rear++;
q->arr[q->rear]=val;
}
}
void dequeuefront(struct DE_Queue*q){
if(isEmpty(q)){
printf("Queue underflow\n");
}
else{
q->front++;
}
}
void dequeuerear(struct DE_Queue*q){
printf("Deletion at rear end restricted");
}
void display(struct DE_Queue*q){
int i;
for(i=q->front;i<=q->rear;i++){
printf("%d ",q->arr[i]);
}
printf("\n");
}
int main(){
struct DE_Queue* q=(struct DE_Queue*)malloc(sizeof(struct DE_Queue));
q->size=5;
q->front=q->rear=-1;
q->arr=(int*)malloc(q->size*sizeof(int));
while(1){
int choice;
printf("1.Enqueuefront\n");
printf("2.Enqueuerear\n");
printf("3.Dequeuefront\n");
printf("4.Dequeuerear\n");
printf("5.Display\n");
printf("5.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int val;
printf("Enter a value to enqueue:");
scanf("%d",&val);
enqueuefront(q,val);
printf("Successfully Enqueued\n");
break;
}
case 2:
{
int val;
printf("Enter a value to enqueue:");
scanf("%d",&val);
enqueuerear(q,val);
printf("Successfully Enqueued\n");
break;
}
case 3:
{
dequeuefront(q);
printf("Successfully dequeued\n");
break;
}
case 4:
{
dequeuerear(q);
break;
}
case 5:
{
display(q);
break;
}
case 6:
{
printf("Exiting the program");
exit(0);
break;
}
default:
printf("Not a valid choice");
}
}
return 0;
}