有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。

有两个链表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);
		}
	}
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值