在哈希表中实现链表

时间:2014-08-05 03:31:13

标签: c methods linked-list hashtable

我正在学习数据结构,到目前为止已经看到了链表,二进制tress和哈希表。我试图找到一种解决实践问题的一般方法,它可以让我自由地使用这些数据结构来解决它。我希望收到一些关于解决这个问题的最佳方法的反馈。问题告诉我要读取两个文件,一个文件包含完成学位所需的所有类和先决条件,另一个文件包含已完成的所有类,并找出我能够学习的类。所以我们假设degree.txt有以下内容(第一个类是主类,垂直条表示先决条件)

ENGL 1301|none
ENGL 1302|ENGL 1301|none
MATH 1323|none
MATH 1376|MATH 1323|none
MATH 1425|MATH 1323|MATH 1376|none

completed.txt具有以下内容

ENGL 1301
MATH 1323

然后我希望我的程序打印

ELIGIBLE TO TAKE 
ENGL 1302

以下是我想要接近问题的方法。一个哈希表链,每个“节点”内都有链表,用于先决条件。

-declare struct node {
             char* class;
             int found;
             struct node* next;
             struct node* head;
             };

/*building the hash table data structure*/
-read each line of degree 
     tokenize to get main class
     hash main class and return index to place in hash table
     copy main class into struct node class
     set found value to 0
     tokenize to get pre-req classes
         add each pre-req to a linked list inside each node in the hash table/*possible?*/

/*reading completed classes and connecting them with hash table through "found" value*/
-read each line of completed
     send line(class) to be hashed returning [index]
     search through hash table[index] "nodes" for matching class
     if class is in hash table change found value to 1

/*printing classes eligible to take*/
-iterate through hash table
-if "found" value in "node"(struct node) = 1 go to next node
-if "found" value in node = 0(means class has not been taken)
    iterate through linked list(list of pre-reqs) in that node
    send pre-req to be hashed and return [index] value
    search hashtable[index] to find pre-req class in hastable
    if "found" = 1 return value 1 to continue through the pre-req linked list
    if "found" = 0 return value 0 to indicate that pre req has not been met and contine iterating through hash table

所以我的主要问题是如果我的上述方法可行。我主要试图找出如何将前必需类存储为哈希表的每个节点中的“键/值”对方法中的“值”,并且我想到的第一件事是链表,因为它易于添加元素因为我不知道每个班级有多少先决条件。我正在考虑将先决条件存储在一个数组中,但我再也不知道先决条件的大小,因此不知道制作必备数组的大小。你们有什么想法来解决这个问题?

0 个答案:

没有答案