删除链表a中与链表b具有相同年龄的结点,并把删除后得到的新的链表a与链表b合并,输出合并后的链表。

删除链表a中与链表b具有相同年龄的结点,并把删除后得到的新的链表a与链表b合并,输出合并后的链表。

方法:

#include <stdio.h>
#include <stdlib.h>

//声明结构体struct Student
struct Student
{                                        
    int num;
    char name[20];
	char sex;
    int age;
    float score;
	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*);//声明删除单个节点函数
struct Student *merge(struct Student *stu1, struct Student *stu2, int m);//声明合并函数
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);
	print(merge(a, b, 3));                                  //调用输出函数,参数为合并函数返回指针值
    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 %c %d", &p->num, p->name, &p->sex, &p->age);
		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;
	printf("在链表a中删除与链表b具有相同年龄的结点,则新的链表a为:\n");	
	for (q = list2; q != NULL; q = q->next)
	{
		for (p = *list1; p != NULL; )
		{
			if (p->age == q->age)
				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;
}

//合并函数
struct Student *merge(struct Student *stu1, struct Student *stu2, int m)
{
	struct Student *p = stu1;
	while (p->next!=NULL)
		p=p->next;
	p->next=stu2;                         //再合并,将a链表最末尾的next指向b链表首地址 
	printf("把删除后得到的新的链表a和链表b合并后得到的链表a为:\n");			
	return stu1;
}

//输出函数
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 %c %d\n", p->num, p->name, p->sex, p->age);
		}		
	}
}

在VS2019下,需将源文件的scanf做些修改:

#include <stdio.h>
#include <stdlib.h>

//声明结构体struct Student
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
	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*);//声明删除单个节点函数
struct Student* merge(struct Student* stu1, struct Student* stu2, int m);//声明合并函数
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);
	print(merge(a, b, 3));                                  //调用输出函数,参数为合并函数返回指针值
	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 %c %d", &p->num, p->name, (unsigned int)sizeof(p->name), &p->sex, (unsigned int)sizeof(p->sex), &p->age);
		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;
	printf("在链表a中删除与链表b具有相同年龄的结点,则新的链表a为:\n");
	for (q = list2; q != NULL; q = q->next)
	{
		for (p = *list1; p != NULL; )
		{
			if (p->age == q->age)
				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;
}

//合并函数
struct Student* merge(struct Student* stu1, struct Student* stu2, int m)
{
	struct Student* p = stu1;
	while (p->next != NULL)
		p = p->next;
	p->next = stu2;                         //再合并,将a链表最末尾的next指向b链表首地址 
	printf("把删除后得到的新的链表a和链表b合并后得到的链表a为:\n");
	return stu1;
}

//输出函数
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 %c %d\n", p->num, p->name, p->sex, p->age);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值