| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:出现一个奇怪的C问题: 高手看看,指点一下?
只看楼主 加入收藏
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
 问题点数:0 回复次数:3 
出现一个奇怪的C问题: 高手看看,指点一下?

下面是建立,插入,删除,显示动态链表的C程序,一般情况运行功能没问题,但当删除

"后插入的结点"时

程序的打印函数print却显示的数据总是比原先的少1

比如,数据是799,当要删除它时,程序的打印函数print却显示"798 deleted"(总是少1)

不知何故? 请高手看看,指点一下?

#include <stdio.h> #include <alloc.h> #define LEN sizeof(struct stu) struct stu { int num; char name[20]; int score; struct stu *next; }; int n; struct stu *new(void) { int flag=0; struct stu *head; struct stu *p1,*p2; n=0; head=NULL; p1=p2=NULL; printf("\ninput data:\n"); do { p1=(struct stu *)malloc(LEN); scanf("%d%s%d",&p1->num,p1->name,&p1->score); if(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; } else flag=1; }while(flag!=1); p2->next=NULL; return(head); }

void print(struct stu *head) { struct stu *p; p=head; if(p==NULL) return; printf("\nthe records are:\n"); printf("%d %o\n",p,p); do { printf("%d %o %d %s %d %d %o\n",p,p,p->num,p->name,p->score,p->next,p->next); p=p->next; }while(p!=NULL); }

struct stu *del(struct stu *head,int num) { struct stu *p1,*p2; if(head==NULL) { printf("\nlist is NULL\n"); return(head); } p1=head; while(num!=p1->num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->num) { if(p1==head) head=p1->next; else p2->next=p1->next; free(p1); printf("%d deleted\n",num); n=n-1; } else printf("%d not found!\n",num); return(head); }

struct stu *insert(struct stu *head,struct stu *s) { struct stu *p0,*p1,*p2; p1=head; p0=s; if(head==NULL) { head=p0; p0->next=NULL; } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(p1==head) { head=p0; p0->next=p1; } else { p2->next=p0; p0->next=p1; } } else { p1->next=p0; p0->next=NULL; } } n=n+1; return(head); }

main() { struct stu *p; struct stu a; int delnum; p=new(); print(p); printf("input the data to insert:\n"); scanf("%d%s%d",&a.num,a.name,&a.score); p=insert(p,&a); print(p); printf("input the number to delete:\n"); scanf("%d",&delnum); p=del(p,delnum); print(p); }

搜索更多相关主题的帖子: include 动态 
2005-01-05 13:22
忧郁的风
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2004-11-1
收藏
得分:0 
下面是小弟编得,你可以看看,不过我不是用的子函数
#define MAX 7
struct number
       {int data;
    int cur;
       };
void init(struct number *list)
{int i=0;
 for(;i&lt;MAX-1;i++) list[i].cur=i+1;  /*inite list*/
 list[MAX-1].cur=0;   /*end inite*/
}/*end init*/
int lmalloc(struct number *list)   /*get node from the list*/
{int i;
 i=list[0].cur;
 if(list[0].cur) list[0].cur=list[i].cur;
 return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
 while(n)
 {n=list[n].cur;
  len++;
 }
 return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
 printf("the list is :\n");
 printf("    label   data    cur\n");
 for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 for(i=0;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d            %-4d\n",i,list[i].cur);
 if(list[0].cur!=0)
   printf("    %-4d            0\n",i);
}/*end print*/
main()
{struct number space[MAX];
 int r,s,t,n,m,i,j,b,k,p;
 init(space);
 s=lmalloc(space);
 r=s;
 loop:
   {printf("input the amount of list A(between 1 and %d):",MAX-2);
    scanf("%d",&amp;n);
   }
 if(n&lt;1||n&gt;MAX-2)
   {printf("input error!\n");
    goto loop;
   }
 printf("input the elems of list A:");
 for(j=1;j&lt;=n;j++)
   {i=lmalloc(space);
    scanf("%d",&amp;space[i].data);
    space[r].cur=i;
    r=i;
   }
 space[r].cur=0;
 printf("the length of list A is %d\n",length(space));
 print(space);
 label:
   {printf("input the amount of list B:");
    scanf("%d",&amp;m);
   }
 if(m&lt;=0)
   {label0:
    printf("do you want to continue(1) or exit(0)?");
    scanf("%d",&amp;t);
    if(t==1) goto label;
    else if(t==0) exit(0);
     else {printf("input error!\n");
           goto label0;
          }
   }/*end if*/
 for(j=1;j&lt;=m;j++)
   {printf("input one elem of list B:");
    scanf("%d",&amp;b);
    p=s;
    k=space[s].cur;
    while(k!=space[r].cur&amp;&amp;space[k].data!=b)
      {p=k;
       k=space[k].cur;
      }
    if(k==space[r].cur)    /*insert*/
      {if(length(space)==MAX-2)    /*judge whether the list is full or
not*/
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("the length of the list is %d\n",length(space));
      print(space);
      label1: printf("do you want to delete(1) or exit(0)?");
      scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
           else {printf("input error!\n");
             goto label1;
            }
     }/*end judge*/
       else    /*insert b at the end of list A*/
     {i=lmalloc(space);
      space[i].data=b;
      space[i].cur=space[r].cur;
      space[r].cur=i;
      r=i;
         }
      }/*end insert*/
    else    /*delete*/
      {space[p].cur=space[k].cur;
       space[k].cur=space[0].cur;
       space[0].cur=k;
       if(r==k) r=p;
      }
    if(length(space)==0)
      {printf("the list is empty!\nyou can not delete any elem before
you insert!\n");   /*judge whether the list is empty or not*/
       printf("the length of the list is 0\n");
       label2: printf("do you want to insert(1) or exit(0)?");
       scanf("%d",&amp;t);
       if(t==1) goto label;
       else if(t==0) exit(0);
        else {printf("input error!\n");
          goto label2;
         }
      }/*end if*/
   printf("the length of the list is %d\n",length(space));
   print(space);
   if(length(space)==MAX-2)
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("do you want to delete(1) or exit(0)?");
      label3: scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
       else {printf("input error!\ninput again:");
         goto label3;
        }
     }/*end if*/
   }/*end for*/
   printf("do you want to continue(1) or exit(0)?");
   lab: scanf("%d",&amp;t);
   if(t==1) goto label;
   else if(t==0) exit(0);
    else {printf("input error!\ninput again:");
          goto lab;
         }
}#define MAX 7
struct number
       {int data;
    int cur;
       };
