有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。
方法一:(链表删除操作得动态初始化链表,其余操作动态初始化或静态初始化链表都行)
#include <stdio.h>
#include <stdlib.h>
//声明结构体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*);//声明删除两个链表中重复的多个节点函数
struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
void print(struct Student *);//声明输出函数操作
int main()
{
struct Student *a,*b;
a=input(3); //调用输入函数,生成a链表
print(a);
printf("\n");
b=input(2); //调用输入函数,生成b链表
del_list(&a, b); //从a中删除b
print(a);
return 0;
}
//输入函数
struct Student *input(int n)
{
struct Student *head = NULL, *stu;
int i=0;
for (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);
}
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;
for (q = list2; q != NULL; q = q->next)
{
for (p = *list1; p != NULL; )
{
if (p->num == q->num)
p = del_item(list1, p);
else
p = p->next;
}
}
}
//删除单个节点函数
struct Student* 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;
struct Student* next = p->next;
return next;
}
//输出函数
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);
}
}
}
在VS2019下,需将源文件的scanf做些修改:
#include <stdio.h>
#include <stdlib.h>
//声明结构体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*);//声明删除两个链表中重复的多个节点函数
struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
void print(struct Student*);//声明输出函数操作
int main()
{
struct Student* a, * b;
a = input(3); //调用输入函数,生成a链表
print(a);
printf("\n");
b = input(2); //调用输入函数,生成b链表
del_list(&a, b); //从a中删除b
print(a);
return 0;
}
//输入函数
struct Student* input(int n)
{
struct Student* head = NULL, * stu;
int i = 0;
for (i = 1; i <= n; i++)//动态初始化链表
{
stu = (struct Student*)malloc(sizeof(struct Student));
printf("Please enter No.%d student info: ", i);
scanf_s("%d %s", &stu->num, stu->name, (unsigned int)sizeof(stu->name));
append(&head, stu);
}
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;
for (q = list2; q != NULL; q = q->next)
{
for (p = *list1; p != NULL; )
{
if (p->num == q->num)
p = del_item(list1, p);
else
p = p->next;
}
}
}
//删除单个节点函数
struct Student* 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;
struct Student* next = p->next;
return next;
}
//输出函数
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 <stdio.h>
#include <stdlib.h>
//声明结构体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*);//声明删除两个链表中重复的多个节点函数
struct Student* 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);
printf("\n");
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;
for (q = list2; q != NULL; q = q->next)
{
for (p = *list1; p != NULL; )
{
if (p->num == q->num)
p = del_item(list1, p);
else
p = p->next;
}
}
}
//删除单个节点函数
struct Student* 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;
struct Student* next = p->next;
return next;
}
//输出函数
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);
}
}
}
在VS2019下,需将源文件的scanf做些修改:
#include <stdio.h>
#include <stdlib.h>
//声明结构体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*);//声明删除两个链表中重复的多个节点函数
struct Student* 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);
printf("\n");
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_s("%d %s", &p->num, p->name, (unsigned int)sizeof(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;
for (q = list2; q != NULL; q = q->next)
{
for (p = *list1; p != NULL; )
{
if (p->num == q->num)
p = del_item(list1, p);
else
p = p->next;
}
}
}
//删除单个节点函数
struct Student* 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;
struct Student* next = p->next;
return next;
}
//输出函数
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);
}
}
}