实验五 结构体程序设计(五)

一、实验目的及要求

  1. 进一步理解结构体的概念;
  2. 掌握结构体的定义和结构体变量的定义和使用方法;
  3. 能正确使用结构体数组;
  4. 掌握链表的基本概念,能够编写简单的应用程序
  5. 进一步提高编程能力。

7.有两个链表ab。每个链表中节点包括学号、成绩。要求把两个链表合并。(自己编程建立链表)。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student {
	int id;
	char name[1024];
	int score[3];
	struct student* next;
};
typedef struct student stu;

stu* creat(int n) 
{//创建链表
	stu* s;
	stu* head;
	stu* p;
	s = (stu*)malloc(sizeof(stu));
	head = s;
	p = head;
	for (int i = 0; i < n; ++i) {
		s= (stu*)malloc(sizeof(stu));
		printf("请输入第%d个学生的信息\n", i + 1);
		printf("请输入学号,姓名\n");
		scanf("%d%s", &s->id, &s->name);
		for (int j = 0; j < 3; ++j) 
		{
			printf("请输入第%d门课的成绩\n", j + 1);
			scanf("%d", &s->score[j]);
		}
		s->next = p->next;
		p->next = s;
		p = p->next;
		if (i == n - 1) {
			p->next = NULL;
		}
	}
	return head;
}
void print(stu* head) {//输出
	for (stu* p = head->next; p != NULL; p = p->next) {
		printf("%d\t%s\t%d\t%d\t%d\n", p->id, p->name, p->score[0], p->score[1], p->score[2]);
	}
}
void fun(stu* a, stu* b) {//合并
	stu* p;
	for (p = a; p->next != NULL; p = p->next);//找到a的最后一个指针
	p->next = b->next;//让它指向b
	free(b);

}
int main() {
	int n, m;
	printf("请输入链表a的长度和链表b的长度\n");
	scanf("%d%d", &n,&m);
	printf("请输入\n");
	printf("请输入链表a中的学生信息\n");
	stu* a = creat(n);
	printf("请输入链表b中的学生信息\n");
	stu* b = creat(m);
	printf("链表a学生信息\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	print(a);
	printf("链表b学生信息\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	print(b);
	printf("合并后为\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	fun(a, b);
	print(a);
	system("pause");
}

8.在题7的基础上,要求合并两个链表,但是要求按学号升序。(可以借助第三个链表)。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student {
	int id;
	char name[1024];
	int score[3];
	struct student* next;
};
typedef struct student stu;

stu* creat(int n) 
{//创建链表
	stu* s;
	stu* head;
	stu* p;
	s = (stu*)malloc(sizeof(stu));
	head = s;
	p = head;
	for (int i = 0; i < n; ++i) {
		s= (stu*)malloc(sizeof(stu));
		printf("请输入第%d个学生的信息\n", i + 1);
		printf("请输入学号,姓名\n");
		scanf("%d%s", &s->id, &s->name);
		for (int j = 0; j < 3; ++j) 
		{
			printf("请输入第%d门课的成绩\n", j + 1);
			scanf("%d", &s->score[j]);
		}
		s->next = p->next;
		p->next = s;
		p = p->next;
		if (i == n - 1) {
			p->next = NULL;
		}
	}
	return head;
}
void print(stu* head) {//输出
	for (stu* p = head->next; p != NULL; p = p->next) {
		printf("%d\t%s\t%d\t%d\t%d\n", p->id, p->name, p->score[0], p->score[1], p->score[2]);
	}
}
void swap(stu* p, stu* q) {//交换函数
	p->id = p->id^q->id;
	q->id = p->id^q->id;
	p->id = p->id^q->id;
	char temp[1024];
	strcpy(temp, p->name);
	strcpy(p->name, q->name);
	strcpy(q->name, temp);
	for (int i = 0; i < 3; ++i) {
		p->score[i] = p->score[i] ^ q->score[i];
		q->score[i] = p->score[i] ^ q->score[i];
		p->score[i] = p->score[i] ^ q->score[i];
	}
}
void fun(stu* a, stu* b) {//合并并排序
	stu* p;
	for (p = a; p->next != NULL; p = p->next);//找到a的最后一个指针
	p->next = b->next;//让它指向b
	free(b);
	for (p = a->next; p != NULL; p = p->next) { 
		for (stu* q = p->next; q != NULL; q = q->next) { //
			if (p->id > q->id) {
				swap(p, q);
			}
		}
	}
}
int main() {
	int n, m;
	printf("请输入链表a的长度和链表b的长度\n");
	scanf("%d%d", &n,&m);
	printf("请输入\n");
	printf("请输入链表a中的学生信息\n");
	stu* a = creat(n);
	printf("请输入链表b中的学生信息\n");
	stu* b = creat(m);
	printf("链表a学生信息\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	print(a);
	printf("链表b学生信息\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	print(b);
	printf("合并排序后为\n学号\t姓名\t成绩1\t成绩2\t成绩3\n");
	fun(a, b);
	print(a);
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值