-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue using linked list.c
102 lines (91 loc) · 2.39 KB
/
queue using linked list.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
#include <stdio.h>
#include <stdlib.h>
// Define a node structure
struct node {
int data;
struct node *next;
};
// Define front and rear pointers
struct node *front = NULL, *rear = NULL;
// Function to insert element in queue
void enqueue() {
int item;
printf("\nEnter the element to be inserted: ");
scanf("%d", &item);
// Allocate memory for new node
struct node *new_node = (struct node*)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Memory allocation failed.\n");
return;
}
new_node->data = item;
new_node->next = NULL;
// Insert the node into the queue
if (rear == NULL) {
// Queue is empty
front = rear = new_node;
} else {
// Queue is not empty
rear->next = new_node;
rear = new_node;
}
}
// Function to delete element from queue
void dequeue() {
if (front == NULL) {
printf("\nQueue is empty.\n");
return;
}
// Remove the node from the front of the queue
struct node *temp_node = front;
front = front->next;
if (front == NULL) {
rear = NULL; // Queue is now empty
}
printf("\nDeleted element is %d\n", temp_node->data);
free(temp_node);
}
// Function to display elements of queue
void display() {
if (front == NULL) {
printf("\nQueue is empty.\n");
return;
}
// Display the elements of the queue
struct node *temp_node = front;
printf("\nElements in the queue are: ");
while (temp_node != NULL) {
printf("%d ", temp_node->data);
temp_node = temp_node->next;
}
printf("\n");
}
int main() {
int choice;
while(1) {
printf("\nQueue Menu\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\nExiting...\n");
exit(0);
default:
printf("\nInvalid choice.\n");
}
}
return 0;
}