| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 765 人关注过本帖
标题:链表删除节点问题
只看楼主 加入收藏
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
结帖率:95%
收藏
 问题点数:0 回复次数:5 
链表删除节点问题
#include "stdio.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)

struct student
{char num[6];
 char name[8];
 struct student *next;
};

struct student *creat(void)
{struct student *p1,*p2,*head;
 int n=0;
 p1=p2=(struct student *)malloc(LEN);
 head=NULL;
 printf("Input 0 to end.\n");
 printf("Input num,name:\n");
 scanf("%s,%s",&p1->num,&p1->name);
 while(p1!=NULL)
 {if(n==0)head=p1;
  else p2->next=p1;
  n++;
  p2=p1;
 }
 p2->next=NULL;
 return(head);
}

void print(struct student *p)
{while(p!=NULL)
 {printf("%4s,%4s\n",p->num,p->name);
  p=p->next;
 }
}

main()
{struct student *ha,*hb,*p1,*p2,*p;
 ha=creat();
 hb=creat();
 print(ha);
 print(hb);
 p1=ha;
 p2=hb;
/*delete*/
 while(p1!=NULL)
 {if(strcmp(p2->num,p1->num)!=0)
  p2=p2->next;
  else
  {if(p1==ha)
      p1->next=ha;
   else
      p->next=p1->next;
   p=p1;
   p1=p1->next;                    这里是不是有问题?

  }
 }
 print(ha);
    getch();
}
搜索更多相关主题的帖子: 删除 节点 链表 
2010-02-04 04:18
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
收藏
得分:0 
#include "stdio.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)

struct student
{char num[6];
char name[8];
struct student *next;
};

struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
printf("Input 0 to end.\n");
printf("Input num,name:\n");
scanf("%s,%s",&p1->num,&p1->name);
while(p1!=NULL)
{if(n==0)head=p1;
  else p2->next=p1;
  n++;
  p2=p1;
}
p2->next=NULL;
return(head);
}

void print(struct student *p)
{while(p!=NULL)
{printf("%4s,%4s\n",p->num,p->name);
  p=p->next;
}
}

main()
{struct student *ha,*hb,*p1,*p2,*p;
ha=creat();
hb=creat();
print(ha);
print(hb);
p1=ha;
p2=hb;
/*delete*/
while(p1!=NULL)
{if(strcmp(p2->num,p1->num)!=0)
  p2=p2->next;
  else
  {if(p1==ha)
      p1->next=ha;
   else
      p->next=p1->next;
   }
   p=p1;
   p1=p1->next;                    这里是不是有问题?要删除链表A中与B中相同的元素
  }
print(ha);
    getch();
}
2010-02-04 04:27
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
收藏
得分:0 
#include "stdio.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)

struct student
{char num[6];
char name[8];
struct student *next;
};

struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
printf("Input 0 to end.\n");
printf("Input num,name:\n");
scanf("%s,%s",&p1->num,&p1->name);
while(p1!=NULL)
{if(n==0)head=p1;
  else p2->next=p1;
  n++;
  p2=p1;
}
p2->next=NULL;
return(head);
}

void print(struct student *p)
{while(p!=NULL)
{printf("%4s,%4s\n",p->num,p->name);
  p=p->next;
}
}

main()
{struct student *ha,*hb,*p1,*p2,*p;
ha=creat();
hb=creat();
print(ha);
print(hb);
p1=ha;
/*delete*/
while(p1!=NULL)
{p2=hb;
 if(strcmp(p2->num,p1->num)!=0)
  p2=p2->next;
  else
  {if(p1==ha)
      p1->next=ha;
   else
      p->next=p1->next;        问题是如果第一次就执行这句程序时,指针p并没有指向任一节点,哪来的p->next?
   }
   p=p1;                        这句程序未执行前。哪来的p->next。
   p1=p1->next;               
  }
print(ha);
    getch();
}
2010-02-04 04:36
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
收藏
得分:0 
#include "stdio.h"
#include "conio.h"
#define NULL 0
#define LEN sizeof(struct student)

struct student
{char num[6];
char name[8];
struct student *next;
};

struct student *creat(void)
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
printf("Input 0 to end.\n");
printf("Input num,name:\n");
scanf("%s,%s",&p1->num,&p1->name);
while(p1!=NULL)
{if(n==0)head=p1;
  else p2->next=p1;
  n++;
  p2=p1;
}
p2->next=NULL;
return(head);
}

void print(struct student *p)
{while(p!=NULL)
{printf("%4s,%4s\n",p->num,p->name);
  p=p->next;
}
}

main()
{struct student *ha,*hb,*p1,*p2,*p;
ha=creat();
hb=creat();
print(ha);
print(hb);
p1=ha;
/*delete*/
while(p1!=NULL)
{p2=hb;
if(p2!=NULL&&strcmp(p2->num,p1->num)!=0)                   这样改是不是就可以了。
  p2=p2->next;
  else
  {if(p1==ha)
      p1->next=ha;
   else
      p->next=p1->next;        
   }
   p=p1;                        
   p1=p1->next;               
  }
print(ha);
    getch();
}
2010-02-04 06:22
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
收藏
得分:0 
如果第一次删的是表头,第二次还是删表头,好像程序就有问题了啊。。?
2010-02-04 06:25
陈威
Rank: 1
等 级:新手上路
帖 子:114
专家分:0
注 册:2009-10-18
收藏
得分:0 
我知道了,实际上也是可以的。。。。。。
2010-02-04 06:34
快速回复:链表删除节点问题
数据加载中...
 
   



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

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