-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority-queue.c
91 lines (83 loc) · 2.07 KB
/
priority-queue.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
// Program to implement priority queue
#include<stdio.h>
#include<stdlib.h>
struct node {
int data, priority;
struct node *next;
};
void enqueue(struct node **front) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &newNode -> data);
printf("Enter the priority of the number: ");
scanf("%d", &newNode -> priority);
newNode -> next = NULL;
if(*front==NULL || newNode->priority <= (*front)-> priority) {
newNode -> next = *front;
*front = newNode;
} else {
struct node *temp = *front;
while (temp -> next!=NULL && temp ->next->priority <= newNode-> priority)
temp = temp -> next;
newNode -> next = temp -> next;
temp -> next = newNode;
}
}
void dequeue(struct node **front, struct node **rear) {
if((*front == NULL)&& (*rear==NULL)) {
printf("Queue is empty");
} else {
struct node *temp = *front;
printf("\nThe deleted item is %d", (*front)->data);
*front=(*front)->next;
free(temp);
}
}
void display(struct node *front, struct node *rear) {
struct node *temp = front;
if((front==NULL) && (rear == NULL))
printf("Queue is empty");
else {
printf("\nThe list is : \n| ");
while(temp != NULL) {
printf("%d - %d | ", temp -> data, temp -> priority);
temp = temp -> next;
}
}
}
int main() {
int n;
printf("Enter the size of the items: ");
scanf("%d", &n);
struct node *front = NULL, *rear = NULL;
for(int i =1; i<= n; i++) {
enqueue(&front);
}
printf("\nBefore the deletion");
display(front, rear);
dequeue(&front, &rear);
printf("\nAfter the deletion");
display(front, rear);
return 0;
}
// Output
/*
Enter the size of the items: 5
Enter the data: 34
Enter the priority of the number: 2
Enter the data: 56
Enter the priority of the number: 1
Enter the data: 44
Enter the priority of the number: 5
Enter the data: 23
Enter the priority of the number: 3
Enter the data: 60
Enter the priority of the number: 4
Before the deletion
The list is :
| 56 - 1 | 34 - 2 | 23 - 3 | 60 - 4 | 44 - 5 |
The deleted item is 56
After the deletion
The list is :
| 34 - 2 | 23 - 3 | 60 - 4 | 44 - 5 |
*/