void init(struct number *list)
{int i=0;
 for(;i&lt;MAX-1;i++) list[i].cur=i+1;  /*inite list*/
 list[MAX-1].cur=0;   /*end inite*/
}/*end init*/
int lmalloc(struct number *list)   /*get node from the list*/
{int i;
 i=list[0].cur;
 if(list[0].cur) list[0].cur=list[i].cur;
 return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
 while(n)
 {n=list[n].cur;
  len++;
 }
 return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
 printf("the list is :\n");
 printf("    label   data    cur\n");
 for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 for(i=0;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d            %-4d\n",i,list[i].cur);
 if(list[0].cur!=0)
   printf("    %-4d            0\n",i);
}/*end print*/
main()
{struct number space[MAX];
 int r,s,t,n,m,i,j,b,k,p;
 init(space);
 s=lmalloc(space);
 r=s;
 loop:
   {printf("input the amount of list A(between 1 and %d):",MAX-2);
    scanf("%d",&amp;n);
   }
 if(n&lt;1||n&gt;MAX-2)
   {printf("input error!\n");
    goto loop;
   }
 printf("input the elems of list A:");
 for(j=1;j&lt;=n;j++)
   {i=lmalloc(space);
    scanf("%d",&amp;space[i].data);
    space[r].cur=i;
    r=i;
   }
 space[r].cur=0;
 printf("the length of list A is %d\n",length(space));
 print(space);
 label:
   {printf("input the amount of list B:");
    scanf("%d",&amp;m);
   }
 if(m&lt;=0)
   {label0:
    printf("do you want to continue(1) or exit(0)?");
    scanf("%d",&amp;t);
    if(t==1) goto label;
    else if(t==0) exit(0);
     else {printf("input error!\n");
           goto label0;
          }
   }/*end if*/
 for(j=1;j&lt;=m;j++)
   {printf("input one elem of list B:");
    scanf("%d",&amp;b);
    p=s;
    k=space[s].cur;
    while(k!=space[r].cur&amp;&amp;space[k].data!=b)
      {p=k;
       k=space[k].cur;
      }
    if(k==space[r].cur)    /*insert*/
      {if(length(space)==MAX-2)    /*judge whether the list is full or
not*/
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("the length of the list is %d\n",length(space));
      print(space);
      label1: printf("do you want to delete(1) or exit(0)?");
      scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
           else {printf("input error!\n");
             goto label1;
            }
     }/*end judge*/
       else    /*insert b at the end of list A*/
     {i=lmalloc(space);
      space[i].data=b;
      space[i].cur=space[r].cur;
      space[r].cur=i;
      r=i;
         }
      }/*end insert*/
    else    /*delete*/
      {space[p].cur=space[k].cur;
       space[k].cur=space[0].cur;
       space[0].cur=k;
       if(r==k) r=p;
      }
    if(length(space)==0)
      {printf("the list is empty!\nyou can not delete any elem before
you insert!\n");   /*judge whether the list is empty or not*/
       printf("the length of the list is 0\n");
       label2: printf("do you want to insert(1) or exit(0)?");
       scanf("%d",&amp;t);
       if(t==1) goto label;
       else if(t==0) exit(0);
        else {printf("input error!\n");
          goto label2;
         }
      }/*end if*/
   printf("the length of the list is %d\n",length(space));
   print(space);
   if(length(space)==MAX-2)
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("do you want to delete(1) or exit(0)?");
      label3: scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
       else {printf("input error!\ninput again:");
         goto label3;
        }
     }/*end if*/
   }/*end for*/
   printf("do you want to continue(1) or exit(0)?");
   lab: scanf("%d",&amp;t);
   if(t==1) goto label;
   else if(t==0) exit(0);
    else {printf("input error!\ninput again:");
          goto lab;
         }
}#define MAX 7
struct number
       {int data;
    int cur;
       };
