满意答案
dfgjdgerg
2013.03.23
采纳率:58% 等级:12
已帮助:10057人
#include
#include
#define OK 1;
struct student
{
int num;
struct student *next;
};
//用student类型声明变量时前面加上struct
//否则使用typedef得到一个别名
//如typedef struct student student_t
void InitList(struct student *st)
{
st=(struct student *)malloc(sizeof(struct student));
st->next=NULL;
}
void InSert(int x, struct student *st)
{
struct student *TempCell;
TempCell=(struct student *)malloc(sizeof(struct student));
if(TempCell==NULL)
printf("out the List");
TempCell->num=x;
TempCell->next =st->next;
st->next=TempCell;
}
void BubbleSort(struct student *st,int n)
{
int i,j,temp;
struct student *p;
for(i=0;i
for(j=0, p=st->next;j next)
{
if(p->num > p->next->num)
{
temp=p->num;
p->num=p->next->num;
p->next->num=temp;
}
}
}
void ListTraverst(struct student *st)
{
struct student *p=st->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
//增加一个函数在程序退出时释放链表申请的内存
void ListFree(struct student *st)
{
struct student *tmp;
while (st != NULL)
{
tmp = st;
st = st->next;
free(tmp);
}
}
int main()
{
int n,i,x;
struct student *st1;
st1=(struct student *)malloc(sizeof(struct student));
st1->next=NULL;
printf("Input the List's number:\n");
scanf("%d",&n);
for(i=0;i
{
printf("Input a number:\n");
scanf("%d",&x);
InSert(x, st1);
}
ListTraverst(st1);
BubbleSort(st1,n);
ListTraverst(st1);
ListFree(st1);
printf("\n");
return 0;
}
01分享举报