• 09-11-2009
budala
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) }```
• 09-11-2009
quzah
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.
• 09-11-2009
C_ntua
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) }```
• 09-11-2009
quzah
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.