首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >程序不是打印我需要打印的东西。

程序不是打印我需要打印的东西。
EN

Stack Overflow用户
提问于 2016-11-04 15:31:09
回答 2查看 71关注 0票数 0

我想要创建一个排序链接列表,所以每次我们插入一个元素时,它都应该被排序(按升序排列)。现在我做了一个排序的链接列表。这个计划运转良好,但有一个问题。它没有在排序链接列表中打印中间和最后插入的元素,即使它是插入的。

我的代码是

代码语言:javascript
运行
复制
 //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;
}

该程序的输出如下:

代码语言:javascript
运行
复制
1
2
3

我正在使用Visual 2012终极版。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-04 16:20:20

这是一个简化的版本。它在开始时检查新节点是否属于列表的首位,作为特例。它循环遍历列表,直到找到列表的正确位置或结束为止。

代码语言:javascript
运行
复制
// 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;
}
票数 1
EN

Stack Overflow用户

发布于 2016-11-04 15:55:26

插入列表中间的逻辑是错误的:

代码语言:javascript
运行
复制
temp2->next = temp->next;
temp->next = temp2;

此时,您希望在temp之后插入新节点temp2。但是,您有temp2,后面是temp->next的初始值,即NULL,然后是temp,后面跟着temp2 (与您想要的正好相反)。

所以你有这个:

代码语言:javascript
运行
复制
           ----------------
temp -->   | num  |  NULL |
           ----------------

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

你想要这个

代码语言:javascript
运行
复制
                                temp --|
                                       v
           ----------------     ----------------    ----------------
temp2 -->  | v1   |   . --|---> | num   |   . --|--> | v2   |   . --|--> ...
           ----------------     ----------------    ----------------

所以你就这样做:

代码语言:javascript
运行
复制
temp->next = temp2->next;
temp2->next = temp;

另外,在赋值或检查空指针时,请始终使用NULL而不是0。在大多数情况下,他们是一样的,但标准并没有说他们必须是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40426441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档