# Thread: algorithms to creating list

1. ## algorithms to creating list

I am having trouble to create linked list I made a rough algorithm to create link list to understand basics

Step 1 : Create a New node
Step 2 : assign data to data member of the new node
Step 3 : if head is empty go to step 4 else else go to step 4
step 4 : make new node as head node and goto step 5
Step 5 : End

step 1 : code for step 1 : Create a New node

Code:
```struct Node{
int Number;
struct Node *next;
}```
Step 2 : code for step 2 : assign data to data member of the new node
Code:
```// assign data to Node
Node->Number = 2;```
Step 3 : Code for step 3 : if head is empty go to step 4 else else go to step 4

Code:
```head=NULL;
End ;```
Hope anyone who is familiar with this can help me to understand basics

2. Step 0, declare a Node structure
Code:
```struct Node{
int Number;
struct Node *next;
}```
Step 1, create a Node
Code:
```struct Node* newNode(int number, struct node *next) {
struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
}```
Step 2, an empty list
Code:
```int main ( ) {
struct Node *list = NULL;
}```
Code:
```int main ( ) {
struct Node *list = NULL;
list = newNode(1,list);
}```
Step 4, add another node (at the front)
Code:
```int main ( ) {
struct Node *list = NULL;
list = newNode(1,list);
list = newNode(2,list);
}```

3. Originally Posted by Salem
Step 0, declare a Node structure

Step 1, create a Node
Hi Salem

It was nice explanations but I do not understand step 1

I do not return type in the below function. I haven't seen this type of function deceleration in c language
Code:
```struct Node* newNode(int number, struct node *next) {
struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
}```
as per my understand I have tried to write in one program

Code:
```#include<stdio.h>#include<stdlib.h>

struct Node{
int Number;
struct Node *next;
}
// does it valid function in c
struct Node* newNode(int number, struct node *next) {
struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
}

int main ( ) {
struct Node *list = NULL;
list = newNode(1,list);
list = newNode(2,list);

return 0;
}```

4. You need to return the pointer to the new node.

5. Well sure, you can mush it all together in main if you want.

But learning about functions, how to pass parameters and how to return results should be HIGH on your list of things to do next.

Here, if only to show you how tedious and painful it is to keep copy/pasting.
Code:
```int main ( ) {
struct Node *list = NULL;

struct Node *new1 = malloc(sizeof(*new1));
new1->Number = 1;
new1->next = list;
list = new1;

struct Node *new2 = malloc(sizeof(*new2));
new2->Number = 2;
new2->next = list;
list = new2;
}```

6. Originally Posted by laserlight
You need to return the pointer to the new node.
I am doing

Code:
```struct Node* newNode(int number, struct node *next) {    struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
return new;
}```

7. Originally Posted by Salem
Well sure, you can mush it all together in main if you want.

But learning about functions, how to pass parameters and how to return results should be HIGH on your list of things to do next.
I am agree with you. I have studied easy example of functions to pass the parameter and return result. I would work on something complex such as passing the structure and I feel I am lacking somewhere in structure

But I am on my point I have never seen this type of declaration

Code:
```structNode* newNode(intnumber, structnode *next) {    struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
}```
If you don't mind How would you convert your steps made in post 2 into program.

I tried as per my best but I couldn't get result because of my bad understanding

8. Originally Posted by laserlight
You need to return the pointer to the new node.
I am sure this function do what you suggested
Code:
```structNode* newNode(intnumber, structnode *next) {    structNode *new= malloc(sizeof(*new));    new->Number = number;
new->next = next;
return new;
}```
Does it function return pointer to new node

9. Great, so what is your current code and how does it not work?

10. Originally Posted by laserlight
Great, so what is your current code and how does it not work?
laserlight Thanks for watching this post

I need to spend more time to learning and I am trying it.

I want to display list and I do not understand how to print each node
Code:
```#include<stdio.h>#include<stdlib.h>
struct Node{
int Number;
struct Node *next;
}

struct Node* newNode(int number, struct node *next) {
struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
return new;
}

int main ( ) {

struct Node *list = NULL;
list = newNode(1,list);
printf ("list : %d ", list);
list = newNode(2,list);
list = newNode(3,list);

return 0;
}```

Test.c:9:1: error: expected ';', identifier or '(' before 'struct'
struct Node* newNode(int number, struct node *next) {
^~~~~~
Test.c:9:41: warning: 'struct node' declared inside parameter list will not be visible outside of this definition or declaration
struct Node* newNode(int number, struct node *next) {
^~~~
Test.c: In function 'newNode':
Test.c:12:15: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
new->next = next;
^
Test.c: In function 'main':
Test.c:19:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
list = newNode(1,list);
^~~~
Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
struct Node* newNode(int number, struct node *next) {
^~~~~~~
Test.c:23:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
list = newNode(2,list);
^~~~
Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
struct Node* newNode(int number, struct node *next) {
^~~~~~~
Test.c:24:22: warning: passing argument 2 of 'newNode' from incompatible pointer type [-Wincompatible-pointer-types]
list = newNode(3,list);
^~~~
Test.c:9:14: note: expected 'struct node *' but argument is of type 'struct Node *'
struct Node* newNode(int number, struct node *next) {

11. Apparently you don't understand the concept of a pointer, think of pointers as a phone and the data you want to pass as the phone numbers, when you want to pass a number you don't draw the phone and pass that you read the number on the display what you did is use the pointer which the compiler saw as unfit for what you attempted to use it for and thus refused to continue just as a person would refuse to take a drawing of a phone as a phone number (unless they're from stone age or tremendously stupid)

13. Originally Posted by Salem
I have corrected errors so How to call the function in main
Code:
```#include<stdio.h>#include<stdlib.h>
struct Node{
int Number;
struct Node *next;
};

struct Node *newNode(int number, struct node *next) {
struct Node *new = malloc(sizeof(*new));
new->Number = number;
new->next = next;
return new;
}

int main ( ) {

return 0;
}```

14. That's not correction, that's removal

15. > I have corrected errors so How to call the function in main
Round and round we go,
algorithms to creating list