Thread: Array of forums

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Array of forums

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Um, just like you would "build" an array of anything, right? Something like this for a declaration perhaps:

    Code:
    LinkedList arrayoflinkedlists[100];

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    151

    Re:

    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 &#37; 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.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Easy. Just follow your logic.

    Code:
    if (List[bucket] == NULL)
    				temp = addhead(string,List[bucket]);
    List[bucket] is null, and you pass it to addhead.

    In addhead, you do this:
    Code:
    temp->next = NewNode;
    without checking to see if it is null or not.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    151

    Re:

    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.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    151
    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 &#37; 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;
    	}
    }

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    assert(temp == NULL);
    if (temp->next == NULL)
    If temp is NULL, you cannot access the next member without a seg fault.

    ETA: Oh, and do you have both a global temp variable and a local one as well? Bad news.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    151
    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

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    	temp->next = NewNode;
    	temp->next = NULL;
    How do you think what is the result of the code above?


    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

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    151
    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.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    151
    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?

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    151
    When inserting the following code to print:
    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("&#37;s",word_copy);
    
    		            printf("\nFinished Step3a\n");
    			   curr=curr->next;
    		            printf("\nFinished Step3b\n");
    				}
    		             printf("\nFinished Step4\n");
    			}
    		}
    This is result that I am getting when running:

    Code:
    Finished Step1
    
    Finished Step3
    [&#195;c
    inished Step3a
    
    Finished Step3b
    [&#195;c
    inished Step3a
    Segmentation Fault
    Last edited by Ron; 07-10-2008 at 07:28 PM.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    151
    I know the problem is in my adding function.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    151
    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);
    	}
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM