What are the ways of adding nodes in a linked list

This is a discussion on What are the ways of adding nodes in a linked list within the C Programming forums, part of the General Programming Boards category; I've read the tutorial that I found here about linked list and confuses me. but I do get like 80%. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    66

    What are the ways of adding nodes in a linked list

    I've read the tutorial that I found here about linked list and confuses me. but I do get like 80%. my problem is now how do I add or create a node? multiple nodes to be exact for example . the first node contains the value of 29, while the next node is 56. what are the proper ways of adding a node? also is it good to always use pass by reference?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well I normally write a function or two

    node *add_to_front ( node *list, node *node );
    This adds node to the front of list, and returns the new list.

    node *add_to_back( node *list, node *node );
    This adds node to the back of list, and returns the new list.

    So you would typically have in main
    Code:
    node *list = NULL;
    while ( some condition ) {
        node *newNode = malloc( sizeof *newNode );
        // assign members
        list = add_to_front( list, newNode );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    also is it good to always use pass by reference?
    Depends....
    In some cases, it's recommended to pass by reference but mostly yes we try to pass by reference...

    what are the proper ways of adding a node?
    First, think about your problem.
    Second, add the node :-)
    Suppose i got 5,12,34,3,9 as data part and i want to insert in order.
    So first it's 5, i'll check if there is already a node, if no, i'll create node and put my data.
    Then 12, i'll compare it with 5, and insert to the next.
    34, greate than 12, insert in front.
    3, smaller than 34, go to the rear of 34, it's 12, compare with 12, again it's smaller, go to the rear of 12, it's 5, compare and smaller, go to the rear of 5, it's null, now two ways.
    i). Create new node and point it's next to 5.
    ii). From 5's rear, create a node whom 5 will point..
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Quote Originally Posted by Mr.777 View Post
    Depends....
    In some cases, it's recommended to pass by reference but mostly yes we try to pass by reference...


    First, think about your problem.
    Second, add the node :-)
    Suppose i got 5,12,34,3,9 as data part and i want to insert in order.
    So first it's 5, i'll check if there is already a node, if no, i'll create node and put my data.
    Then 12, i'll compare it with 5, and insert to the next.
    34, greate than 12, insert in front.
    3, smaller than 34, go to the rear of 34, it's 12, compare with 12, again it's smaller, go to the rear of 12, it's 5, compare and smaller, go to the rear of 5, it's null, now two ways.
    i). Create new node and point it's next to 5.
    ii). From 5's rear, create a node whom 5 will point..
    I get the whole point, now my only troubles is actually coding it,

    but I do know that the first node is suppose to be null, but if you to add more data you add a node right? . well usually I add my node like this . I am just talking about basics since I am quite confused on linked list specifically adding nodes. I do get its concept but the coding confuses me.

    I do create my node like this
    Code:
    struct node *addnode(struct node *root , int x)
    
    { struct node *conductor;
      conductor= root;
      conductor=(sizeof(struct node));
      
    conductor->x=25;
    conductor->next=Null
    
    }
    am I doing this correctly?

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You should perform some checks. Like;
    Code:
    if(root!=NULL)
    {
    //your code here
    }
    else
    {
    //your code here
    }
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    How will I know if it's correct? if it prints a digit?

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    What exactly are you asking about?
    Your root pointer should always point to the first node in the whole linked list.. Ain't this?
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Quote Originally Posted by Mr.777 View Post
    What exactly are you asking about?
    Your root pointer should always point to the first node in the whole linked list.. Ain't this?
    wait nevermind what I posted about printing xD.

    yes the first node in the whole linked list XD. if you don't mind can you provide me a sample of a program that has 2 nodes and has values in it? so I could understand just a little bit of how to code them, I do get the concept really. but the coding is the only thing that's bothering me

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Since this is pretty much the same as your other thread, I'll close it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circularly linked list
    By BlackOps in forum C Programming
    Replies: 0
    Last Post: 07-18-2009, 08:12 AM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:13 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21