Adding several nodes to a linked list using a loop

This is a discussion on Adding several nodes to a linked list using a loop within the C Programming forums, part of the General Programming Boards category; Lets say I have a node like this Code: typedef struct node { int data; struct node *next; } Node; ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    Adding several nodes to a linked list using a loop

    Lets say I have a node like this

    Code:
    typedef struct node
    {
    	int data;
    	struct node *next;
    } Node;
    and lets say that i entered 3 nodes (which all have their memory allocated of course) in a list which looks like this:

    Code:
    1. node data = 1
    2. node data = 2
    3. node data = 3
    If I enter a number lets say 7, I would like to have a funcntion that enters 7-3 = 4 nodes at the end of the list so when it is done the list looks like this:

    Code:
    1. node data = 1
    2. node data = 2
    3. node data = 3
    4. node data = 4
    5. node data = 5
    6. node data = 6
    7. node data = 7
    I'd like to use a loop to do this but it doesn't work at all, it doesn't compile as it breaks on the green line.

    Code:
    /* NEW NODE */
    Node* NewNode(int n)
    {
        Node* new = malloc(sizeof(kat));
    	
        new->data = n;
        new->next = NULL;	
    	
        return(new);	
    } 
    
    /* LAST NODE */
    Node* LastNode(Node *head)
    {
        Node *temp = head;
        while (temp != NULL)
    	  temp = temp->next;
    		
        return(temp);
    }
    
    /* ADD NODES */
    void AddNodes(Node **head, int n)
    {
       Node *last;
       do 
       {
             last = LastNode(*poc);
             Node *new = NewNode(last->data);
             last->next = new;
       } while (last->data < n)
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your LastNode function is going to return NULL. You should instead use:
    Code:
    while( temp->next ) temp = temp->next;
    This assumes you've already made sure temp isn't NULL before it hits the while loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The green line may note be correct. What compiler errors do you get. Maybe you want this:
    Code:
    /* ADD NODES */
    void AddNodes(Node **head, int n)
    {
       Node *last, *new;
       do 
       {
             last = LastNode(*poc);
             new = NewNode(last->data);
             last->next = new;
       } while (last->data < n)
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The description of 'AddNodes' doesn't really match with what it's actually doing either. Say you make one new node, and you put a 1 in it. Now, you call this with a three:
    Code:
    do
        last = 1
        new = NN(1)
        last->next = new = 1
    check to see that 1 is less than 3
    do that indefinitely, because last always = 1
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM

Tags for this Thread


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