-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue.c
73 lines (68 loc) · 1.42 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
int priority;
struct node *next;
};
struct node *start = NULL;
struct node *new_node, *p;
/* First make a normal list with priority & data then arrange it on basis of priority.*/
void priority_list_create(){
int n, pr;
printf("Enter data & it's priority : ");
scanf("%d %d", &n, &pr);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = n;
new_node->priority = pr;
if(start == NULL){
start = new_node;
p = start;
new_node->next = NULL;
}
else{
p->next = new_node;
p = new_node;
new_node->next = NULL;
}
}
void display(){
struct node *q;
q = start;
while(q != NULL){
printf("\t(%d, %d)", q->data, q->priority);
q = q->next;
}
}
void priority_list_arrangement(){
struct node *temp, *p, *q;
p = start;
while(p->next != NULL){
q = p->next;
while(q != NULL){
if(p->priority > q->priority){
temp->data = p->data;
temp->priority = p->priority;
p->data = q->data;
p->priority = q->priority;
q->data = temp->data;
q->priority = temp->priority;
}
q = q->next;
}
p = p->next;
}
}
int main(){
int a = 5;
while(a > 0){
priority_list_create();
a--;
}
printf("\nThe irregular list is: ");
display();
printf("\nThe priority wise arranged list is: ");
priority_list_arrangement();
display();
return 0;
}