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;
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",&N);
int i;
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;
Node_c->link=malloc(sizeof(Node));
Node_c=Node_c->link;
} 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;
Node_c->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->value);
Node_current = Node_current->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<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;
Node_c->link=malloc(sizeof(Node));
Node_c=Node_c->link;
} 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;
Node_c->link=NULL;
}
}
i=N-1
seems meaningless, so I think theif
statement should be removed and it should be a simpleelse
clause. \$\endgroup\$else if (i=N-1)
toelse if (i==N-1)
\$\endgroup\$if
clause takes care of the cases wherei
is not alreadyN-1
. The assignment is wrong; it should be a comparison — or, in fact, it should be just anelse
not anelse if
so the comparison isn't needed. \$\endgroup\$i
with the same value that it already has \$\endgroup\$