-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_using_array.c
115 lines (105 loc) · 2.24 KB
/
queue_using_array.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
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void display();
void insert();
void del();
int data[MAX];
int front = -1;
int rear = -1;
int main(){
int choice,dta;
while(1){
printf("Select option\n\n");
printf("1. DISPLAY queue\n2. INSERT into queue\n3. DELETE from queue\n0.EXIT\n");
scanf("%d",&choice);
switch(choice){
case 1:
display();
break;
case 2:
printf("Enter data: ");
scanf("%d", &dta);
insert(dta);
break;
case 3:
del();
break;
case 0:
exit(0);
break;
};
}
return 0;
}
void insert(int data_add){
if(rear == MAX-1){
printf("ERROR: OVERFLOW, cannot insert element\n");
return; //failsafe
}
else{
if(front == -1 && rear == -1){
front = 0;
rear = 0;
}
else{
++rear;
}
data[rear] = data_add;
printf("\nINSERTION SUCCESSFUL\n");
}
}
void del(){
int confirm;
if (front == -1 && rear == -1){
printf("ERROR: UNDERFLOW, no data to delete\n");
return;
}
else{
if(front == rear){
printf("DATA TO DELETE %d. press 1 to confirm. \n",data[front]);
scanf("%d",&confirm);
if(confirm == 1){
front = rear = -1;
}
else{
printf("OPERATION CANCELLED\n\n");
return;
}
}
else{
printf("DATA TO DELETE %d. press 1 to confirm. \n",data[front]);
scanf("%d",&confirm);
if(confirm == 1){
++front;
}
else{
printf("OPERATION CANCELLED\n\n");
return;
}
}
}
}
void display(){
int i;
if(rear == -1){
printf("ERROR: EMPTY QUEUE\n");
}
else{
printf("data in the queue\n");
for(i=front;i<=rear;++i){
if(i == front && i == rear){
printf("%d <--- FRONT <---REAR\n", data[i]);
}
else if(i == front){
printf("%d <--- FRONT\n", data[i]);
}
else if(i == rear){
printf("%d <--- REAR\n", data[i]);
}
else{
printf("%d\n",data[i]);
}
}
}
}