Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Linked List Problem(add node)

2011-03-20
2012-09-26
  • here's my code I can't seem to print the other values(5 and 2 ) my code only
    prints out the number 8, what seems to be the problem with my code?

    include<stdio.h>

    include<stdlib.h>

    struct node
    {
    int x;
    struct node next;
    };
    void add(struct node
    root,int y)
    {

    struct node *conductor;
    root = malloc(sizeof(struct node));
    conductor=root;
    conductor->next=0;
    conductor->x=y;

    }

    void display()
    {
    struct node *conductor;
    while(conductor->next!=NULL);
    {
    printf("%d\n",conductor->x);
    conductor=conductor->next;
    }
    }
    int main()
    {
    struct node root;
    add(&root,5);
    add(&root,2);
    add(&root,8);
    display(root);

    system("\npause");
    }

     
  • Daniel
    Daniel
    2011-04-16

    Hi, I have also faced such type of problems so many time during prgramming so
    I hope I will also get so many solutions for such type of problem. So thanks
    for starting this thread. Thanks!


    environmental sustainability software

     
  • Jim Pattee
    Jim Pattee
    2011-09-20

    You need to keep a pointer to the first node and start the display with that
    node.
    You are starting the display with the updated root variable which contains the
    last node.

     
  • Aris
    Aris
    2012-03-28

    void add(struct node *root,int y)
    {
       //why do you create "conductor" pointer root isn't ok??
       struct node *conductor;
       root = malloc(sizeof(struct node));
       conductor=root;
       conductor->next=0;
       conductor->x=y;
    }
    
    void display() // no arguments!!! 
    {
       // you create "conductor" a local pointer. Where is this pointer
       // points??
       struct node *conductor;
       //while(conductor->next!=NULL) stops
       while(conductor->next!=NULL);//here because of ;
       {
          printf("%d\n",conductor->x);
          conductor=conductor->next;                           
       }
       // conductor->next is NULL because you don't change its value
       // anywhere....
    }
    
    int main() // its better int main(void)
    {
       //you create a new node
       struct node root;
    
       // you pass &root as argument &root to add function
       // but inside add you dont use this node, you create a new
       // node with root = malloc(sizeof(struct node)); command
       // this root you create inside add function, is not linked with
       // the root that you pass as argument here.
       add(&root,5);
       add(&root,2);
       add(&root,8);
       // you pass root by value to display function but,you defined
       // display function
       //as void display() no arguments!!!
    
       display(root);
    
       system("\npause");
    
       return 0;
       // this command say's to Operating System that programms execution
       // end normaly.
    }
    
    // You don't link nodes each other anywhere
    // You create a node in main, then you pass it by value in add()
    // inside add() you lose any conection with main's node because you
    // create a new with malloc and make next to point to NULL. So your
    // are not linked.
    
    // function() mean that function doesn't make check of the
    // passing arguments.
    
    /code]
    
    [b] This programm could be like this
    don't just copy it....
     try to understant it
    [/b]
    
    [code]
    #include<stdio.h>
    #include<stdlib.h>
    
    struct node
    {
       int x;
       struct node *next;
    };
    
    void add(struct node *node_ptr,  int y)
    {
       while(node_ptr->next != NULL)
       {
          node_ptr = node_ptr->next;
       } //goes to the last node
    
       struct node *new_node = malloc(sizeof(struct node));
       new_node->next = NULL;
       new_node->x = y;
    
       node_ptr->next = new_node;
    
       return;   
    }
    
    void display(struct node * node_ptr)
    {
       do
       {
          printf("%d\n",node_ptr->x);
          node_ptr=node_ptr->next;
       }
       while(node_ptr->next != NULL);
    
       printf("%d\n",node_ptr->x); // print the last one
    
       return;
    }
    
    int main()
    {
       struct node *root = malloc(sizeof(struct node)); //first node
       root->next = NULL;
       root->x = 5;
    
       add(root,2);
       add(root,8);
       display(root);
    
       system("\n\npause");
       return 0;
    }
    [/code]
    
     
  • Aris
    Aris
    2012-03-28

    i wrote int main()
    is better
    int main(void)