有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。
方法一:(链表删除操作得动态初始化链表,其余操作动态初始化或静态初始化链表都行)
#include
#include
//声明结构体struct Student
struct Student
{
int num;
char name[20];
struct Student *prev;
struct Student *next;
};
struct Student *input(int n);//声明输入函数
void append(struct Student **head2, struct Student *stu);//声明建立链表操作
void del_list(struct Student **, struct Student *);//声明删除两个链表中重复的多个节点函数
void del_item(struct Student **, struct Student *);//声明删除单个节点函数
void print(struct Student *);//声明输出函数操作
int main()
{
struct Student *a,*b;
a=input(3); //调用输入函数,生成a链表
print(a);
b=input(2); //调用输入函数,生成b链表
del_list(&a, b); //从a中删除b
print(a);
return 0;
}
//输入函数
struct Student *input(int n)
{
struct Student *head = NULL, *stu;
for (int i=1; i<=n; i++)//动态初始化链表
{
stu = (struct Student*)malloc(sizeof(struct Student));
printf("Please enter No.%d student info: ", i);
scanf("%d %s", &stu->num,stu->name);
append(&head, stu);
}
printf("\n");
return head;
}
//建立链表操作
void append(struct Student **head2, struct Student *stu)
{
stu->next = NULL;
if (*head2 == NULL)
{
stu->prev = NULL;
*head2 = stu;
}
else
{
struct Student *p = *head2;
while (p->next!=NULL)
p=p->next;
p->next = stu;
stu->prev = p;
}
}
//删除多个节点函数
void del_list(struct Student **list1, struct Student *list2)
{
struct Student *p, *q, *next;
for (q=list2; q!=NULL; q=q->next)
{
for (p=*list1; p!=NULL; p=p->next)
{
if (p->num == q->num)
{
del_item(list1, p);
}
}
}
}
//删除单个节点函数
void del_item(struct Student **head, struct Student *p)
{
if (p==*head)
*head=p->next;
if (p->prev!=NULL)
p->prev->next=p->next;
if (p->next!=NULL)
p->next->prev=p->prev;
free(p);
}
//输出函数
void print(struct Student *stu)
{
if(stu == NULL)
printf("Empty Linked list.\n");
else
{
struct Student *p;
for (p=stu; p!=NULL; p=p->next)
{
printf("%d %s\n", p->num, p->name);
}
}
}
方法一简化版:(简化input函数)
#include
#include
//声明结构体struct Student
struct Student
{
int num;
char name[20];
struct Student *prev;
struct Student *next;
};
struct Student *input(struct Student *stu, int n);//声明输入函数
void del_list(struct Student **, struct Student *);//声明删除两个链表中重复的多个节点函数
void del_item(struct Student **, struct Student *);//声明删除单个节点函数
void print(struct Student *);//声明输出函数操作
int main()
{
struct Student *a=(struct Student*)malloc(3*sizeof(struct Student));//相当于struct Student a[3]
struct Student *b=(struct Student*)malloc(2*sizeof(struct Student));//相当于struct Student b[3]
a=input(a, 3); //调用输入函数,生成a链表
print(a);
b=input(b, 2); //调用输入函数,生成b链表
del_list(&a, b); //从a中删除b
print(a);
free(a);
free(b);
return 0;
}
//输入函数
struct Student *input(struct Student *stu, int n)//先建立两个链表
{
int i=0;
struct Student *p;
for (p=stu; p<stu+n; p++, i++)
{
printf("Please enter No.%d student info; ", i+1);
scanf("%d %s", &p->num, p->name);
i==0?(p->prev=NULL):(p->prev=&stu[i-1]);
i==n-1 ? (p->next=NULL) : (p->next=&stu[i+1]);
}
return stu;
}
//删除多个节点函数
void del_list(struct Student **list1, struct Student *list2)
{
struct Student *p, *q, *next;
for (q=list2; q!=NULL; q=q->next)
{
for (p=*list1; p!=NULL; p=p->next)
{
if (p->num == q->num)
{
del_item(list1, p);
}
}
}
}
//删除单个节点函数
void del_item(struct Student **head, struct Student *p)
{
if (p==*head)
*head=p->next;
if (p->prev!=NULL)
p->prev->next=p->next;
if (p->next!=NULL)
p->next->prev=p->prev;
}
//输出函数
void print(struct Student *stu)
{
if(stu == NULL)
printf("Empty Linked list.\n");
else
{
struct Student *p;
for (p=stu; p!=NULL; p=p->next)
{
printf("%d %s\n", p->num, p->name);
}
}
}