回复 楼主 云团
这样试试看~
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(Stu)
typedef struct Stu
{
int data;
struct Stu* next;
}Stu;
Stu* Creat_Node();
Stu* Creat();
void Print(char* p,Stu* stu);
Stu* Sort(Stu* stu);
void Del(Stu* stu);
int main()
{
Stu* stu=Creat();
Print("排序前输出数据如下:\n",stu);
stu=Sort(stu);
Print("排序后输出数据如下:\n",stu);
Del(stu);
return 0;
}
Stu* Creat_Node()
{
Stu* stu=(Stu*)malloc(LEN);
if (stu==NULL)
{
puts("分配空间失败!");
exit(0);
}
memset(stu,0,LEN);
return stu;
}
Stu* Creat()
{
int n=0;
Stu* stu=Creat_Node();
Stu* head=stu;
puts("请输入数据,输入0结束输入:");
while (scanf("%d",&n)&&n)
{
stu=stu->next=Creat_Node();
stu->data=n;
}
return head;
}
void Print(char* p,Stu* stu)
{
if (stu->next==NULL)
{
puts("该表数据为空!");
return ;
}
printf(p);
for (;stu=stu->next;printf("%-4d",stu->data));
puts("");
}
Stu* Sort(Stu* stu)
{
Stu* head=stu;
Stu* p=NULL;
int t=0;
while (p)
p=p->next;
while (stu!=p)
{
while (stu->next!=p)
{
if (stu->data>stu->next->data)
{
t=stu->data;
stu->data=stu->next->data;
stu->next->data=t;
}
stu=stu->next;
}
p=stu;
stu=head->next;
}
return head;
}
void Del(Stu* stu)
{
Stu* p=stu;
while (p=p->next)
{
free(stu);
stu=p;
}
free(stu);
}