簡體   English   中英

將值按升序插入排序的鏈表中

[英]Insert value into sorted linked list in ascending order

此函數將要求用戶輸入一個整數,然后將其按升序插入到鏈表中。 如果當前值已存在,將不會插入。

typedef struct _listnode{
    int item;
    struct _listnode *next;
} ListNode;         

typedef struct _linkedlist{
    int size;
    ListNode *head;
} LinkedList;           

void insertSortedLinkedList(LinkedList *l)
{
    ListNode *cur = l->head;
    ListNode* newNode = malloc(sizeof(ListNode)); // create the node to be inserted
    int x;
    printf("please input an integer you want to add to the linked list:");
    scanf("%d", &x);
    newNode->item = x;
    newNode->next = NULL;

    if (l->head == NULL) // linkedlist is empty, inserting as first element
    {
        l->head = malloc(sizeof(ListNode));
        l->head->item = x;
        l->head->next = NULL;
        l->size++;
    }
    else
    {

        if (x < l->head->item) // data is smaller than first element, we will insert at first element and update head.
        {
            newNode->next = l->head;
            l->head = newNode;
            l->size++;
            return;
        }
        while (cur->next != NULL) // loop through the linkedlist
        {
            if (cur->next->item > x) // next element is bigger than data, we will insert it now.
            {
                if (cur->item != x) // if current element is not same as data, it must not have already existed.
                {
                    newNode->next = cur->next;
                    cur->next = newNode;
                    l->size++;
                    return;
                }
            }
            if (cur->next == NULL) // we have reached the last element and data is even greater than that. we will then insert it as last element.
            {
                cur->next = newNode;
                l->size++;
                return;
            }
            cur = cur->next;
        }
    }
}

不知何故,其中有一個錯誤。 當我嘗試插入以下內容時,我得到了這些結果。 如果數據大於現有數據,則也不會插入。

Insert : 10
Result : 10
Insert : 5
Result : 5 10
Insert : 8
Result : 5 8 10
Insert : 10
Result : 5 8 10
Insert : 7
Result : 5 7 8 10
Insert : 9
Result : 5 7 8 9 10
Insert : 6 
Result : 5 6 7 8 9 10
Insert : 5
Result : 5 6 5 7 8 9 10 << why?

您在錯誤的位置測試是否相等:始終跳過第一個節點。 您還需要改進分配方案:您為頭節點分配了兩次內存,如果整數已經在列表中,則忘記釋放內存。

這是一個改進的版本:

void insertSortedLinkedList(LinkedList *l)
{
    ListNode *cur, *newNode;
    int x;

    printf("please input an integer you want to add to the linked list:");
    if (scanf("%d", &x) != 1)
        return;

    newNode = malloc(sizeof(ListNode)); // create the node to be inserted
    newNode->item = x;
    newNode->next = NULL;

    if (l->head == NULL)
    {
        // linkedlist is empty, inserting as first element
        l->head = newNode;
        l->size++;
        return;
    }
    if (x < l->head->item)
    {
        // data is smaller than first element, we will insert at first element and update head.
        newNode->next = l->head;
        l->head = newNode;
        l->size++;
        return;
    }
    for (cur = l->head;; cur = cur->next) // loop through the linkedlist
    {
        if (cur->item == x)
        {
            // element already in the list
            free(newNode);
            return;
        }
        if (!cur->next || cur->next->item > x)
        {
            // next element is bigger than data or end of list, we will insert it now.
            newNode->next = cur->next;
            cur->next = newNode;
            l->size++;
            return;
        }
    }
}

可以使用指向鏈接的指針來簡化此代碼:

void insertSortedLinkedList(LinkedList *l)
{
    ListNode **curp, *cur, *newNode;
    int x;

    printf("please input an integer you want to add to the linked list:");
    if (scanf("%d", &x) != 1)
        return;

    for (curp = &l->head; (cur = *curp) != NULL; curp = &cur->next) {
        if (cur->item == x)
            return;
        if (cur->item > x)
            break;
    }
    // cur element is bigger than data or end of list, we will insert it now.
    newNode = malloc(sizeof(ListNode)); // create the node to be inserted
    newNode->item = x;
    newNode->next = cur;
    *curp = newNode;
    l->size++;
}

問題是您永遠不會達到if條件,因為當cur->next == NULL您的循環會中斷:

while (cur->next != NULL) // loop through the linkedlist
    ....
        if (cur->next == NULL) // we have reached the last element and data is even greater than that. we will then insert it as last element.
        {
           ...
        }
    ...
    }

相反,您應該使用while(cur != NULL)作為循環,並將cur->next == NULL為第一個if條件,以便在cur == NULL時, (cur->next->item不會使程序崩潰。 cur == NULL

注意:在這種情況下,您的循環條件將不重要,因為if (cur->next == NULL)將破壞循環, if (cur->next == NULL) return in。 它不退出環只有重要的是你得到執行之前if -condition。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM