3
\$\begingroup\$

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;
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – MikeCAT Jun 24 '16 at 13:44
  • \$\begingroup\$ The input part should be merged into one set of code, not two like copied and pasted. \$\endgroup\$ – MikeCAT Jun 24 '16 at 13:44
  • 1
    \$\begingroup\$ make else if (i=N-1) to else if (i==N-1) \$\endgroup\$ – Cherubim Anand Jun 24 '16 at 13:49
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Jonathan Leffler Jun 24 '16 at 14:32
  • \$\begingroup\$ ya @JonathanLeffler I did notice that :) , anyway the OP is again assigning the value of i with the same value that it already has \$\endgroup\$ – Cherubim Anand Jun 24 '16 at 14:37
3
\$\begingroup\$

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
    
            Node_c->link=malloc(sizeof(Node));
            Node_c=Node_c->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",&temp);
            Node_c->value=temp;
            //to here
    
            Node_c->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",&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;
Node * Node_c=head;
for(i=0;i<N;i++)
{
    if(i!=0) //not required once again for head
    {
        Node_c->link=malloc(sizeof(Node));
        Node_c=Node_c->link;
    }

    //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);

    Node_c->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
\$\endgroup\$
  • \$\begingroup\$ Thank you for the answer but i can't understand why it should be "if(i!=0)" \$\endgroup\$ – Giorgio M. Jun 24 '16 at 14:12
  • 1
    \$\begingroup\$ see the description i gave in the edited answer @GiorgioM. to be precise... that's because i=0 refers to head node and you need not assign once again memory to head node and link to a previous node because it's the first node and has no previous nodes \$\endgroup\$ – Cherubim Anand Jun 24 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.