|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include"stdio.h"
- #include"stdlib.h"
- #include"string.h"
- struct student
- {
- int num;
- char name[19];
- int age;
- struct student *next;
- };
- struct student *Create(void)
- {
- int num;
- struct student *head=NULL,*p,*last;
- FILE *fp;
- fp=fopen("e:\\a.txt","r");
- fscanf(fp,"%d",&num);
- while(num!=0)
- {
- p=(struct student *)malloc(sizeof(struct student));
- p->next=NULL;
- if(head==NULL)
- {
- head=p;
- last=p;
- }
- else
- {
- last->next=p;
- last=p;
- }
- p->num=num;
- fscanf(fp,"%s%d%d",p->name,&p->age,&num);
- }
- fclose(fp);
- return head;
- }
- struct student *PaiXu(struct student *head)
- {
- struct student *p=head,*q,*r,*s,*t,*m,*n=NULL;
- while(head!=n)
- {
- q=head;
- r=q->next;
- while(r!=NULL)
- {
- if(r->next==n)
- {
- n=r;
- if(q->next > r->next)
- {
- s->next=r;
- r->next=q;
- m=r;
- r=q;
- q=m;
- }
- break;
- }
- else
- {
- t=r->next;
- if(q->num > r->num)
- {
- if(q==head)
- {
- r->next=q;
- q->next=t;
- head=r;
- m=r;
- r=q;
- q=m;
- head=q;
- }
- else
- {
- s->next=r;
- r->next=q;
- q->next=t;
- m=r;
- r=q;
- q=m;
- }
- }
- s=q;
- q=r;
- r=r->next;
- }
- }
- }
- return head;
- }
- void OutPut2(struct student *head)
- {
- struct student *p=head;
- while(p!=NULL)
- {
- printf("%d %s %d\n",p->num,p->name,p->age);
- p=p->next;
- }
- printf("********************\n");
- }
- int main()
- {
- int age;
- char name[19];
- head=Create();
- OutPut2(head);
- head=PaiXu(head);
- OutPut2(head);
- return 0;
- }
复制代码
你这个直接把链表之间的连接给打乱了,我写了一个应该是对的,你到时候把文件路径给改一下。有很多变量没有使用,代码有一些小错误。最好不要用拼音,我看的不是很习惯。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct student
- {
- int num;
- char name[19];
- int age;
- struct student *next;
- };
- void exchage_str(char (*a)[19],char (*b)[19])
- {
- char temp[19]="zhutianci";
- strcpy(temp,*a);
- strcpy(*a,*b);
- strcpy(*b,temp);
- }
- struct student *Create(void)
- {
- int num;
- struct student *head=NULL,*p,*last = NULL;
- FILE *fp;
- fp=fopen("a.txt","r");
- fscanf(fp,"%d",&num);
- while(num!=0)
- {
- p=(struct student *)malloc(sizeof(struct student));
- p->next=NULL;
- if(head==NULL)
- {
- head=p;
- last=p;
- }
- else
- {
- last->next=p;
- last=p;
- }
- p->num=num;
- fscanf(fp,"%s%d%d",p->name,&p->age,&num);
- }
- fclose(fp);
- return head;
- }
- struct student *sort(struct student *head)
- {
- struct student *i,*j;
- struct student temp;
- for(i=head;i!=NULL;i=i->next)
- for(j=head;j!=NULL;j=j->next)
- {
- if(i->num>j->num)
- {
- temp.age=i->age;
- i->age=j->age;
- j->age=temp.age;
- exchage_str(&(i->name),&(j->name));
- temp.num=i->num;
- i->num=j->num;
- j->num=temp.num;
- }
- }
- return head;
- }
- void OutPut2(struct student *head)
- {
- struct student *p=head;
- while(p!=NULL)
- {
- printf("%d %s %d\n",p->num,p->name,p->age);
- p=p->next;
- }
- printf("********************\n");
- }
- int main()
- {
- struct student *head;
- head=Create();
- OutPut2(head);
- head=sort(head);
- OutPut2(head);
- return 0;
- }
复制代码
|
|