-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.b.c
87 lines (72 loc) · 1.62 KB
/
8.b.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
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* front = NULL;
Node* rear = NULL;
void enqueue(int x) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = x;
temp->next = NULL;
if (front == NULL && rear == NULL) {
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return 0;
}
Node* temp = front;
if (front == rear) {
front = rear = NULL;
} else {
front = front->next;
}
int x = temp->data;
free(temp);
return x;
}
void printQueue() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Queue: ");
Node* temp = front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main() {
int choice, data;
while(1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Print\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the value to be enqueued: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
printf("Dequeued value: %d\n", dequeue());
break;
case 3:
printQueue();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}