How do I create an array of linked list? Actually, I just need to know how to build an array of nodes. I know how to build linked list from the nodes.
Please reply.
How do I create an array of linked list? Actually, I just need to know how to build an array of nodes. I know how to build linked list from the nodes.
Please reply.
Um, just like you would "build" an array of anything, right? Something like this for a declaration perhaps:
Code:LinkedList arrayoflinkedlists[100];
The purpose of my program is to save a list of words into an array of linked lists using hashing.
This is just a snippet of my code. I cant understand why Im getting a segmentation fault at addhead(string,List[bucket]) and add(string,List[bucket]).
I know its a segmentation fault on those lines as when I put print statements and '//' line by line. When I include both the addhead and head without '//'. I get the segmentaion fault.
Code:#define WORD_LEN 15 #define MAX 10000 Node *List[MAX]; int bucket=0; int key =0; Node *temp; while(fgets(string, WORD_LEN, fp)) { key = 0; for (i=0;i<strlen(string);i++) { key = (int) string[i] + (31 * key); } bucket = (int) key % MAX; if (List[bucket] == NULL) temp = addhead(string,List[bucket]); else {temp=add(string,List[bucket]);} } ------------------------------------------------------------------------------------------------------------ struct NODE { char string[WORD_LEN]; NODE *next; }; Node *newNode(char *new_string) { Node *NewNode = NULL; NewNode = (Node *)malloc(sizeof(Node)); strcpy(NewNode->string,new_string); NewNode->next = NULL; return(NewNode); } Node *add(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); while (temp->next != NULL) { assert(temp->next != NULL); temp = temp->next; } temp->next = NewNode; temp->next = NULL; return(NewNode); } Node *addhead(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); temp->next = NewNode; NewNode->next = NULL; return (NewNode); }
Last edited by Ron; 07-03-2008 at 06:26 PM.
Easy. Just follow your logic.
List[bucket] is null, and you pass it to addhead.Code:if (List[bucket] == NULL) temp = addhead(string,List[bucket]);
In addhead, you do this:
without checking to see if it is null or not.Code:temp->next = NewNode;
Todd
Mainframe assembler programmer by trade. C coder when I can.
Well, I didnt see the necessity for that. Because if from the main function, I did check if temp is linked to null then addhead. But I did do an assert in the addhead function, to check if the temp->next == NULL. And I still get the segmentation fault.
You can't just do an assert if temp->next==NULL. You first have to do an assert that temp != NULL.
Mainframe assembler programmer by trade. C coder when I can.
Same segmentation fault.
Here is the code a bit modified:
Code:#define WORD_LEN 15 #define MAX 10000 Node *List[MAX]; int bucket=0; int key =0; Node *temp; while(fgets(string, WORD_LEN, fp)) { key = 0; for (i=0;i<strlen(string);i++) { key = (int) string[i] + (31 * key); } bucket = (int) key % 101; if (List[bucket] == NULL) temp = addhead(string,List[bucket]); else {temp=add(string,List[bucket]);} } ------------------------------------------------------------------------------------------------------------ struct NODE { char string[WORD_LEN]; NODE *next; }; Node *newNode(char *new_string) { Node *NewNode = NULL; NewNode = (Node *)malloc(sizeof(Node)); strcpy(NewNode->string,new_string); NewNode->next = NULL; return(NewNode); } Node *add(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); while (temp->next != NULL) { assert(temp!= NULL); temp = temp->next; } temp->next = NewNode; temp->next = NULL; return(NewNode); } Node *addhead(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); assert(temp == NULL); if (temp->next == NULL) { temp->next = NewNode; NewNode->next = NULL; return(NewNode); } else { return 0; } }
If temp is NULL, you cannot access the next member without a seg fault.Code:assert(temp == NULL); if (temp->next == NULL)
ETA: Oh, and do you have both a global temp variable and a local one as well? Bad news.
O yea that was a mistake. But I get the segmenation fault without any asserts too.
temp is not a global variable. It is in the main function. I told so, this is a snippet of the code
How do you think what is the result of the code above?Code:temp->next = NewNode; temp->next = NULL;
addhead receives null-pointer, so temp->next will cause the crash
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Yea now it is working. I had a doubt about this. I thought as we are are making an array of nodes, We have to add the head node to the next of array-element-node.
Thanks.
Im facing a problem. When I add a print statement to print the contents of the nodes that are not null. Null is printed in every node. That means nothing is being entered into the nodes.
What could be the problem?
When inserting the following code to print:
This is result that I am getting when running:Code:printf("\nFinished Step1\n"); for (bucket=0;bucket<MAX; bucket++) { if (List[bucket]!=NULL) { curr=List[bucket]; printf("\nFinished Step3\n"); while (curr != NULL) { strcpy(word_copy,List[bucket]->string); printf("%s",word_copy); printf("\nFinished Step3a\n"); curr=curr->next; printf("\nFinished Step3b\n"); } printf("\nFinished Step4\n"); } }
Code:Finished Step1 Finished Step3 [Ãc inished Step3a Finished Step3b [Ãc inished Step3a Segmentation Fault
Last edited by Ron; 07-10-2008 at 07:28 PM.
I know the problem is in my adding function.
this is my modified codes for addhead and add
Code:Node *add(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); while (temp->next != NULL) { assert(temp!= NULL); temp = temp->next; } temp->next = NewNode; temp->next = NULL; return(NewNode); } Node *addhead(char *new_string, Node *temp) { Node *NewNode = newNode(new_string); if (temp -> string == NULL) { temp = NewNode; temp->next = NULL; return(temp); } }