-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDE_queue2.c
More file actions
137 lines (120 loc) · 2.74 KB
/
Copy pathDE_queue2.c
File metadata and controls
137 lines (120 loc) · 2.74 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
// Input 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 isEmptyfront(struct DE_Queue*q){
if(q->front==-1 || q->front>q->rear) return 1;
return 0;
}
int isEmptyrear(struct DE_Queue*q){
if(q->rear==-1) return 1;
return 0;
}
int isFull(struct DE_Queue*q){
if(q->rear==q->size-1) return 1;
else return 0;
}
void enqueuefront(struct DE_Queue*q){
printf("Insertion is restricted at the front end\n");
}
void enqueuerear(struct DE_Queue*q,int val){
if(isFull(q)){
printf("Queue Overflow\n");
}
else{
if(q->front==-1){
q->front=0;
}
q->rear++;
q->arr[q->rear]=val;
}
}
void dequeuefront(struct DE_Queue*q){
if(isEmptyfront(q)){
printf("Queue underflow\n");
}
else{
q->front++;
}
}
void dequeuerear(struct DE_Queue*q){
if(isEmptyrear(q)){
printf("Queue underflow\n");
}
else{
q->rear--;
}
}
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:
{
enqueuefront(q);
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);
printf("Successfully dequeued\n");
break;
}
case 5:
{
display(q);
break;
}
case 6:
{
printf("Exiting the program");
exit(0);
break;
}
default:
printf("Not a valid choice");
}
}
return 0;
}