-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastructs.c
214 lines (172 loc) · 4.52 KB
/
datastructs.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
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
195
196
197
198
199
200
201
202
#include "datastructs.h"
/*HASH TABLE METHODS*/
/*This method takes in a string input and returns the string's hash*/
unsigned int hashingfunction(char* pass){
unsigned int hash = OFFSET;
char *s = pass;
while(*s != '\0'){
hash = hash^*s;
hash *= PRIME_NUM;
s++;
}
return hash;
}
/*This method creates a fNode with the given parameters
*and attachs it to the appropriate hash bucket
*/
int addFileNode(hashNode table, char *word, int fNum, int frNum){
fileNode fl, temp;
int position;
char *wordholder;
if((fl = (fileNode)(malloc(sizeof(struct fNode)))) == NULL){
return -1;
}
if((wordholder = (char *)(malloc(sizeof(char)*strlen(word)))) == NULL){
return -1;
}
strncpy(wordholder, word, strlen(word));
fl->word = wordholder;
fl->fileNum = fNum;
fl->numFrequency = frNum;
position = hashingfunction(word)%MAXWORDS;
if(table[position].next == NULL){
table[position].next = fl;
return 0;
}
else{
temp = table[position].next;
while(temp->next != NULL){
if(temp->next->fileNum < fl->fileNum){
temp = temp->next;
}
else{
break;
}
}
fl->next = temp->next;
temp->next = fl;
return 0;
}
return -1;
}
/* This method was written to print out the contents of the hash table.
* It was written purely for testing purposes.
*/
void printtable(hashNode table){
int i;
fileNode test;
for(i = 0; i < MAXWORDS; i++){
test = table[i].next;
while(test != NULL){
printf("the word is %s\n", test->word);
printf("the hash is %d\n", hashingfunction(test->word)%MAXWORDS);
printf("the file number is %d\n", test->fileNum);
printf("the frequency is %d\n", test->numFrequency);
test = test->next;
}
}
}
/*This method frees all the memory used in the hash table*/
void deallocatetable(hashNode table){
int i;
fileNode test, test2;
hashNode *temp;
for(i = 0; i < MAXWORDS; i++){
test = table[i].next;
while(test!= NULL){
test2 = test;
test = test->next;
free(test2);
}
free(test);
table[i].next = NULL;
/**/
}
/*
printtable(table);
for(i = 0; i < MAXWORDS; i++){
temp = &table[i];
free(temp);
}*/
free(table);
}
/*PRIORITY QUEUE METHODS*/
PriorityQueue PQCreate(int MaximumElements){
PriorityQueue pq;
if((pq = (PriorityQueue)(malloc(sizeof(struct Heap)))) == NULL){
fprintf(stderr, "Error allocating memory for Priority Queue", MAXLINELENGTH);
return NULL;
}
pq->maxSize = MaximumElements;
pq->currSize = 0;
if((pq->Elements = (HeapNode*)(MaximumElements)) == NULL){
fprintf(stderr, "Error allocating memory for Priority Queue elements", MAXLINELENGTH);
return NULL;
}
return pq;
}
void PQInsert(PriorityQueue pq, char *word2){
HeapNode temp;
int position;
while(pq->currSize >= pq->maxSize){
temp = deleteMin(pq);
free(temp->word);
free(temp);
}
position = pq->currSize++;
while(position > 0 && 1 < (pq->Elements[(position-1)/2]).priority){
(pq->Elements[position]).priority = (pq->Elements[(position-1)/2]).priority;
(pq->Elements[position]).word = (pq->Elements[(position-1)/2]).word;
position = (position-1)/2;
}
(pq->Elements[position]).priority = 1;
(pq->Elements[position]).word = word2;
return(0);
}
void HeapSort(PriorityQueue pq, int size){
int i, temp;
for(i = (size/2); i >= 0; i--){
siftDown(pq->Elements, i, size-1;
}
}
char* deleteMin(PriorityQueue pq){
char *minWord;
int rootWord = 0;
int pos, newPos, last;
if(pq->currSize == 0){
return NULL;
}
minWord = (pq->Elements[0]).word;
pos = 0;
while(!rootWord){
if( 2*pos + 2 < pq->currSize ){
if( (pq->Elements[2*pos+1]).priority < (pq->Elements[2*pos+2]).priority)
newpos = 2*pos + 1;
else
newpos = 2*pos + 2;
(pq->Elements[pos]).priority = (pq->Elements[newpos]).priority;
(pq->Elements[pos]).word = (pq->Elements[newpos]).word;
pos = newpos;
}
else if ( 2*pos + 2 == pq->currSize ){
newpos = 2*pos + 1;
(pq->Elements[pos]).priority = (pq->Elements[newpos]).priority;
(pq->Elements[pos]).word = (pq->Elements[newpos]).word;
pq->currSize -= 1;
return(minWord);
/* finished, came out exactly on last element */
}
else
rootWord = 1;
}
last = --pq->currSize;
while(pos > 0 && (pq->Elements[last]).priority < (pq->Elements[(pos-1)/2]).priority ){
(pq->Elements[pos]).priority = (pq->Elements[(pos-1)/2]).priority;
(pq->Elements[pos]).word = (pq->Elements[(pos-1)/2]).word;
pos = (pos-1)/2;
}
(pq->Elements[pos]).priority = (pq->Elements[last]).priority;
(pq->Elements[pos]).word = (pq->Elements[last]).word;
/* filled pos by moving last element in it*/
return(minWord);
}