Thread: doubly linked lists insertion

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    doubly linked lists insertion

    Hey guys please correct me if im wrong but does all the code inside the if(*head ==NULL) its just initialize each to a safe state then creating a head node in our list.

    Then if the head node is already created create prev and next pointers then create a new node with prev and next pointers to that as well.

    Code:
    void insertNode( struct node **head, int data)
    {
         struct node *tempnode, *tempnode1;
         
         if( *head == NULL )
         {
            *head = createnode();
            (*head)->next = NULL;
            (*head)->prev = NULL;
            (*head)->data = data;
         }
         else
         {
             tempnode = *head;
             
             while( empnode->next != NULL )
                tempnode = tempnode->next;
             
             tempnode1 = createnode();
             tempnode1->prev = tempnode;
             tempnode1->next = NULL;
             tempnode1->data = data;
             
             tempnode->next = tempnode1;
         }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep, but unless you have a special reason you need to append nodes on the tail of the list, you could save yourself some trouble and just prepend them.

    Code:
    if( *head )
    {
        temp = createnode( );
        if( temp )
        {
            temp->prev = NULL;
            temp->next = *head;
            temp->data = data;
            (*head)->prev = temp;
            *head = temp;
        }
    }
    It's not like you're sorting as you insert anyway, so why not take the easy way out?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Then if the head node is already created create prev and next pointers then create a new node with prev and next pointers to that as well.
    you check for head == null becuase to see is there any list already created. If there was no list created i traverse down to the end and add a new node. Or u could create a new node and add then in the front and the puch the list to grow toward to your right.

    To be honest with you the if part will normally be excuted only once in the executing of the code. Its the else past which inserts the node.

    ssharish2005

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Looks okay. If the list is empty, you add a node at the front. If the list isn't empty, you add a node ad the end. I don't think createnode() is good though because it doesn't return an error status or you don't check for it. That leaves you with ignoring the error of not being able to create a node, or just closing the program. Both are bad. I'd write it something like this:
    Code:
    struct node *createnode( int data, struct node *prev, struct node *next ) {
      struct node *new = malloc( sizeof *new );
    
      if ( new ) {
        new->data = data;
        new->prev = prev;
        new->next = next;
      }
    
      return new;
    }
    Then you can use it like this:
    Code:
    int insertNode( struct node **head, int data)
    {
         struct node *tempnode, *tempnode1;
         
         if( *head == NULL )
         {
            *head = createnode( data, NULL, NULL );
    
            if ( *head == NULL ) {
              return 0;
            }
         }
         else
         {
             tempnode = *head;
             
             while( empnode->next != NULL )
                tempnode = tempnode->next;
             
             tempnode1 = createnode( data, tempnode, NULL );
    
             if ( tempnode1 == NULL ) {
               return 0;
             }
             
             tempnode->next = tempnode1;
         }
    
         return 1;
    }

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    okk and the tail is bascially the end of the list?

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yeah.

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Thnks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Doubly Linked Lists ???????
    By Magica in forum C Programming
    Replies: 5
    Last Post: 05-04-2003, 12:14 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM