已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。

已有a,b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。

方法一:

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

struct Student
{                                        //声明结构体struct Student
	int num;
	float score;
	struct Student* prev;
	struct Student* next;
};

struct Student* input(int);
void append(struct Student**, struct Student*);
void merge(struct Student**, struct Student*);
void sort(struct Student**);
void swap(struct Student*, struct Student*);
void print(struct Student*);

int main()
{
	struct Student* a, * b;                          //定义链表a和b
	a = input(3);                                   //调用输入函数,输入a链表
	b = input(2);                                   //调用输入函数,输入b链表
	merge(&a, b);                                   //合并a和b
	sort(&a);                                       //将a进行排序
	print(a);                                       //输出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 %f", &stu->num, &stu->score);
		append(&head, stu);
	}
	printf("\n");
	return head;
}

//建立链表操作 
void append(struct Student** head, struct Student* stu)
{
	stu->next = NULL;
	if (*head == NULL)
	{
		stu->prev = NULL;
		*head = stu;
	}
	else
	{
		struct Student* p = *head;
		while (p->next != NULL)
			p = p->next;
		p->next = stu;
		stu->prev = p;
	}
}

//合并函数
void merge(struct Student** list1, struct Student* list2)
{
	if (*list1 == NULL)
	{
		*list1 = list2;
	}
	else
	{
		struct Student* p = *list1;
		while (p->next != NULL)
			p = p->next;
		p->next = list2;
		list2->prev = p;
	}
}

//排序函数
void sort(struct Student** head)
{
	struct Student* p, * q, * i=NULL;
	int min=0;
	for (q = *head; q != NULL; q = q->next)
	{
		for (p = q; p != NULL; p = p->next)
		{
			if (min > p->num || p == q)
			{
				i = p;
				min = p->num;
			}
		}
		swap(q, i);
	}
}

//交换元素函数
void swap(struct Student* a, struct Student* b)
{
	struct Student* ap, * an, * bp, * bn;
	struct Student stu;

	ap = a->prev;
	an = a->next;
	bp = b->prev;
	bn = b->next;

	stu = *a;
	*a = *b;
	*b = stu;

	a->prev = ap;
	a->next = an;
	b->prev = bp;
	b->next = bn;
}

//输出函数
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 %.2f\n", p->num, p->score);
		}
	}
}

在VS2019下,需将源文件的scanf改为scanf_s:

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

struct Student
{                                        //声明结构体struct Student
	int num;
	float score;
	struct Student* prev;
	struct Student* next;
};

struct Student* input(int);
void append(struct Student**, struct Student*);
void merge(struct Student**, struct Student*);
void sort(struct Student**);
void swap(struct Student*, struct Student*);
void print(struct Student*);

int main()
{
	struct Student* a, * b;                          //定义链表a和b
	a = input(3);                                   //调用输入函数,输入a链表
	b = input(2);                                   //调用输入函数,输入b链表
	merge(&a, b);                                   //合并a和b
	sort(&a);                                       //将a进行排序
	print(a);                                       //输出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 %f", &stu->num, &stu->score);
		append(&head, stu);
	}
	printf("\n");
	return head;
}

//建立链表操作 
void append(struct Student** head, struct Student* stu)
{
	stu->next = NULL;
	if (*head == NULL)
	{
		stu->prev = NULL;
		*head = stu;
	}
	else
	{
		struct Student* p = *head;
		while (p->next != NULL)
			p = p->next;
		p->next = stu;
		stu->prev = p;
	}
}

//合并函数
void merge(struct Student** list1, struct Student* list2)
{
	if (*list1 == NULL)
	{
		*list1 = list2;
	}
	else
	{
		struct Student* p = *list1;
		while (p->next != NULL)
			p = p->next;
		p->next = list2;
		list2->prev = p;
	}
}

//排序函数
void sort(struct Student** head)
{
	struct Student* p, * q, * i = NULL;
	int min = 0;
	for (q = *head; q != NULL; q = q->next)
	{
		for (p = q; p != NULL; p = p->next)
		{
			if (min > p->num || p == q)
			{
				i = p;
				min = p->num;
			}
		}
		swap(q, i);
	}
}

