链表删除
要求:有两个链表A,B,设结点包含学号,姓名;从A链表中删除与B链表中有相同学号的那些结点。#include <stdio.h>
#define null 0
struct stu
{
int num;
char name[20];
struct stu *next;
};
int n=0;
struct stu *creat(void) \\\\\建立链表
{
struct stu *p1,*p2,*head;
int i;
p1=p2=(struct stu*)malloc(sizeof(struct stu));
printf("\ntha data:\n");
printf("input num and name:\n");
scanf("%d,%s",&p2->num,p2->name);
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct stu *) malloc(sizeof(struct stu));
scanf("%d,%s",&p1->num,p1->name);
}
p2->next=null;
return(head);
}
struct stu *del(struct stu *head1,struct stu* head) \\\\\\删除结点,我试了,这个函数有问题,但是找了很久没发现问题所在。
{
struct stu *p1,*p2,*p3;
int count=0;
p1=p3=head1;
p2=head;
while(count!=1)
{
if(p1->num==p2->num)
{
if(p1==head1)
head1=p1->next;
else if(p1->next==null)
p1->next=null;
else
p3->next=p1->next;
}
p3=p1;
p1=p1->next;
if(p1->next==null)
{ p1=head1;
p2=p2->next;
}
if(p2->next=null)
count=1;
}
p3->next=null;
return(head1);
}
void print(struct stu *head1) /////打印链表
{
struct stu *p1;
p1=head1;
printf("\nthe new data:\n");
do
{
printf("num:%d name:%s\n",p1->num,p1->name);
p1=p1->next;
}
while(p1!=null);
}
void main()
{
struct stu *head1,*head;
head1=creat();
head=creat();
head1=del(head1,head);
print(head1);
}