gpt4 book ai didi

c - 在 C 中对链表进行排序时的无限循环

转载 作者:太空宇宙 更新时间:2023-11-04 01:02:20 26 4
gpt4 key购买 nike

我是 C 编程的新手,我必须对我的链表进行排序。该链表表示已由用户输入创建的多项式。每个节点都有一个系数和一个幂。当我尝试对列表进行排序并将其返回到打印它的主函数时,没有打印任何内容(我认为我的程序永远不会退出 while 循环)。

我在这里创建结构:

struct node
{
int sunt;
int dun;
struct node* next;
};
typedef struct node* Poly;

这是我对列表进行排序的代码(head 是我的初始列表名称):

struct node* poly = head;

struct node* temp;
int length = 0;

while (poly != NULL)
{
length++;
poly = poly->next;
}

poly = head;

while (length > 0)
{
int i = 0;
while ((poly != NULL) && (poly->next != NULL))
{
if (poly->dun > poly->next->dun)
{
temp = poly;
poly = poly->next;
poly->next = temp;
if(i = 0)
{
head = poly;
}
i ++;
}
else
{
if(i = 0)
{
head = poly;
}
i ++;
}
poly = poly->next;
}
length --;
poly = head;
}
return head;

这是我在 main 函数中打印列表的地方(POLYmake 函数是我创建和排序列表的地方):

poly1 = POLYmake();

while(poly1 != NULL)
{
printf("%d %d \n", poly1->sunt, poly1->dun);
poly1 = poly1->next;
}

我知道这很复杂,但我没有 C 语言经验,希望你能帮助我。

最佳答案

这个循环

while(length > 0)

将始终为真,因为当您完成遍历列表时,您只会将其减一。

此外,还有如下代码:

if(i = 0)
{
head = poly;
}

应该是

if(i == 0)
{
head = poly;
}

关于c - 在 C 中对链表进行排序时的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33634622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com