-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedQ.h
194 lines (141 loc) · 3.12 KB
/
linkedQ.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef linkedQ_H
#define linkedQ_H
//////////////////////// LINKED LIST DS ////////////////////
typedef struct node_t {
char data;
int freq;
node_t *next;
node_t *prev;
node_t *left;
node_t *right;
// a frankenstein structure
}node_t;
node_t *create_node(char item){
node_t *node = (node_t*)malloc(sizeof(node_t));
if(node == NULL)
return NULL;
node->data = item;
node->freq = 1;
node->next = NULL;
node->prev = NULL;
node->right = NULL;
node->left = NULL;
return node;
}
node_t *find_node(node_t *head, char value){
while(head != NULL){
if(head->data == value)
return head;
head = head->next;
}
return NULL;
}
bool insert_first(node_t **head, node_t **tail, char item){
node_t *tmp = create_node(item);
if(tmp == NULL)
return false;
tmp->prev = NULL;
if(*head == NULL){
tmp->next = NULL;
*tail = tmp;
}
else
tmp->next = *head;
*head = tmp;
return true;
}
void swap_nodes(node_t *node1, node_t *node2){
if(node1 == node2)
return;
char tmp_data = node1->data;
int tmp_freq = node1->freq;
node_t *right_tmp = node1->right;
node_t *left_tmp = node1->left;
node1->data = node2->data;
node1->freq = node2->freq;
node1->left = node2->left;
node1->right = node2->right;
node2->data = tmp_data;
node2->freq = tmp_freq;
node2->left = left_tmp;
node2->right = right_tmp;
}
void sort_list(node_t *head){
node_t *i, *j;
for(i=head; i!=NULL; i=i->next){
for(j=i->next; j!=NULL; j=j->next){
if(i->freq > j->freq)
swap_nodes(i, j);
}
}
}
bool insert_node_first(node_t **head, node_t *node){
if((*head) == NULL || node == NULL)
return false;
node->prev = NULL;
node->next = *head;
*head = node;
return true;
}
bool remove_node_first(node_t **head, node_t **node){
if((*head) == NULL)
return false;
(*node) = (*head);
(*head) = (*head)->next;
return true;
}
// void free_list(node_t *head){
// node_t *tmp;
// while(head != NULL){
// tmp = head;
// head = head->next;
// free(tmp);
// }
// }
//////////////////////// END OF LINKED LIST DS //////////////////
///////////////////// Q DS USING LINKED LIST ///////////////////
typedef struct Q_t{
node_t *head;
node_t *tail;
Q_t(){
head = NULL;
tail = NULL;
}
// ~Q_t(){
// free_list(head);
// }
}Q_t;
bool enQ(Q_t* q, char item, node_t *node){
// this enQ function is multi-purpose, used to enQ a character and a node
// by accepting both, it can act according to the input parameters
// if the input node is null, this means we want to enQ a character
// else, enQ the node given
if(node != NULL){
if(!insert_node_first(&(q->head), node))
return false;
sort_list(q->head);
}
else{
node_t *tmp = find_node(q->head, item);
if(tmp == NULL){
if(!insert_first(&(q->head), &(q->tail), item))
return false;
}
else{
tmp->freq++;
}
}
return true;
}
bool deQ(Q_t* q, node_t **node){
// when the Q is empty, will return false to stop the deQing and set the garbage node to NULL;
if(q->head == NULL){
(*node) = NULL;
return false;
}
if(!remove_node_first(&(q->head), node))
return false;
return true;
}
///////////////////// END OF Q DS ///////////////////
#endif