-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.c
More file actions
137 lines (118 loc) · 3.13 KB
/
priorityqueue.c
File metadata and controls
137 lines (118 loc) · 3.13 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1024
/**
* @brief priority queue structure
*/
typedef struct priortyqueue
{
int heap[MAX_SIZE]; // heap size with 1024
int count; // heap counts (index)
} priortyqueue;
/**
* @brief node location change
* @param first frist node
* @param second second node
*/
void
node_exchange(int * first, int * second)
{
int temp = *first;
*first = *second;
*second = temp;
}
/**
* @brief push function for priority queue
* @param root priority queue root pointer
* @param data data to insert into the queue
* @return none
*/
void
push(priortyqueue *root, int data)
{
if( MAX_SIZE <= root->count) return; // boundary check (to block heap overflow)
root->heap[root->count] = data; // index location
int cur = root->count;
int par = (root->count - 1) / 2;
while
(cur > 0 && root->heap[cur] > root->heap[par])
{
node_exchange(&root->heap[cur], &root->heap[par]);
cur = par;
par = (par - 1) / 2;
}
root->count++;
}
/**
* @brief pop function for priority queue
* @param root priority queue root pointer
* @return -1 : error, top value : success
*/
int
pop(priortyqueue *root) {
if (0 >= root->count) return -1; // if there is no count, return -1
int ret = root->heap[0]; // to pop value (first value to pop)
root->count--; // decrease by one
root->heap[0] = root->heap[root->count]; // move the last node to the first node
int cur = 0;
// next is to move the index location
int next = 0;
int left = 1;
int right = 2;
// top down exchagne
while (left < root->count)
{
// if the top node is larger than the left child node
if (root->heap[next] < root->heap[left])
{
// move the index to compare and exchange
next = left;
}
// if the top node is larger than the right child node;
if (root->heap[next] < root->heap[right])
{
// out of the index location
if (right < root->count)
{
//node_exchange(&root->heap[next], &root->heap[right]);
next = right;
}
}
if (next == cur) break;
else
{
node_exchange(&root->heap[next], &root->heap[cur]);
// index jump after exchanging
cur = next;
// i * n + j
left = cur * 2 + 1;
right = cur * 2 + 2;
}
}
return ret;
}
int main(void)
{
int n = 12;
priortyqueue * root = malloc(sizeof(*root));
//printf("sizeof %ld", sizeof( root*));
push(root, 14);
push(root, 22);
push(root, 36);
push(root, 65);
push(root, 44);
push(root, 53);
push(root, 46);
push(root, 57);
push(root, 86);
push(root, 97);
push(root, 99);
push(root, 25);
for(int i = 0; i < n; i++)
{
int data = pop(root);
printf("%d\n", data);
}
free(root);
return 0;
}