#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//
struct student
{
char name[100];
char sno[10];
struct student *next;
}stu;
struct student *creat(int len)
{
struct student *head;//头结点
struct student *s;
struct student *p;
head = p = (struct student *)malloc(sizeof(struct student));
head = p;
for(int i=0;i<len;i++)
{
s = (struct student *)malloc(sizeof(struct student)); //新节点
printf("第%d个学生的姓名:\n",i+1);
scanf("%s",&s->name);
printf("第%d个学生的学号:\n",i+1);
scanf("%s",&s->sno);
p->next = s;
p = s;
}
p->next = NULL;
return head;
}
void deletea(struct student *a,struct student *b)
{
int flag;
struct student *p1,*p2,*q1;
q1 = b->next;
p1 = a;// 记录前驱结点
p2 = p1->next;
while(q1)// 双层循环,外层是b链表
{
flag = 0;//首次赋值以及重置吧,用于标记是否发生过相同值,是否移动p1指针
p1= a;
while(p1->next)
{
p2=p1->next;
flag=0;
if(!strcmp(p2->sno,q1->sno))
{
p1->next = p2->next;
free(p2);
flag =1;
}
if(flag==0)
{
p1=p1->next;
}
}
q1=q1->next;
}
}
void printfLink(struct student *link)
{
struct student *node;
node = link->next;
while(node!=NULL)
{
printf(" %s, %s \n",node->name,node->sno);
node=node->next;
}
}
int main()
{
int m,n;
printf("请输入a链表的长度:\n");
scanf("%d",&m);
printf("请输入b链表的长度:\n");
scanf("%d",&n);
struct student *a = creat(m);
struct student *b = creat(n);
deletea(a,b);
printfLink(a);
return 0;
}
有两个链表 a 和 b,设结点中包含学号、姓名。从 a 链表中删去与 b 链表中相同学号的结点
最新推荐文章于 2024-07-14 16:24:46 发布