-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHashTable.h
148 lines (147 loc) · 3.19 KB
/
MyHashTable.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
#include "MyData.h"
#define TableSize 300
typedef struct Node Node;
struct Node
{
GtkTreeIter iter;
char name[1024];
int flag;
Node* next;
Node* pre;
};
void NodeConstructor(Node* x, char val[4][1024])
{
strcpy(x->name, val[3]);
x->flag = 1;
x->next = NULL;
x->pre = NULL;
gtk_list_store_prepend(GTK_LIST_STORE(TMD.ProcessorData), &x->iter);
gtk_list_store_set(GTK_LIST_STORE(TMD.ProcessorData), &x->iter, 0, val[0], 1, val[1], 2, val[2], 3, val[3], -1);
}
typedef struct HashTable
{
Node** Table;
} HashTable;
void HashTableConstructor(HashTable* a)
{
a->Table = (Node**)malloc(TableSize*sizeof(Node*));
for (int i = 0; i < TableSize; i++) a->Table[i] = NULL;
}
void HashTableDestructor(HashTable *a)
{
for(int i = 0 ; i < TableSize ; i++)
{
Node* temp = a->Table[i];
while(temp != NULL)
{
Node* temp2 = temp;
temp = temp->next;
temp2->next = NULL;
temp2->pre = NULL;
free(temp2);
temp2 = NULL;
}
}
free(a->Table);
a->Table = NULL;
}
int Hash(char *value)
{
int val = atoi(value);
int temp1 = val;
while(temp1 >= 10) temp1 /= 10;
int temp2 = val%100;
return (temp1 * 100 + temp2) % TableSize;
}
int Search(HashTable* a, char val[4][1024])
{
int h = Hash(val[3]);
Node* temp = a->Table[h];
while(temp != NULL)
{
if(!strcmp(temp->name, val[3]))
{
if(gtk_list_store_iter_is_valid(GTK_LIST_STORE(TMD.ProcessorData), &temp->iter))
{
gtk_list_store_set(GTK_LIST_STORE(TMD.ProcessorData), &temp->iter, 0, val[0], 1, val[1], 2, val[2], 3, val[3], -1);
temp->flag = 1;
return h;
}
}
temp = temp->next;
}
return -1;
}
void Insert(HashTable* a, char val[4][1024])
{
int h = Hash(val[3]);
Node* newnode = (Node*)malloc(sizeof(Node));
if(TMD.TimerOn)
{
NodeConstructor(newnode, val);
if (a->Table[h] == NULL) a->Table[h] = newnode;
else
{
Node* temp = a->Table[h];
while (temp->next != NULL) temp = temp->next;
temp->next = newnode;
newnode->pre = temp;
}
}
else free(newnode);
}
void Delete(HashTable* a)
{
for(int i = 0 ; i < TableSize ; i++)
{
Node* temp = a->Table[i];
while(temp != NULL)
{
if(temp->flag == 0)
{
if(gtk_list_store_iter_is_valid(GTK_LIST_STORE(TMD.ProcessorData), &temp->iter))
{
gtk_list_store_remove(GTK_LIST_STORE(TMD.ProcessorData), &temp->iter);
if(temp->next == NULL && temp->pre == NULL)
{
Node* temp2 = temp;
temp = temp->next;
free(temp2);
temp2 = NULL;
}
else if(temp->next == NULL)
{
Node* temp2 = temp;
temp = temp->next;
temp2->pre->next = NULL;
temp2->pre = NULL;
free(temp2);
}
else if(temp->pre == NULL)
{
Node* temp2 = temp;
a->Table[i] = a->Table[i]->next;
temp = temp->next;
temp2->next->pre = NULL;
temp2->next = NULL;
free(temp2);
}
else
{
Node* temp2 = temp;
temp = temp->next;
temp2->pre->next = temp2->next;
temp2->next->pre = temp2->pre;
free(temp2);
}
}
else temp = temp->next;
}
else
{
temp->flag = 0;
temp = temp->next;
}
}
}
}