C - Insert in a linked list in ascending order

I am trying to create a program that inserts numbers into a linked list in ascending order. This is my insert function. It works for entering some numbers, but not for others. I think this has something to do with the last part, but I can't figure it out.

node* insert(node* head, int value) {

    //check if head hasn't been created
    if (head == NULL) {
        head = malloc(sizeof(node));
        if(head == NULL) {
            printf("Failed to create head node");
            return head;
        }
        head->value = value;
        head->next = NULL;
        return head;
    }

    //create a new node
    node *newNode;
    newNode = malloc(sizeof(node));
    if(newNode == NULL) {
        printf("Failed to create node");
        return newNode;
    }
    newNode->value = value;
    newNode->next = NULL;

    //see if new node should be placed before head
    if (value < head->value) {
        newNode->next = head;
        return newNode;
    }

    //search through to find correct spot and insert the node
    node *temp = NULL;
    temp = head;
    while(temp->next != NULL && temp->value < value) {
        temp = temp->next;
    }
    newNode->next = temp->next;
    temp->next = newNode;
    return head;

}
+3
source share
4 answers

Part of the following bad

//search through to find correct spot and insert the node
node *temp = NULL;
temp = head;
while(temp->next != NULL && temp->value < value) {
    temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;

eg. fix like this:

node *temp ,*prev;
temp = head;
while(temp != NULL && temp->value <= value) {
    prev = temp;
    temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;

or

node *temp ,*prev;
temp = head->next;
prev = head;
while(temp != NULL && temp->value < value) {
    prev = temp;
    temp = temp->next;
}
newNode->next = temp;
prev->next = newNode;
+4
source

You need to check temp->next->valueinside the last while loop.

0
source
//This code of mine works perfectly.

void insertInAscOrder(int val) 
{
    node *new1;
    node *temp;
    node *previous; 

    //create new node
    new1 = (node *)malloc(sizeof(node)); 

    //check whether node is created or not
    if(new1 == NULL) 
    {
        printf("Insufficient memory.");
        return;
    }   

    //Updating different parts of the node
    new1 -> info = val;
    new1 -> next = NULL;    

    //checking whether the node created is only node or not
    if (start == NULL) 
    {       
        start = new1;
    } 
    //If value is less than the value of first node
    else if(val < start -> info) 
    {
        new1 -> next = start;
        start = new1;
    } 
    else 
    {   
        previous = start;
        temp = start -> next;


            //Go to the position where node is to be inserted
            while(temp != NULL && val > temp -> info) 
            {
                previous = temp;
                temp = temp -> next;
            }


            //Insert the node at particular position
           if(temp == NULL) 
           {
                   previous -> next = new1;
           } 
           else 
           {
                   new1 -> next = temp;
               previous -> next = new1;
           }
    }
}
0

, ( ) , : push_front(), insert() (insert before) push_back(), (, advance (Node* curr, int steps);), , ..

  • ( node , push_front / back())
  • (advance()) head , :
    • an item with a value greater than the new one insert()before it.
    • last element reached push_back().

in your new feature insert_ordered().

0
source

All Articles