Repository navigation
Expand file tree
/
Copy pathQueue.c
More file actions
111 lines (96 loc) · 1.95 KB
/
Copy pathQueue.c
File metadata and controls
111 lines (96 loc) · 1.95 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
//
// Created by Prince on 21-08-2024.
//
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* link;
} *front, * rear;
void enqueue(const int value) {
struct Node* ptr = malloc(sizeof(struct Node));
if (ptr == NULL) {
printf("\nMemory allocation failed.\n");
return;
}
ptr->data = value;
ptr->link = NULL;
if (rear != NULL) {
rear->link = ptr;
}
else {
front = ptr;
}
rear = ptr;
}
void dequeue() {
if (front != NULL) {
struct Node* ptr = front;
printf("\nElement dequeued is: [%d]", front->data);
front = front->link;
if (front == NULL) {
rear = NULL;
}
free(ptr);
}
else {
printf("\nQueue is empty.");
}
}
void displayQueue() {
if (front != NULL) {
const struct Node* ptr = front;
printf("\nElements: ");
while (ptr != NULL) {
printf("[%d] ", ptr->data);
ptr = ptr->link;
}
}
else {
printf("\nQueue is empty.");
}
}
void displayMenu() {
printf("\nChoose operation:\n");
printf("[1] Enqueue\n");
printf("[2] Dequeue\n");
printf("[3] Display\n");
printf("[4] Exit\n");
printf("Enter the option: ");
}
int main() {
front = NULL, rear = NULL;
int option, value, elements;
do {
displayMenu();
scanf("%d", &option);
switch (option) {
case 1:
printf("\nEnter the number of elements to be enqueued: ");
scanf("%d", &elements);
for (int i = 0; i < elements; i++) {
printf("\nEnter #%d number: ", i + 1);
scanf("%d", &value);
enqueue(value);
}
break;
case 2:
printf("\nEnter the number of elements to be dequeued: ");
scanf("%d", &elements);
for (int i = 0; i < elements; i++) {
dequeue();
}
break;
case 3:
displayQueue();
break;
case 4:
printf("\nExiting program ...\n");
break;
default:
printf("\nInvalid input.");
break;
}
} while (option != 4);
return 0;
}