标题:建立两个连表,合并,并按学好从小到大输出,有错,但找不出来,请各位看看
只看楼主
llp108
Rank: 1
等 级:新手上路
帖 子:70
专家分:0
注 册:2007-11-15
结帖率:25%
 问题点数:0 回复次数:1 
建立两个连表,合并,并按学好从小到大输出,有错,但找不出来,请各位看看
错误出现在排序中用红色表示的语句上,当我把该语句删除程序可执行,但是只能排前两个接点,达不到从小到大的效果,如果加上 程序执行有错,麻烦各位看一下 谢谢!!
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)
#define NUL 0
struct student
{
    char num[5];
    char name[20];
    float score;
    struct student*next;
};
main()
{
    struct student *creat(struct student*,int);
    struct student *insert(struct student*,struct student*);
    void paixu(struct student*,int);  //对连表进行排序
    void  print(struct student*);
    struct student *pa,*pb,*pc,a,b;
    int na,nb,nc;
    pa=&a;
    pb=&b;
    printf("请分别输入要建两个链表的结点个数:\n");
    printf("第一个链表结点数:");
    scanf("%d",&na);
    pa=creat(pa,na);
    printf("第二个链表结点数:");
    scanf("%d",&nb);
    pb=creat(pb,nb);
    pc=insert(pa,pb);
    nc=na+nb;
    paixu(pc,nc);
    print(pc);
}
struct student *creat(struct student*p,int n)
{
    struct student *p1,*p2;
    int N=1;
    if(n==0)
    p=NUL;
    else
    {p1=p;
    do
     {printf("第%d个学生:\n",N);
      printf("学号:");
      scanf("%s",&p1->num);
      printf("姓名:");
      scanf("%s",&p1->name);
      printf("成绩:");
      scanf("%f",&p1->score);
      if(n==1)
         p->next=NUL;
      else
        {p2=p1;
         p1=(struct student*)malloc(LEN);
         p2->next=p1;
        }
      N++;} while(N<=n);
     if(n!=1)
      p2->next=NUL;}
     return(p);
}         
void paixu(struct student*p,int n)
{
    struct student temp,*head;
    int i,j;
    head=p;
    for(i=0;i<n-1;i++)
       for(j-0;j<n-i;j++)
        {if(strcmp(p->num,(p->next)->num)>0)
           {strcpy(temp.num,p->num);
            strcpy(p->num,(p->next)->num);
            strcpy((p->next)->num,temp.num);
            strcpy(temp.name,p->name);
            strcpy(p->name,(p->next)->name);
            strcpy((p->next)->name,temp.name);
            temp.score=p->score;
            p->score=(p->next)->score;
            (p->next)->score=temp.score;
           }
          p=p->next;
         }
    p=head; //在下一伦开始时使p回到head//   
}
void print(struct student*p)
{
    printf("排序好的连表是:\n");
    printf("学号        姓名        成绩\n");
    do
          {printf("%5s%10s%10.2f\n",p->num,p->name,p->score);
           p=p->next;}while(p!=NUL);
}
struct student*insert(struct student*p1,struct student*p2)
{
    struct student *head;
    head=p1;
    while(p1->next!=NUL)
       p1=p1->next;
    p1->next=p2;
    return(head);
}
搜索更多相关主题的帖子: 从小到大 输出 
2008-01-08 21:01
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
得分:0 
呜呼哀哉

坚决不跟用TC的人打交道!
2008-01-08 21:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-196562-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.366750 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved