# Thread: What are the ways of adding nodes in a linked list

1. ## What are the ways of adding nodes in a linked list

I've read the tutorial that I found here about linked list and confuses me. but I do get like 80%. my problem is now how do I add or create a node? multiple nodes to be exact for example . the first node contains the value of 29, while the next node is 56. what are the proper ways of adding a node? also is it good to always use pass by reference?

2. Well I normally write a function or two

node *add_to_front ( node *list, node *node );
This adds node to the front of list, and returns the new list.

node *add_to_back( node *list, node *node );
This adds node to the back of list, and returns the new list.

So you would typically have in main
Code:
```node *list = NULL;
while ( some condition ) {
node *newNode = malloc( sizeof *newNode );
// assign members
list = add_to_front( list, newNode );
}```

3. also is it good to always use pass by reference?
Depends....
In some cases, it's recommended to pass by reference but mostly yes we try to pass by reference...

what are the proper ways of adding a node?
Second, add the node :-)
Suppose i got 5,12,34,3,9 as data part and i want to insert in order.
So first it's 5, i'll check if there is already a node, if no, i'll create node and put my data.
Then 12, i'll compare it with 5, and insert to the next.
34, greate than 12, insert in front.
3, smaller than 34, go to the rear of 34, it's 12, compare with 12, again it's smaller, go to the rear of 12, it's 5, compare and smaller, go to the rear of 5, it's null, now two ways.
i). Create new node and point it's next to 5.
ii). From 5's rear, create a node whom 5 will point..

4. Originally Posted by Mr.777
Depends....
In some cases, it's recommended to pass by reference but mostly yes we try to pass by reference...

Second, add the node :-)
Suppose i got 5,12,34,3,9 as data part and i want to insert in order.
So first it's 5, i'll check if there is already a node, if no, i'll create node and put my data.
Then 12, i'll compare it with 5, and insert to the next.
34, greate than 12, insert in front.
3, smaller than 34, go to the rear of 34, it's 12, compare with 12, again it's smaller, go to the rear of 12, it's 5, compare and smaller, go to the rear of 5, it's null, now two ways.
i). Create new node and point it's next to 5.
ii). From 5's rear, create a node whom 5 will point..
I get the whole point, now my only troubles is actually coding it,

but I do know that the first node is suppose to be null, but if you to add more data you add a node right? . well usually I add my node like this . I am just talking about basics since I am quite confused on linked list specifically adding nodes. I do get its concept but the coding confuses me.

I do create my node like this
Code:
```struct node *addnode(struct node *root , int x)

{ struct node *conductor;
conductor= root;
conductor=(sizeof(struct node));

conductor->x=25;
conductor->next=Null

}```
am I doing this correctly?

5. You should perform some checks. Like;
Code:
```if(root!=NULL)
{
}
else
{
}```

6. How will I know if it's correct? if it prints a digit?

7. What exactly are you asking about?
Your root pointer should always point to the first node in the whole linked list.. Ain't this?

8. Originally Posted by Mr.777