Thread: Array of forums

  1. #46
    Registered User
    Join Date
    May 2006
    Posts
    151
    Thanks <tabstop>.
    The program is working now. In addhead I had to assign the return to a temp node, which in turn I have to equate to List[bucket].
    As for as, add function, I worked with 2 pointers prev and curr. Now it is working.

    Thanks Once again tabstop.

  2. #47
    Registered User
    Join Date
    May 2006
    Posts
    151
    No the add is still not working. When I tried printing the contents of the node, only the heads are printed, not the rest of the linked list.

    Im still not getting it. Once a linked list is set, can't I just leave it void.

    My whole program is working except for this. I have to take another input file and compare the words with the words in the array of nodes. Then destroy the array. Everything is working, but the add function. Please someone show a spotlight to me.

    PLEASE PLEASE

  3. #48
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ron View Post
    No the add is still not working. When I tried printing the contents of the node, only the heads are printed, not the rest of the linked list.

    Im still not getting it. Once a linked list is set, can't I just leave it void.

    My whole program is working except for this. I have to take another input file and compare the words with the words in the array of nodes. Then destroy the array. Everything is working, but the add function. Please someone show a spotlight to me.

    PLEASE PLEASE
    We already have:
    Quote Originally Posted by tabstop
    Question: Suppose you use the add() function to add a second node to your linked list. What line of your program sets the link from the head of the list to the new node? Bonus question: What, if anything, does the line temp = &NewNode do in your add() function, in the grand scheme of things? Double bonus question: When you are back in your main program after calling add(), what, if anything, is pointing to your newly-created node?

  4. #49
    Registered User
    Join Date
    May 2006
    Posts
    151
    In the add function, I have returned the whole Linked list temp.
    Code:
    Then from main :
    {
     Node *temp
    
    temp=add(para,para);
     List[bucket] = temp;
    }
    But this doesnt help either.

  5. #50
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ron View Post
    In the add function, I have returned the whole Linked list temp.
    Code:
    Then from main :
    {
     Node *temp
    
    temp=add(para,para);
     List[bucket] = temp;
    }
    But this doesnt help either.
    So? Look at the question again:
    What line of your program sets the link from the head of the list to the new node?
    I'll ask another question to lead in to it: where would this link from one node to ... oh, let's call it the "next" node ... where would this link be?

  6. #51
    Registered User
    Join Date
    May 2006
    Posts
    151
    Good question. I was always thinking of that. But didnt figure that to be the problem.
    How can I set it up?
    I have to create a function get head. But how can I do this with an arry of Node pointers?

  7. #52
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ron View Post
    Good question. I was always thinking of that. But didnt figure that to be the problem.
    How can I set it up?
    You seem to have, in your code, code to follow a linked list all the way to the end. (I believe it's even in your add function.) How do you get from one node to the next? If that's how you get from one node to the next, how do you tack another node on to the end?

    And an aside, if you hadn't noticed: the less work you put into your replies, the more obscure our answers become. If you want me to give you some better hints, you'll have to show that you've understood anything at all we've been saying to you. (And no, you really haven't shown much understanding at all in this thread that I can see.)

    Quote Originally Posted by Ron View Post
    I have to create a function get head. But how can I do this with an arry of Node pointers?
    You don't care about this until you get the rest working.

  8. #53
    Registered User
    Join Date
    May 2006
    Posts
    151
    I have a pointer curr in the function , that goes curr =curr-> next.
    This is my add code.
    Code:
    void add(char *new_string, Node *temp)
    {
    	Node *NewNode;
    	NewNode = newNode(new_string);
    	Node *curr;
    	Node *prev;
    	curr = temp;
    	while (curr != NULL)
    	{
    		prev = curr;
    		curr = curr->next;
    	}
    	prev->next=NewNode;
    return (temp);
    }
    Code:
    Main function
    			if (List[bucket] == NULL)
    				{  
    					temp = addhead(string,List[bucket]);
    				 List[bucket]= temp;
    
    				}
        		else
        	    	{
    
    					
    					temp =  add(string,List[bucket]);
                                            List[bucket] = temp;
    
    		}
    My question is about the function call. Im aware you are making me think with your replies. which is good. So I can solve the problem myself. But your vocabulary is way over my head.
    Thanks.

  9. #54
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ron View Post
    I have a pointer curr in the function , that goes curr =curr-> next.
    This is my add code.
    Code:
    void add(char *new_string, Node *temp)
    {
    	Node *NewNode;
    	NewNode = newNode(new_string);
    	Node *curr;
    	Node *prev;
    	curr = temp;
    	while (curr != NULL)
    	{
    		prev = curr;
    		curr = curr->next;
    	}
    	prev->next=NewNode;
    return (temp);
    }
    Code:
    Main function
    			if (List[bucket] == NULL)
    				{  
    					temp = addhead(string,List[bucket]);
    				 List[bucket]= temp;
    
    				}
        		else
        	    	{
    
    					
    					temp =  add(string,List[bucket]);
                                            List[bucket] = temp;
    
    		}
    My question is about the function call. Im aware you are making me think with your replies. which is good. So I can solve the problem myself. But your vocabulary is way over my head.
    Thanks.
    Alright, now we're getting somewhere. add does everything it's supposed to: walks the link, tacks the new node at the end, sets the link. So why do you feel the need to do anything else afterwards? Why not just call
    Code:
    add(string, List[bucket]);
    and be done with it?

    Also, note that add is a void function, which means you cannot return something from it, so return(temp) is invalid and your compiler should be screaming to you about it.

  10. #55
    Registered User
    Join Date
    May 2006
    Posts
    151
    That is what I have been telling so far. After doing the add and add head function. when I print the list. including sublists. only the heads are printed.

  11. #56
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    I think (assuming you removed the return from add and the extra two assignments) that you have that right, then. I have no idea what your print code looks like.

  12. #57
    Registered User
    Join Date
    May 2006
    Posts
    151
    Code:
    void print (Node *temp)
    {
    	char word_copy[MAX];
    	Node *curr;
    	curr = temp;
    
    				if (curr !=NULL)
    				{
    
    
    					
    					while (curr != NULL)
    					{
    						strcpy(word_copy,temp->string);
    						printf("&#37;s",word_copy);
    
    
    						curr=curr->next;
    
    					}
    				
    				}
    
    }
    Code:
    Main function:
    	for(bucket=0;bucket < MAX; ibucket++)
    	{
    
    	print (List[bucket]);
    
    	}

  13. #58
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    strcpy(word_copy,temp->string);
    temp??? Why print temp, when you want to print curr?

  14. #59
    Registered User
    Join Date
    May 2006
    Posts
    151
    Just wanted to print the contents of the node. It is curr, sorry.

    Still doesnt work, as i count checked the contents in the lists.
    Last edited by Ron; 07-13-2008 at 08:39 PM.

  15. #60
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    In an attempt to save tabstop's Hat of Guessing from becoming too bloodied:
    Code:
    void print (Node *temp)
    {
        Node *curr = temp;
    
        while (curr !=NULL)
        {
            if (curr->string)
                printf("%s", curr->string);
            curr=curr->next;
        }
    }

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