void init(struct number *list)
{int i=0;
 for(;i&lt;MAX-1;i++) list[i].cur=i+1;  /*inite list*/
 list[MAX-1].cur=0;   /*end inite*/
}/*end init*/
int lmalloc(struct number *list)   /*get node from the list*/
{int i;
 i=list[0].cur;
 if(list[0].cur) list[0].cur=list[i].cur;
 return(i);
}/*end lmalloc*/
int length(struct number *list)
{int len=0,n=list[1].cur;
 while(n)
 {n=list[n].cur;
  len++;
 }
 return(len);
}/*end length*/
void print(struct number *list)
{int i=1;
 printf("the list is :\n");
 printf("    label   data    cur\n");
 for(i=list[i].cur;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 printf("    %-4d    %-4d    %-4d\n",i,list[i].data,list[i].cur);
 for(i=0;list[i].cur!=0;i=list[i].cur)
   printf("    %-4d            %-4d\n",i,list[i].cur);
 if(list[0].cur!=0)
   printf("    %-4d            0\n",i);
}/*end print*/
main()
{struct number space[MAX];
 int r,s,t,n,m,i,j,b,k,p;
 init(space);
 s=lmalloc(space);
 r=s;
 loop:
   {printf("input the amount of list A(between 1 and %d):",MAX-2);
    scanf("%d",&amp;n);
   }
 if(n&lt;1||n&gt;MAX-2)
   {printf("input error!\n");
    goto loop;
   }
 printf("input the elems of list A:");
 for(j=1;j&lt;=n;j++)
   {i=lmalloc(space);
    scanf("%d",&amp;space[i].data);
    space[r].cur=i;
    r=i;
   }
 space[r].cur=0;
 printf("the length of list A is %d\n",length(space));
 print(space);
 label:
   {printf("input the amount of list B:");
    scanf("%d",&amp;m);
   }
 if(m&lt;=0)
   {label0:
    printf("do you want to continue(1) or exit(0)?");
    scanf("%d",&amp;t);
    if(t==1) goto label;
    else if(t==0) exit(0);
     else {printf("input error!\n");
           goto label0;
          }
   }/*end if*/
 for(j=1;j&lt;=m;j++)
   {printf("input one elem of list B:");
    scanf("%d",&amp;b);
    p=s;
    k=space[s].cur;
    while(k!=space[r].cur&amp;&amp;space[k].data!=b)
      {p=k;
       k=space[k].cur;
      }
    if(k==space[r].cur)    /*insert*/
      {if(length(space)==MAX-2)    /*judge whether the list is full or
not*/
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("the length of the list is %d\n",length(space));
      print(space);
      label1: printf("do you want to delete(1) or exit(0)?");
      scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
           else {printf("input error!\n");
             goto label1;
            }
     }/*end judge*/
       else    /*insert b at the end of list A*/
     {i=lmalloc(space);
      space[i].data=b;
      space[i].cur=space[r].cur;
      space[r].cur=i;
      r=i;
         }
      }/*end insert*/
    else    /*delete*/
      {space[p].cur=space[k].cur;
       space[k].cur=space[0].cur;
       space[0].cur=k;
       if(r==k) r=p;
      }
    if(length(space)==0)
      {printf("the list is empty!\nyou can not delete any elem before
you insert!\n");   /*judge whether the list is empty or not*/
       printf("the length of the list is 0\n");
       label2: printf("do you want to insert(1) or exit(0)?");
       scanf("%d",&amp;t);
       if(t==1) goto label;
       else if(t==0) exit(0);
        else {printf("input error!\n");
          goto label2;
         }
      }/*end if*/
   printf("the length of the list is %d\n",length(space));
   print(space);
   if(length(space)==MAX-2)
     {printf("the list is full!\nyou can not insert any elem before you
delete!\n");
      printf("do you want to delete(1) or exit(0)?");
      label3: scanf("%d",&amp;t);
      if(t==1) goto label;
      else if(t==0) exit(0);
       else {printf("input error!\ninput again:");
         goto label3;
        }
     }/*end if*/
   }/*end for*/
   printf("do you want to continue(1) or exit(0)?");
   lab: scanf("%d",&amp;t);
   if(t==1) goto label;
   else if(t==0) exit(0);
    else {printf("input error!\ninput again:");
          goto lab;
         }
}

轻轻的我走了,正如我轻轻的来,轻轻的我挥一挥衣袖,轻轻的抹去我留下的痕迹------风过无痕
2005-01-13 16:34
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
得分:0 
哇! 这么多! 多谢关注,
idealistic,我试试你的建议

有缘相识!我中文系的 3月拿下C/C++/VC 5天写游戏 2月写游戏系统 有空去http://mykool.go2.
2005-01-14 00:41
快速回复:出现一个奇怪的C问题: 高手看看,指点一下?
数据加载中...
 
   



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

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