Simple program to insert values in a linked list I tried to write a simple program using linked list where I just insert the value stored in every node and then through a function print the values.

This is the code:

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

typedef struct Node {
    int value;
    struct Node* link;
} Node;

void print_node(Node * );

int main() {

    Node* head = NULL;
    head=malloc(sizeof(Node));
    if (head==NULL) {
       return 0;
    }
    printf("Insert the number of nodes :\n");
    int N;
    scanf("%d",&amp;N);
    int i;
    Node * Node_c=head;
    for (i=0;i&lt;N;i++) {
        if (i&lt;N-1) {
            int temp;
            printf("Insert value stored into the node %d:\n",i+1);
            scanf("%d",&amp;temp);
            Node_c-&gt;value=temp;
            Node_c-&gt;link=malloc(sizeof(Node));
            Node_c=Node_c-&gt;link;
        } else if (i=N-1) {
            int temp;
            printf("Insert value stored into the node %d:\n",i+1);
            scanf("%d",&amp;temp);
            Node_c-&gt;value=temp;
            Node_c-&gt;link=NULL;
        }
    }
    printf("\n");
    print_node(head);
    return 0 ;
}

void print_node(Node * head) {
    Node * Node_current = head;
    int i=1;
    while (Node_current != NULL) {
        printf("Value stored into node %d:  %d\n",i,Node_current-&gt;value);
        Node_current = Node_current-&gt;link;
        i++;
    }
} 


The code runs just fine but I want to know if this part where I insert the values stored in each node can be done in an shorter way

Node * Node_c=head;
for(i=0;i&lt;N;i++){
    if(i&lt;N-1){
        int temp;
        printf("Insert value stored into the node %d:\n",i+1);
        scanf("%d",&amp;temp);
        Node_c-&gt;value=temp;
        Node_c-&gt;link=malloc(sizeof(Node));
        Node_c=Node_c-&gt;link;
    } else if (i=N-1) {
        int temp;
        printf("Insert value stored into the node %d:\n",i+1);
        scanf("%d",&amp;temp);
        Node_c-&gt;value=temp;
        Node_c-&gt;link=NULL;
    }
}
I want to know if this part where I insert the values stored in each node can be done in an shorter way


Definitely it can be done in a shorter way!


the problem is that in your code you are taking the pain to do the same thing twice while inserting. in the for loop :

for (i=0;i&lt;N;i++) 
{
    if (i&lt;N-1)
    {

        //here
        int temp;
        printf("Insert value stored into the node %d:\n",i+1);
        scanf("%d",&amp;temp);
        Node_c-&gt;value=temp;
        // to here

        Node_c-&gt;link=malloc(sizeof(Node));
        Node_c=Node_c-&gt;link;

    } 
    else if (i==N-1) //Note: it must be == operator not =
    { 

        //the same thing again from here 
        int temp;
        printf("Insert value stored into the node %d:\n",i+1);
        scanf("%d",&amp;temp);
        Node_c-&gt;value=temp;
        //to here

        Node_c-&gt;link=NULL;

} 

The below part appears twice in your insertion logic :

    int temp;
    printf("Insert value stored into the node %d:\n",i+1);
    scanf("%d",&amp;temp);
    Node_c-&gt;value=temp;

Now can we avoid writing twice the same thing? 


... Definetely you can write that logic only once and change your insertion logic to :

int i;
Node * Node_c=head;
for(i=0;i&lt;N;i++)
{
    if(i!=0) //not required once again for head
    {
        Node_c-&gt;link=malloc(sizeof(Node));
        Node_c=Node_c-&gt;link;
    }

    //you don't require temp as you can directly access Node_c-&gt;value
    printf("Insert value stored into the node %d:\n",i+1);
    scanf("%d",&amp;Node_c-&gt;value);

    Node_c-&gt;link=NULL;

}


the logic is quite simple to understand :


first we create a memory for a node and save it's  address to the link of previous node(we don't if its a head node as it's already allocated with memory in the before part of the code and it has no previous nodes to be linked with)
then we take in the value and store in the value member of structure Node_c
then we point the link member of the structure Node_c to NULL... this goes on for N number of times