//交换元素函数
void swap(struct Student* a, struct Student* b)
{
	struct Student* ap, * an, * bp, * bn;
	struct Student stu;

	ap = a->prev;
	an = a->next;
	bp = b->prev;
	bn = b->next;

	stu = *a;
	*a = *b;
	*b = stu;

	a->prev = ap;
	a->next = an;
	b->prev = bp;
	b->next = bn;
}

//输出函数
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 %.2f\n", p->num, p->score);
		}
	}
}

方法一简化版:(简化input函数)

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

//声明结构体struct Student
struct Student
{                                             
	int num;								
	float score;
	struct Student* prev;
	struct Student *next;
};

void input(struct Student *stu, int n);                            
void merge(struct Student**, struct Student*);
void sort(struct Student**);
void swap(struct Student*, struct Student*);
void print(struct Student *stu);                                   

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]
	input(a, 3);                                            //调用输入函数,生成a链表
	input(b, 2);                                            //调用输入函数,生成b链表
	merge(&a, b);                                   //合并a和b
	sort(&a);                                       //将a进行排序
	print(a);                                       //输出a
	return 0;
}

//输入函数
void 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 %f", &p->num, &p->score);
		i==n-1 ? (p->next=NULL) : (p->next=&stu[i+1]);
	}
	printf("\n");
}

//合并函数
void merge(struct Student** list1, struct Student* list2)
{
	if (*list1 == NULL)
	{
		*list1 = list2;
	}
	else
	{
		struct Student* p = *list1;
		while (p->next != NULL)
			p = p->next;
		p->next = list2;
		list2->prev = p;
	}
}

//排序函数
void sort(struct Student** head)
{
	struct Student* p, * q, * i=NULL;
	int min=0;
	for (q = *head; q != NULL; q = q->next)
	{
		for (p = q; p != NULL; p = p->next)
		{
			if (min > p->num || p == q)
			{
				i = p;
				min = p->num;
			}
		}
		swap(q, i);
	}
}

//交换元素函数
void swap(struct Student* a, struct Student* b)
{
	struct Student* ap, * an, * bp, * bn;
	struct Student stu;

	ap = a->prev;
	an = a->next;
	bp = b->prev;
	bn = b->next;

	stu = *a;
	*a = *b;
	*b = stu;

	a->prev = ap;
	a->next = an;
	b->prev = bp;
	b->next = bn;
}

//输出函数
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 %.2f\n", p->num, p->score);
		}
	}
}

在VS2019下,需将源文件的scanf改为scanf_s:

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

//声明结构体struct Student
struct Student
{
	int num;
	float score;
	struct Student* prev;
	struct Student* next;
};

void input(struct Student* stu, int n);
void merge(struct Student**, struct Student*);
void sort(struct Student**);
void swap(struct Student*, struct Student*);
void print(struct Student* stu);

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]
	input(a, 3);                                            //调用输入函数,生成a链表
	input(b, 2);                                            //调用输入函数,生成b链表
	merge(&a, b);                                   //合并a和b
	sort(&a);                                       //将a进行排序
	print(a);                                       //输出a
	return 0;
}

//输入函数
void 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 %f", &p->num, &p->score);
		i == n - 1 ? (p->next = NULL) : (p->next = &stu[i + 1]);
	}
	printf("\n");
}

//合并函数
void merge(struct Student** list1, struct Student* list2)
{
	if (*list1 == NULL)
	{
		*list1 = list2;
	}
	else
	{
		struct Student* p = *list1;
		while (p->next != NULL)
			p = p->next;
		p->next = list2;
		list2->prev = p;
	}
}

//排序函数
void sort(struct Student** head)
{
	struct Student* p, * q, * i = NULL;
	int min = 0;
	for (q = *head; q != NULL; q = q->next)
	{
		for (p = q; p != NULL; p = p->next)
		{
			if (min > p->num || p == q)
			{
				i = p;
				min = p->num;
			}
		}
		swap(q, i);
	}
}

