-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityQueue.c
85 lines (68 loc) · 1.48 KB
/
priorityQueue.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
//Priority queue using LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct node *head;
struct node{
int data;
int prio;
struct node *next;
};
//display the values of queue
void display(struct node *flag){
// int count = 0;
while(flag->next!=NULL){
printf("%d->", flag->data);
flag = flag->next;
// count++;
}
printf("%d\n", flag->data);
// printf("Total node is : %d.\n", count+1);
}
//pushing values according to priority
void push(int val, int pVal){
struct node *p = head, *newN;
newN = malloc(sizeof(struct node));
newN->data = val;
newN->prio = pVal;
if(head == NULL){
newN->next = NULL;
head = newN;
return;
}
if(head->prio < pVal){
newN->next = head;
head = newN;
return;
}
while(p->next != NULL){
if(p->next->prio < pVal){
newN->next = p->next;
p->next = newN;
return;
}
p = p->next;
}
newN->next = p->next;
p->next = newN;
}
//remove node from the first
void pool(){
struct node *flag;
flag = head;
head = head->next;
printf("%d has been removed.\n", flag->data);
free(flag);
}
int main(void){
head = NULL;
push(5,5);
push(4,6);
push(3,7);
push(2,8);
push(1,3);
display(head);
pool();
pool();
display(head);
return 0;
}