-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue2.c
122 lines (102 loc) · 2.98 KB
/
queue2.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
//stack using link list
#include <stdio.h>
#include <stdlib.h>
// Node structure for the linked list
struct Node {
int data;
struct Node* next;
};
// Function to initialize the queue
void initQueue(struct Node** front, struct Node** rear) {
*front = NULL;
*rear = NULL;
}
// Function to check if the queue is empty
int isEmpty(struct Node** front) {
return *front == NULL;
}
// Function to add an element to the queue (enqueue)
void enqueue(struct Node** front, struct Node** rear, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*rear == NULL) {
// If the queue is empty, both front and rear are set to the new node
*front = newNode;
} else {
// Add the new node at the end of the queue
(*rear)->next = newNode;
}
*rear = newNode; // Move rear to point to the new node
printf("%d enqueued to the queue\n", value);
}
// Function to remove an element from the queue (dequeue)
void dequeue(struct Node** front, struct Node** rear) {
if (isEmpty(front)) {
printf("Queue is empty! Cannot dequeue\n");
} else {
struct Node* temp = *front;
int value = temp->data;
printf("Dequeued element: %d\n", value);
*front = (*front)->next; // Move front to the next node
free(temp); // Free the dequeued node
// If the queue is now empty, reset rear
if (*front == NULL) {
*rear = NULL;
}
}
}
// Function to view the front element (peek)
void peek(struct Node** front) {
if (isEmpty(front)) {
printf("Queue is empty!\n");
} else {
printf("Front element is: %d\n", (*front)->data);
}
}
// Function to display all elements in the queue
void display(struct Node** front) {
if (isEmpty(front)) {
printf("Queue is empty!\n");
} else {
struct Node* current = *front;
printf("Queue elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
}
int main() {
struct Node* front;
struct Node* rear;
// Initialize the queue
initQueue(&front, &rear);
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(&front, &rear, value);
break;
case 2:
dequeue(&front, &rear);
break;
case 3:
peek(&front);
break;
case 4:
display(&front);
break;
case 5:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}