insert correctly into linked list

This is a discussion on insert correctly into linked list within the C Programming forums, part of the General Programming Boards category; Code: include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; }; void insert_node (struct node **h, struct ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    insert correctly into linked list

    Code:
    include <stdio.h>
    #include <stdlib.h>
    
    struct node {
    int value;
    struct node *next;
    };
    void insert_node (struct node **h, struct node **t, int v) {
    struct node *temp;
    if ((temp = (struct node *)malloc(sizeof(struct node))) == NULL) {
    printf("Node allocation failed. \n");
    exit(1); /* Stop program */
    }
    temp->value = v; temp->next = NULL;
    if (*h == NULL) {
    *h = *t = temp;
    }
    else {
    (*t)->next = temp; *t = (*t)->next;
    }
    }
    The code above correct inserts a node to the end of the linked list. Using this code i neeed to modify to update the list so as you insert more numbers it goes in non decreasing order. EX: i already inserted numbers into linked list. 4 5 7. I want to insert number 6 so linked list would have numbers in the following order. 4 5 6 7.

    I would like to use a while loop to find the right position in the linked list to insert the value, but I am having trouble making a continuation test that works.

    Any ideas would be helpful!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You'll need a second struct node pointer to transverse the list. Init your pointer to h and while (ptr) do something.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    can you or someone elaborate in more detail because all I have is this so far now

    void ins(struct node **h, struct node **t, int x){
    struct node *temp;
    Code:
                    
             if ((temp = (struct node *)malloc(sizeof(struct node))) == NULL) 
             {
              printf("Node allocation failed. \n");
              exit(1); 
              } 
              temp->value= x;
              temp->next=NULL;
              
              if (*h == NULL) {
                 *h = *t = temp;
              }
               while((*h) != NULL){
                               (*h) = temp;
                               (*h) = temp->next;
               }
       }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    The term "second pointer" apparently didn't get my point across. If you use *h, you've just caused a memory leak. Make the line of code struct node *temp; be struct node *temp, *somethingelsehere; so that you can "play" with the pointers without loosing data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  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. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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