-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.c
166 lines (156 loc) · 4.27 KB
/
4.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
typedef struct node *NODE;
NODE getnode();
NODE insert_front(NODE);
NODE delete_front(NODE);
NODE insert_rear(NODE);
NODE delete_rear(NODE);
void display(NODE);
void main() {
NODE first = NULL;
int option;
clrscr();
while (1) {
printf("\nDouble-ended queue operations\n");
printf("1. Entry-restricted queue\n2. Exit-restricted queue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
printf("\nEntry-restricted operations\n");
printf("1. Insert\n2. Delete from front\n3. Delete from rear\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
first = insert_rear(first);
getch();
clrscr();
break;
case 2:
first = delete_front(first);
getch();
clrscr();
break;
case 3:
first = delete_rear(first);
getch();
clrscr();
break;
default:
printf("Invalid choice\n");
}
break;
case 2:
printf("\nExit-restricted operations\n");
printf("1. Delete\n2. Insert from front\n3. Insert from rear\n");
printf("Enter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
first = delete_front(first);
getch();
clrscr();
break;
case 2:
first = insert_front(first);
getch();
clrscr();
break;
case 3:
first = insert_rear(first);
getch();
clrscr();
break;
default:
printf("Invalid choice\n");
}
break;
case 3:
display(first);
getch();
clrscr();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
}
NODE getnode() {
NODE temp;
temp = (NODE)malloc(sizeof(struct node));
return temp;
}
NODE insert_front(NODE first) {
NODE temp;
temp = getnode();
printf("Enter the data: ");
scanf("%d", &temp->data);
temp->link = first;
first = temp;
printf("%d is successfully inserted\n", temp->data);
return first;
}
NODE delete_front(NODE first) {
NODE temp;
if (first == NULL) {
printf("No nodes present");
return first;
}
temp = first;
first = first->link;
printf("%d is successfully deleted\n", temp->data);
free(temp);
return first;
}
NODE insert_rear(NODE first) {
NODE temp, ptr;
temp = getnode();
printf("Enter the data: ");
scanf("%d", &temp->data);
printf("%d is successfully inserted\n", temp->data);
temp->link = NULL;
if (first == NULL) {
first = temp;
return first;
}
ptr = first;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = temp;
return first;
}
NODE delete_rear(NODE first) {
NODE ptr, prev;
if (first == NULL) {
printf("No nodes present\n");
return first;
}
ptr = first;
while (ptr->link != NULL) {
prev = ptr;
ptr = ptr->link;
}
printf("%d is successfully removed\n", ptr->data);
prev->link = NULL;
free(ptr);
return first;
}
void display(NODE first) {
NODE temp;
if (first == NULL)
printf("No nodes present\n");
else {
for (temp = first; temp->link != NULL; temp = temp->link)
printf("%d=>", temp->data);
printf("%d=>", temp->data);
}
}