C - 按升序插入链表

时间:2014-02-14 20:05:35

标签: c linked-list

我正在尝试创建一个程序,按升序将数字插入到链表中。这是我的插入功能。它适用于插入一些数字而不是其他数字。我认为它与最后一部分有关,但我无法弄明白。

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;

}

4 个答案:

答案 0 :(得分:4)

以下部分错误

//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;

e.g。像这样解决:

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

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;

答案 1 :(得分:0)

您需要在最后一次while循环中检查temp->next->value

答案 2 :(得分:0)

//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;
           }
    }
}

答案 3 :(得分:0)

如果您首先实现(和测试)以下函数会更好:var s = "abcdef"; var validate = function(name){ if(name!==""&&name.length<30&&name.match(/^[A-Za-z]+$/)){ console.log("right"); return true; }else{ console.log("wrong"); return false; } }; if(validate(s)){ console.log("s was ok"); }else{ console.log("s was not ok"); }push_front()(之前插入)和insert(),(可能是push_back())然后简单地考虑插入的所有可能性,即:

  • 空列表(当前节点是第一个,所以只有advance (Node* curr, int steps);
  • 对来自push_front / back()及其后的所有元素进行迭代(advance()),直到:
      找到值大于new的
    • 元素,head之前。
    • 到达最后一个元素insert()

在新功能push_back()中。