永发信息网

C语言。有两个链表a和b,从a链表中删去与b链表中有相同学号的那些结点!

答案:1  悬赏:10  手机版
解决时间 2021-03-20 13:50
  • 提问者网友:愿为果
  • 2021-03-19 13:31
C语言。有两个链表a和b,从a链表中删去与b链表中有相同学号的那些结点!
最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-03-19 14:43

#include
#include
#include

struct student
{
int num;
char name[10];
struct student *next;
};

int main()
{
void print(struct student *head);
struct student *creat();
struct student *solve(struct student *head_a,struct student *head_b);
struct student *head,*head_a,*head_b;
printf("********初始化链表A********\n");
head_a=creat();
printf("********输出链表A********\n");
print(head_a);

printf("********初始化链表B********\n");
head_b=creat();
printf("********输出链表B********\n");
print(head_b);

printf("********从a链表中删去与b链表中有相同学号的那些结点********\n");
head=solve(head_a,head_b);

print(head);

return 0;
}

struct student *creat()
{
struct student *head,*p,*p1;


p = p1=(struct student *)malloc(sizeof(struct student));
head = NULL;

printf("请输入学生学号和姓名(输入学号0和姓名0停止接收数据):");
scanf("%d%s",&p->num,&p->name);
while(!(p->num == 0 && strcmp(p->name,"0")==0))
{
if(head == NULL)
{
head = p;
p1=head;
p=(struct student *)malloc(sizeof(struct student));
}
else
{
p1->next=p;
p1=p;
p=(struct student *)malloc(sizeof(struct student));
}

printf("请输入学生学号和姓名(输入学号0和姓名0停止接收数据):");
scanf("%d%s",&p->num,&p->name);
}
p1->next=NULL;
return head;
}

struct student *solve(struct student *head_a,struct student *head_b)
{
struct student *p,*p1,*t;
p=p1=head_a;

if(head_b == NULL)
{
return head_a;
}

while(p!=NULL)
{
t=head_b;


while(t->next!=NULL && (t->num!=p->num))
{
t=t->next;
}

if(t->num==p->num)
{
if(p==head_a)
{
head_a=head_a->next;
p1=p=head_a;
}
else
{
p1->next=p->next;
p=p->next;
}
}
else
{
p1=p;
p=p->next;
}
}
return head_a;
}

void print(struct student *head)
{
struct student *p;

p = head;
while(p!=NULL)
{
printf("%d %s\n",p->num,p->name);
p=p->next;
}
}


这样可以么?追问呃呃,我的代码里面没有struct student *solve(struct student *head_a,struct student *head_b)函数,,,,看完你的答案还是有点没懂·······
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