Array of forums

This is a discussion on Array of forums within the C Programming forums, part of the General Programming Boards category; How do I create an array of linked list? Actually, I just need to know how to build an array ...

  1. #1
    Ron
    Ron is offline
    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,211
    Um, just like you would "build" an array of anything, right? Something like this for a declaration perhaps:

    Code:
    LinkedList arrayoflinkedlists[100];

  3. #3
    Ron
    Ron is offline
    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 07:26 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Ron
    Ron is offline
    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
    Katy, Texas
    Posts
    2,309
    You can't just do an assert if temp->next==NULL. You first have to do an assert that temp != NULL.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Ron
    Ron is offline
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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
    Ron
    Ron is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Ron
    Ron is offline
    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
    Ron
    Ron is offline
    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
    Ron
    Ron is offline
    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 08:28 PM.

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

  15. #15
    Ron
    Ron is offline
    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);
    	}
    
    }

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21