读书人

小弟我这个链表有点有关问题找了半天

发布时间: 2012-07-20 10:38:30 作者: rapoo

我这个链表有点问题,找了半天没找着

C/C++ code
#include"stdio.h"#include"malloc.h"#include"string.h" #define NULL 0struct stu{      char *name;      char sex;      int age;      int num;          };struct stu *creat(int n){    struct stu *head,*pf,*pd;    for(int i=0;i<n;i++)    {        pd=(struct stu*)malloc(sizeof(struct stu));        printf("input Number and Age\n");        scanf("%d%d",&pb->num,&pb->age);       if(i==0)        head=pf=pd;        else {             pf->next=pb;             pb->next=NULL;                 pf=pb;             }    }    return head;}struct stu *search(struct stu *head,int num)    {    struct stu *pf,*pb;    pb=head;    while(pb->num!=num&&pb->next!=NULL)       {        pb=pb->next;         }       if(pb->num=num) ruturn pb;       if(pb->num!=num&&pb->next==NULL)       printf("没有该同学");                  }struct stu *delete(struct stu *head,int num){    struct stu *pf,*pb;    if(head==NULL)    {      printf("这表是个空链表");      goto end;    }    pb=head;    while(pb->num!=num&&pb->next!=NULL)    {        pf=pb;pb=pb->next;    }    if(pb->num=num)      {        if(pb==head)        head=pb->next;        else pf->next=pb->next;       printf("节点已经删除");       free(pb);      }      else printf("没有这个节点");       end;     }  struct stu *insert(struct stu *head,*pi) {     struct stu *pf,*pb;     if(head==NULL)     {         head=pi;         pi-next=NULL;     }     else     {         pb=head;        while(pi->num>pb->num&&pb->next!=NULL)        {            pf=pb;            pb=pf-next;        }         if(pi->num<=pb->num)         {           if(head==pb)             {             head=pi;            }         else            {               pf->next=pi;            pi-next=pb;            }         else {             pb->next=pi;             pi->next=NULL;             }        }    }    return head; } int main() {     struct *head,*pi;     int num;     printf("请输入创建几个同学成绩单");     scanf("%d",&num);     head=creat(num);     print(head);     printf("请输入查找同学的学号");     scanf("%d",&num);     print(search(head,num));     printf("请输入要被删除同学的学号");     scanf("%d",&num);     print(delete(head,num));    printf("请输入要插入同学的学生号和年龄: ");     pi=(struct stu *)malloc(sizeof(struct stu));    scanf("%d%d",&pi->num,&pi->age);    print(insert(head,pi)); }


[解决办法]
struct stu *head,*pf,*pd;

定义的是pd,用的却是pb
[解决办法]
错太多了,显然你是复制别人的东东往上凑的~~~
[解决办法]
没有指向next结构的指针

读书人网 >C语言

热点推荐