Repository navigation
Expand file tree
/
Copy pathlab9.c
More file actions
165 lines (143 loc) · 4.17 KB
/
Copy pathlab9.c
File metadata and controls
165 lines (143 loc) · 4.17 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
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
#include <stdio.h>
#include <stdlib.h>
// RecordType
struct RecordType
{
int id;
char name;
int order;
// create a next pointer (for linkedlist structure)
struct RecordType *next;
};
// Fill out this structure
struct HashType
{
// create a pointer to a RecordType
struct RecordType *pointer;
};
// Compute the hash function
int hash(int x, int tableSize)
{
// hash function is x % tableSize
return x % tableSize;
}
// parses input file to an integer array
int parseData(char *inputFileName, struct RecordType **ppData)
{
FILE *inFile = fopen(inputFileName, "r");
int dataSz = 0;
int i, n;
char c;
struct RecordType *pRecord;
*ppData = NULL;
if (inFile)
{
fscanf(inFile, "%d\n", &dataSz);
*ppData = (struct RecordType *)malloc(sizeof(struct RecordType) * dataSz);
// Implement parse data block
if (*ppData == NULL)
{
printf("Cannot allocate memory\n");
exit(-1);
}
for (i = 0; i < dataSz; ++i)
{
pRecord = *ppData + i;
fscanf(inFile, "%d ", &n);
pRecord->id = n;
fscanf(inFile, "%c ", &c);
pRecord->name = c;
fscanf(inFile, "%d ", &n);
pRecord->order = n;
pRecord->next = NULL;
}
fclose(inFile);
}
return dataSz;
}
// prints the records
void printRecords(struct RecordType pData[], int dataSz)
{
int i;
printf("\nRecords:\n");
for (i = 0; i < dataSz; ++i)
{
printf("\t%d %c %d\n", pData[i].id, pData[i].name, pData[i].order);
}
printf("\n\n");
}
// insert record into the hash table
void insertRecord(struct HashType *hashTable, struct RecordType *record, int tableSize)
{
// call the hash function to get the index
int index = hash(record -> id, tableSize);
// if the RecordType at that index is NULL
if(hashTable[index].pointer == NULL){
// set 'record' equal to the HashType pointer in the table at index
hashTable[index].pointer = record;
}
// else
else{
// traverse to the end of the linkedlist and add 'record' to the end of it
struct RecordType *current = hashTable[index].pointer;
while(current->next != NULL){
current = current -> next;
}
current -> next = record;
}
}
// display records in the hash structure
// skip the indices which are free
// the output will be in the format:
// index x -> id, name, order -> id, name, order ....
void displayRecordsInHash(struct HashType *hashTable, int tableSize)
{
// for each entry in the table
// print the contents in that index
// The output should look like this (for example): "Index 1 -> 21715, Q, 16 -> 28876, a, 26 -> NULL"
for(int i = 0; i<tableSize; i++)
{
//hold indexof the hash tbale
printf("Index %d ->", i);
//Assigning a pointer to the linked list in the hashtable
struct RecordType * current = (hashTable + i)->pointer;
if (current!= NULL){
while(current!= NULL)
{
//prints the linked list at an indext till it hits NULL
printf(" %d, %c, %d, " , current->id, current->name, current->order);
if(current->next == NULL)
{
printf("-> NULL");
}
current = current->next;
}
printf("\n");
}
}
}
int main(void)
{
struct RecordType *pRecords;
int recordSz = 0;
recordSz = parseData("input.txt", &pRecords);
printRecords(pRecords, recordSz);
// Initialize the hash table
struct HashType *hashTable = NULL;
// create a variable hashTableSize and assign it a value
int hashtableSize = 11;
// initialize a hashTable, use calloc (so everything is assigned to NULL)
hashTable = (struct HashType*)calloc(sizeof(struct HashType), hashtableSize);
// for each record in pRecords, insert it into the hash table using the insertRecord function
int n = recordSz;
for (int i = 0; i<n; i++)
{
insertRecord(hashTable, &pRecords[i], hashtableSize);
}
// call the display records function
displayRecordsInHash(hashTable, hashtableSize);
// free all the allocated memory
free(pRecords);
free(hashTable);
return 0;
}