簡體   English   中英

程序沒有打印我需要打印的內容

[英]Program is not printing what i need to print

我想創建一個排序的鏈表,所以每次插入一個元素時,都應該對其進行排序(以升序排列)。 現在,我做了一個排序的鏈表。 該程序運行正常,但是有一個問題。 即使已插入,也不會在排序的鏈表中打印中間插入的元素和最后插入的元素。

我的代碼是

 //Libraries
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>


struct Node{

    int val;
    struct Node  *next;
};

struct LLADT{

    struct Node *head;
};


// Initializing Linked List
void init(struct LLADT *LL){
    LL->head = 0;
}

// Sorted Inserted Linked List Function
void sortInsert(struct LLADT *LL, int num){
    struct Node *temp;
    struct Node *temp2;
    temp = (struct Node*)malloc(sizeof(struct Node));



// If list is Empty
    if(LL->head == 0){
        temp-> val = num;
        temp->next = NULL;
        LL->head = temp;
        return;
    }

// When list is not empty and key is smallest

    if(num < LL->head->val){
        temp-> val = num;
        temp->next = LL->head;
        LL->head = temp;
        return;

    }
// When list is not empty and we have to insert in middle
    temp2 = LL->head;
    temp-> val = num;
    temp->next = NULL;
    while(temp2->next !=0){
        if(num > temp2->next->val){
            temp2 = temp2->next;
        }
        else{

            temp2->next = temp->next;
            temp->next = temp2;
            return;
        }

    }


// When list is not empty and we have to insert at the end
    if(num > temp->val){

        temp->val = num;
        temp->next = NULL;
        temp2->next = temp;
        return;


    }
}

//Printing Linked List
void print_list(struct LLADT *LL){
    struct Node *temp;
    temp = LL-> head;
    while(temp !=0){
        printf("%d\n", temp->val);
        temp = temp -> next;
    }
}

// Main Function
int main(){

    struct LLADT LL;
    init(&LL);

    // inserting
    sortInsert(&LL,17);
    sortInsert(&LL,3);
    sortInsert(&LL,5);
    sortInsert(&LL,2);
    sortInsert(&LL,1);
    sortInsert(&LL,20);

    //Printing
    print_list(&LL);
    getch();
    return 0;
}

該程序的輸出為:

1
2
3

我正在使用Visual Studio 2012 Ultimate。

插入列表中間時,您的邏輯有誤:

temp2->next = temp->next;
temp->next = temp2;

此時,您想在temp2之后插入新節點temp 但是,相反,您有temp2后跟temp->next的初始值NULL ,然后是temp后跟temp2 (與您想要的相反)。

所以你有這個:

           ----------------
temp -->   | num  |  NULL |
           ----------------

           ----------------     ----------------
temp2 -->  | v1   |   . --|---> | v2   |   . --|--> ...
           ----------------     ----------------

而您想要這樣:

                                temp --|
                                       v
           ----------------     ----------------    ----------------
temp2 -->  | v1   |   . --|---> | num   |   . --|--> | v2   |   . --|--> ...
           ----------------     ----------------    ----------------

所以你這樣做:

temp->next = temp2->next;
temp2->next = temp;

附帶說明一下,分配或檢查NULL指針時,請始終使用NULL而不是0 在大多數情況下,它們是相同的,但是標准並沒有說必須如此。

這是簡化版本。 它會在開始時檢查新節點是否屬於列表的開頭(作為特殊情況)。 它循環遍歷列表,直到找到正確的位置或列表的末尾。

// Sorted Inserted Linked List Function
void sortInsert(struct LLADT *LL, int num)
{
    struct Node *newNode;
    struct Node *currentNode;
    struct Node *previousNode;

    // Initialise a new node for the new value.
    newNode = malloc(sizeof(struct Node));
    newNode->val = num;

    // If list is Empty or the new node is first in the list.
    if((NULL == LL->head) || (num < LL->head->val))
    {
        newNode->next = LL->head;
        LL->head = newNode;
        return;
    }

    // Iterate until last element or found position
    currentNode = LL->head;
    while((NULL != currentNode)&&(num >= currentNode->val))
    {
        // Move on to the next element
        previousNode = currentNode;
        currentNode = currentNode->next;
    }

    // Insert the new element between the previous and current
    previousNode->next = newNode;
    newNode->next = currentNode;
}

暫無
暫無

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

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