implementation of linked list inside hash table

会有一股神秘感。 提交于 2019-12-25 04:22:38

问题


i am learning data structures and so far have seen linked list,binary tress, and hash tables. I am trying to find a general way to approach a practice problem in which it gives me freedom to use these data structures to solve it. I was hoping to receive some feedback on the best way to solve this problem. The problem tells me to read two files, one file with all the classes and pre-requisites needed to complete a degree and another file that has all classes that have been completed and figure out what classes i am able to take. So lets say degree.txt has the following(first class is main class and vertical bar indicates pre-requisites)

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

and completed.txt has the following

ENGL 1301
MATH 1323

then i would want my program to print

ELIGIBLE TO TAKE 
ENGL 1302

the following is the way i was thinking of approaching the beggining of the problem. a hash table chain with linked list inside each "node" for the pre-requisites.

-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

so my main question would be if my above method can work. Im mainly trying to figure out how to store the pre requisite classes as the "value" in the "key/value" pair method in each node for the hash table and the first thing i thought of was linked list since its easy to add elements to it since i do not know how many pre-requisites each class will have. I was thinking to store the pre-requisites in an array but again i dont know the size of pre-requisites therefore dont know big to make the pre-requisite array. What are your guys thoughts to approaching this problem?

来源:https://stackoverflow.com/questions/25130754/implementation-of-linked-list-inside-hash-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!