有两个链表a和b,设结点中包含学号和姓名,从a链表中删除和b链表中相同学号的结点?为什么我这个程序没效果,而且输入0的时候不会停止掉真怪?
#include<stdio.h>
#define LEN sizeof(struct student)
#define NULL 0
int n;
struct student
{char num[10];
char name[10];
struct student *next;
}*ahead,*bhead;
main()
{struct student *creat(void);
void del(struct student *ahead,struct student *bhead);
void print(struct student *ahead);
ahead=creat();
bhead=creat();
del(ahead,bhead);
print(ahead);
}
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
head=NULL;
while(strcmp(p1->num,"0")!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s %s",p1->num,p1->name);
}
p2->next=NULL;
return(head);
}
void del(struct student *ahead,struct student *bhead)
{struct student *p,*ap1,*bp1;
ap1=ahead;
while(ahead->next!=NULL)
{ bp1=bhead;
while(bp1!=NULL&&strcmp((bp1->num,ap1->num)==1))
bp1=bp1->next;
if(strcmp(bp1->num,ap1->num)==0)
if(ap1==ahead)
ahead=ap1->next;
else p->next=ap1->next;
p=ap1;
ap1=ap1->next;
}
}
void print(struct student *ahead)
{struct student *p;
p=ahead;
while(p!=NULL)
{printf("%s%s",p->num,p->name);
p=p->next;
}
}
[解决办法]
给你一个 我是手写的 没优化 全写在一个函数里了 可能有垃圾代码 你自己看看吧
#include <stdio.h>#include <malloc.h>#define LEN sizeof(struct a)#define N 5//定意义常量Nstruct a//定义第一个链表节点内容{ int num; struct a *point;};struct b//定义第二个链表节点内容{ int num; struct b *point;};void main(){ struct a *heada,*p1,*p2,*pend; struct b *headb,*p3,*p4; int i,j,t,k,s=N; for(i=1;i<=N;i++)//开始开辟第一个链表空间,链表长度为N { if(i==1) heada=p1=p2=(struct a *)malloc(LEN); else p2=p1; p1->num=i;//为第一个链表num成员赋值,用于测试数据 p1=(struct a *)malloc(LEN); p2->point=p1; } p2->point=0; printf("测试第一个链表开辟是否成功\n"); p1=heada; while(p1->point!=0) { printf("%d ",p1->num); p1=p1->point; } printf("%d\n",p1->num); for(i=1;i<=N;i++)//开始开辟第二个链表空间,链表长度为N { if(i==1) headb=p3=p4=(struct b *)malloc(LEN); else p4=p3; p3->num=i*2;//为第二个链表num成员赋值,用于测试数据,为避免测试数据与链表1完全相同,使i+2 p3=(struct b *)malloc(LEN); p4->point=p3; } p4->point=0; printf("测试第二个链表开辟是否成功\n"); p3=headb; while(p3->point!=0) { printf("%d ",p3->num); p3=p3->point; } printf("%d\n",p3->num); for(i=1;i<=N;i++)//开始检查两个链表中相同数据的节点,并从第一个链表中将其删除 { if(i==1)//先将指向两个链表的指针指向表头 p3=headb; p1=heada;//每循环一次使p1重新指向链表头,使得第二个链表的当前节点总是从第一个链表的第一个节点开始比较 for(j=1;j<=N;j++) { if(j==2) p2=heada; if(p1->num==p3->num&&j==1) { heada=p1->point; s--; } if(p1->num==p3->num&&j>1) { p2->point=p1->point; p1=p1->point; s--; } p1=p1->point; p2=p2->point; } p3=p3->point; } printf("测试第一个链表删除是否成功\n"); p1=heada; while(p1->point!=0) { printf("%d ",p1->num); p1=p1->point; } printf("%d\n",p1->num); pend=p1; for(i=1;i<=N;i++)//开始合并链表 { if(i==1) { pend->point=(struct a *)malloc(LEN);//将p1原本指向0的指针指向新开辟的空间 p2=pend=pend->point; p3=headb;//将p3重新指向第2个链表的表头 } else p2=pend; p2->num=p3->num; pend=(struct a *)malloc(LEN); p2->point=pend; p3=p3->point; } p2->point=0; printf("检查合并是否成功!\n"); p1=heada; while(p1->point!=0) { printf("%d ",p1->num); p1=p1->point; } printf("%d\n",p1->num); p1=p2=heada; for(i=1;i<=N;i++)//开始对链表内的数值排序 { p2=p1->point; for(j=i+1;j<=N;j++) { if(p1->num>p2->num) { t=p1->num; p1->num=p2->num; p2->num=t; } p2=p2->point; } p1=p1->point; } printf("检查排序是否成功!\n"); p1=heada; while(p1->point!=0) { printf("%d ",p1->num); p1=p1->point; } printf("%d\n",p1->num); p1=heada; printf("输入一个已有的num值\n"); scanf("%d",&k); for(i=1;i<=N+s;i++)//开始删除链表中num值与k相等的节点 { if(i==1&&k==1) heada=p1->point; if(i==2) p2=heada; if(p1->num==k&&i!=1) { p2->point=p1->point; p1=p1->point; } p1=p1->point; p2=p2->point; } printf("检查删除是否成功!\n"); p1=heada; while(p1->point!=0) { printf("%d ",p1->num); p1=p1->point; } printf("%d\n",p1->num); printf("\n---------------------------------\n"); getchar(); getchar();}
[解决办法]
楼主,你del函数中,ahead指针位置发生变化,你应该返回这个指针,你main函数中还是原来那个ahead指针:
#include<stdio.h>#include<stdlib.h>#include<string.h>#define LEN sizeof(struct student)#define NULL 0int n;struct student{ char num[10]; char name[10]; struct student *next;}*ahead,*bhead;main(){ struct student *creat(void); struct student * del(struct student *ahead,struct student *bhead); void print(struct student *ahead); ahead=creat();// print(ahead); bhead=creat();// print(bhead); ahead = del(ahead,bhead); print(ahead);}struct student *creat(void){ struct student *p1,*p2,*head; n=0; p1=p2=(struct student *)malloc(LEN); p1->next = NULL; scanf("%s %s",p1->num,p1->name); head=NULL; while(strcmp(p1->num,"0")!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student *)malloc(LEN); p1->next = NULL; scanf("%s %s",p1->num,p1->name); } p2->next=NULL; return(head);}struct student *del(struct student *ahead,struct student *bhead){ struct student *p,*ap1,*bp1; ap1=ahead; while(ap1->next!=NULL) { bp1=bhead; while(bp1!=NULL&&strcmp(bp1->num,ap1->num)==1) bp1=bp1->next; if(strcmp(bp1->num,ap1->num)==0) if(ap1==ahead) ahead=ap1->next; else p->next=ap1->next; p=ap1; ap1=ap1->next; } return ahead;}void print(struct student *ahead){ struct student *p; p=ahead; while(p!=NULL) { printf("%s %s ",p->num,p->name); p=p->next; } printf("\n");}