首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

链表菜鸟,编了个东西好多有关问题,望得到解决

2012-02-21 
链表初学者,编了个东西好多问题,望得到解决#includestdio.h#includemalloc.h#defineLENsizeof(structs

链表初学者,编了个东西好多问题,望得到解决
#include   <stdio.h>
#include   <malloc.h>
#define   LEN   sizeof(struct   student)
#define   NULL   0
typedef   struct   student
{
    int   num;
    char   name[];
    double   score;
    struct   student   *next;
}student;
int   menu()
{
    int   i;
    printf( "\n\n1.Insert\t2.Delete\t3.View\t4.select\t5.paixu\t6.quit\nPlease   select   a   number: ");
    scanf( "%d ",&i);
    return   i;
}

struct   student   *creat(viod)
{struct   student   *head,*p1,*p2;
int   n=0;
  p1=p2=(struct   student   *)malloc(LEN);
  scanf( "%d,%c,%f ",&p1-> num,&p1-> name,&p1-> score);
  head=NULL;
  while(p1-> num!=0)
  {n=n+1;
    if(n==1)head=p1;
  else   p2-> next=p1;
          p2=p1;
          p1=(struct   student   *)malloc(LEN);
scanf( "%d,%c,%f ",&p1-> num,&p1-> name,&p1-> score);
p2-> next=NULL;
return(head);
}
  student       *bubblesort(student     *head)
    {
    student       *q,*tail,*p=(student*)malloc(LEN);
    p-> next       =       head;
    head       =       p;
    tail       =       NULL;
    while(tail       !=       head-> next)
    {
    p       =       head;
    q       =p-> next;
    while(q-> next       !=       tail)
    {
    if       (p-> next-> num       <       q-> next-> num)
    {
    p-> next       =       q-> next;
    q-> next       =       q-> next-> next;
    p-> next-> next       =       q;
    }
    p       =       p-> next;
    q       =       p-> next;
    }
    tail       =       q;
    }
    p       =       head-> next;
    free(head);
    return       p;
    }
        int   insert(student   *head,int   x,char   y,float   z)
      student   *p0,*p1,*p2;
      s=   (student   *)malloc(LEN);
      s-> num=x;
      s-> name=y;
      s-> score=z;
      p=head;
      while(p-> next)
      {
            if(x   <   p-> next-> num)     break;
            else     p=p-> next;


      }

      s-> next=p-> next;
      p-> next=s;
      return(1);
}
int   delete(student   *head,int   x)
{
      int   j=0;
      student   *p;
      p=head;

      while(p-> next)
      {
          if(p-> next-> num==x)
          {
                p-> next=p-> next-> next;
                j++;
          }
          else
              p=p-> next;
      }
      return   j;

}
void   view(student   *head)
{
    student   *p;
    p=head;
    while(p-> next)
    {
          printf( "%d   %c   %f   ",p-> next-> num,p-> next-> name,p-> next-> score);
          p=p-> next;
    }
}
int   select(student   *head,char   x)
{

      student   *p;
      p=head;

      while(p-> next)
      {
          if(p-> next-> name==x)
          {
                  printf( "%d   %c   %f   ",p-> next-> num,p-> next-> name,p-> next-> score);

          }
          else
              p=p-> next;
      };
student       *bubblesort1(student     *head)
    {
    student       *q,*tail,*p=(student*)malloc(LEN);
    p-> next       =       head;
    head       =       p;
    tail       =       NULL;
    while(tail       !=       head-> next)
    {
    p       =       head;
    q       =p-> next;
    while(q-> next       !=       tail)
    {
    if       (p-> next-> score       <       q-> next-> score)
    {
    p-> next       =       q-> next;
    q-> next       =       q-> next-> next;
    p-> next-> next       =       q;
    }
    p       =       p-> next;
    q       =       p-> next;
    }
    tail       =       q;
    }
    p       =       head-> next;
    free(head);      


    return       p;      
    }      
main()
{
    int   c,num,d;
    char   name;
    float   score;
    student   *head;
    head=creat();
    head=bubblesort()
   
    clrscr();
    c=menu();
    while(c!=6)
    {
        if(c==1)
        {
            printf( "\nPlease   input   the   data   you   want   to   insert: ");
            scanf( "%d%c%f ",&num,&name,&score);
            if(insert(head,num,name,score))
                printf( "\nInsert   OK!! ");
            else
printf( "\nInsert   Error!! ");
        }
        else   if(c==2)
        {
            printf( "\nPlease   input   the   number   you   want   to   delete: ");
                scanf( "%d%c%f ",&num,&name,&score);
            d=delete(head,num);
       
        }
        else   if(c==3)
            view(head);
        esle   if(c==4)
        {
            printf( "\nPlease   input   the   name   you   want   to   select: ")   ;    
      scanf( "%c ",&name);
      select(head,name);
          else   if(c=5)
        head=bubblesort1();
   
 
        }
        c=menu();
    }
}


[解决办法]
楼主把这程序扔掉重新写一个吧!!!你的编程习惯不好,好的编程习惯是“自顶而下,逐步求精”的!!!!就是你每写一个链的函数,就要进行调试,检查写出的函数是否有问题(可以写个简单的main函数打印出相关变量),比如你写一个初始化链,就要检查一下新建个链后相关变量的输出有没有错误。初学的时候别写这么长的代码。。。写个简单的容易掌握住链的特点也不容易出错,我们数据结构老师写的代码都没你这个长。。。。。。

热点排行