# Thread: use with a function

1. ## use with a function

can some1 help me with understanding the code
Code:
```struct Edge
{
int terminal;
struct Edge *next;
};

struct Edge *Insert_Vertex (int , struct Edge *);

struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) {
struct Edge *new1, *current;
new1 = (struct Edge *) malloc(sizeof(struct Edge));
//current = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
else {
for(current = first; current->next; current = current->next)
current->next = new1;
return (first);
}
}```
thanks.

2. How are we supposed to know what you do (or do not) understand about it already?

You could try commenting the code, and then posting the results.

Then at least we don't have to worry about things you seem to know already.

3. Code:
```struct Edge
{
int terminal;
struct Edge *next;
};

struct Edge *Insert_Vertex (int , struct Edge *);

struct Edge * Insert_Vertex (int vertex_no, struct Edge *first) { // this function takes verttex_no and pointer to the struct edge
struct Edge *new1, *current;  // we define 2 new variables with struct Edge
new1 = (struct Edge *) malloc(sizeof(struct Edge));
//current = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no; // the new1 points to terminal : its gives Vertex_no. basically
new1->next = NULL; // if the list next is null :means not pointing to anything anymore
if (!first)      // i dont follow this
return (new1);
else {
for(current = first; current->next; current = current->next) // not even this??????
current->next = new1;
return (first);
}
}```
I hope I am clear with my question

4. if (!first) is testing to see if the variable first is equal or not to 0 (NULL)
its equivalent to
Code:
```if (first == NULL)
else```

the cicle I don't get it what it is doing, either way, it could had been written as:
Code:
`for(current = first; current->next != NULL; current = current->next)`

5. thanks ....
I actually dont understand why i get error when i run my actual file (i.e 500 elements)

in Insert_Vertex (vertex_no=334, first=0x0) error at
for(current = first; current->next==NULL; current = current->next)

but dont see it when i run some small example file.(10 elements)

6. Originally Posted by noobiept
if (!first) is testing to see if the variable first is equal or not to 0 (NULL)
its equivalent to
Code:
```if (first != NULL)
else```

the cicle I don't get it what it is doing, either way, it could had been written as:
Code:
`for(current = first; current->next == NULL; current = current->next)`
Those are turned around. !first is equivalent to first == NULL, and current->next in a condition (like in an if statement or loop condition) is equivalent to current->next != NULL.

7. sorry, I got it wrong, its actually the other way around. aahh

8. Originally Posted by robwhit
Those are turned around. !first is equivalent to first == NULL, and current->next in a condition (like in an if statement or loop condition) is equivalent to current->next != NULL.

yep, was just thinking now and wasn't sounding right. you're right.

9. I still dont follow...
I understoof very much like noobiept wrote
so u say it is equivalent to :
Code:
``` if (first== NULL)      // i dont follow this
return (new1);
else {
for(current = first; current->next != NULL; current = current->next)
current->next = new1;
return (first);
}
}```
is it ???

10. Originally Posted by satty
I still dont follow...
I understoof very much like noobiept wrote
so u say it is equivalent to :
Code:
``` if (first== NULL)      // i dont follow this
return (new1);
else {
for(current = first; current->next != NULL; current = current->next)
current->next = new1;
return (first);
}
}```
is it ???
Your loop is assigning 'current->next' to 'new1' upon *each* iteration. That's not what you want, is it (hint: you're aim should be to get to the end of the list first)? Also, you should probably return the newly created node - not 'first'.

11. Either add to the head or the tail of the list, the former being the easier of the two options.
The way for loop is written currently, makes the program end up with lots of dangling pointers.