【发布时间】:2015-04-11 16:06:07
【问题描述】:
我正在尝试将链表中的数字从小到大排序。
但它不起作用!
当我将第二个数字放入 列表(主要)但我不知道为什么。
有什么帮助吗?
#include<stdio.h>
#include<stdlib.h>
typedef struct list list;
struct list{
int a;
list *nxt;
};
void sort(list *l){
int temp,tp;
list *AIDE,*k;
k=AIDE=(list*)malloc(sizeof(list));
while (l->nxt!= NULL)
{
while (l->nxt->a < l->a)
{
temp=l->a;
l=l->nxt;
l->nxt->a=temp;
l=l->nxt;
while (l->a < AIDE->nxt->a )
{
tp=AIDE->a;
AIDE->a=l->a;
AIDE->nxt->a=tp;
AIDE=AIDE->nxt;
}
}
l=l->nxt;
}
while (k->nxt!= NULL)
{
l->a=k->a;
l=l->nxt;
k=k->nxt;
}
l->nxt=NULL;
}
int main() {
list *t,*s;
int n,i,c=0;
printf("\n how many number you need to enter? ");
scanf("%d",&n);
s=t=(list*)malloc(sizeof(list)*n);
while (c!=n)
{
printf("\n Donner le nb %d :",c+1);
scanf("%d",&t->a);
t=t->nxt;
c++;
}
t->nxt=NULL;
sort(s);
while (t->nxt!=NULL)
{
printf("%d",t->a);
}
return 0;
}
【问题讨论】:
-
您是否尝试过在调试器中逐行执行代码?
-
是的,问题就在这里:while (c!=n) { printf("\n Donner le nb %d :",c+1); scanf("%d",&t->a); t=t->nxt; C++; } 当我输入第二个数字时,程序粉碎并停止工作
-
你得到什么错误?
标签: c pointers linked-list