//交换元素函数
void swap(struct Student* a, struct Student* b)
{
	struct Student* ap, * an, * bp, * bn;
	struct Student stu;

	ap = a->prev;
	an = a->next;
	bp = b->prev;
	bn = b->next;

	stu = *a;
	*a = *b;
	*b = stu;

	a->prev = ap;
	a->next = an;
	b->prev = bp;
	b->next = bn;
}

//输出函数
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 %.2f\n", p->num, p->score);
		}
	}
}

方法一终极简化版(简化input函数+简化sort函数):

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

struct Student
{                                        //声明结构体struct Student
	int num;
	float score;
	struct Student* prev;
	struct Student* next;
};

void input(struct Student*, int);
struct Student *merge_sort(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]
	input(a, 3);                                   //调用输入函数,输入a链表
	input(b, 2);                                   //调用输入函数,输入b链表
	merge_sort(a,b,3);                                   //合并a和b,并对学号排序 
	print(a);                                       //输出a
	return 0;
}

//输入函数
void 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 %f", &p->num, &p->score);
		i==n-1 ? (p->next=NULL) : (p->next=&stu[i+1]);
	}
	printf("\n");
}

//合并+排序函数
struct Student *merge_sort(struct Student *stu1, struct Student *stu2, int m)
{
	struct Student *p = stu1;
	while (p->next!=NULL)
		p=p->next;
	p->next=stu2;
	struct Student *q, temp;
	for (p=stu1; p!=NULL; p=p->next)//直接选择排序法对链表的学号由小到大排序,对学号排序就要交换学号,但由于一个学号对应一个成绩,所以也要对成绩排序 
	{
		for (q=p->next; q!=NULL; q=q->next)//同时也要注意交换时只交换结构体变量中的学号和成绩,next指针不交换改变,为了保持原链表的链接顺序。
		{
			if (p->num>q->num)
			{
				temp.num=p->num;
				p->num=q->num;
				q->num=temp.num;
				temp.score=p->score;
				p->score=q->score;
				q->score=temp.score;
			}
		} 
	}			
	return stu1;
}

//输出函数
void print(struct Student *stu)
{
	struct Student *p;
	for (p=stu; p!=NULL; p=p->next)
	{
		printf("%d %.2f\n", p->num, p->score);
	}
}

在VS2019下,需将源文件的scanf改为scanf_s:

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

struct Student
{                                        //声明结构体struct Student
	int num;
	float score;
	struct Student* prev;
	struct Student* next;
};

void input(struct Student*, int);
struct Student* merge_sort(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]
	input(a, 3);                                   //调用输入函数,输入a链表
	input(b, 2);                                   //调用输入函数,输入b链表
	merge_sort(a, b, 3);                                   //合并a和b,并对学号排序 
	print(a);                                       //输出a
	return 0;
}

//输入函数
void 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 %f", &p->num, &p->score);
		i == n - 1 ? (p->next = NULL) : (p->next = &stu[i + 1]);
	}
	printf("\n");
}

//合并+排序函数
struct Student* merge_sort(struct Student* stu1, struct Student* stu2, int m)
{
	struct Student* p = stu1;
	while (p->next != NULL)
		p = p->next;
	p->next = stu2;
	struct Student* q, temp;
	for (p = stu1; p != NULL; p = p->next)//直接选择排序法对链表的学号由小到大排序,对学号排序就要交换学号,但由于一个学号对应一个成绩,所以也要对成绩排序 
	{
		for (q = p->next; q != NULL; q = q->next)//同时也要注意交换时只交换结构体变量中的学号和成绩,next指针不交换改变,为了保持原链表的链接顺序。
		{
			if (p->num > q->num)
			{
				temp.num = p->num;
				p->num = q->num;
				q->num = temp.num;
				temp.score = p->score;
				p->score = q->score;
				q->score = temp.score;
			}
		}
	}
	return stu1;
}

//输出函数
void print(struct Student* stu)
{
	struct Student* p;
	for (p = stu; p != NULL; p = p->next)
	{
		printf("%d %.2f\n", p->num, p->score);
	}
}
  • 10
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值