# 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<stdio.h>
#include<stdlib.h>

typedef struct Node {
int value;
} Node;

void print_node(Node * );

int main() {

return 0;
}
printf("Insert the number of nodes :\n");
int N;
scanf("%d",&N);
int i;
for (i=0;i<N;i++) {
if (i<N-1) {
int temp;
printf("Insert value stored into the node %d:\n",i+1);
scanf("%d",&temp);
Node_c->value=temp;
} else if (i=N-1) {
int temp;
printf("Insert value stored into the node %d:\n",i+1);
scanf("%d",&temp);
Node_c->value=temp;
}
}
printf("\n");
return 0 ;
}

int i=1;
while (Node_current != NULL) {
printf("Value stored into node %d:  %d\n",i,Node_current->value);
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<N;i++){
if(i<N-1){
int temp;
printf("Insert value stored into the node %d:\n",i+1);
scanf("%d",&temp);
Node_c->value=temp;
} else if (i=N-1) {
int temp;
printf("Insert value stored into the node %d:\n",i+1);
scanf("%d",&temp);
Node_c->value=temp;
}
}

• I suggest you should format your code properly. Also the assignment i=N-1 seems meaningless, so I think the if statement should be removed and it should be a simple else clause. – MikeCAT Jun 24 '16 at 13:44
• The input part should be merged into one set of code, not two like copied and pasted. – MikeCAT Jun 24 '16 at 13:44
• make else if (i=N-1) to else if (i==N-1) – Cherubim Anand Jun 24 '16 at 13:49
• @CherubimAnand: I agree with you, but the funny thing is that in the context, the assignment works. The previous if clause takes care of the cases where i is not already N-1. The assignment is wrong; it should be a comparison — or, in fact, it should be just an else not an else if so the comparison isn't needed. – Jonathan Leffler Jun 24 '16 at 14:32
• ya @JonathanLeffler I did notice that :) , anyway the OP is again assigning the value of i with the same value that it already has – Cherubim Anand Jun 24 '16 at 14:37

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<N;i++)
{
if (i<N-1)
{

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

}
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",&temp);
Node_c->value=temp;
//to here

}

• The below part appears twice in your insertion logic :

    int temp;
printf("Insert value stored into the node %d:\n",i+1);
scanf("%d",&temp);
Node_c->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;
for(i=0;i<N;i++)
{
if(i!=0) //not required once again for head
{
}

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


• 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