大一上册期末上机实验之:用选择排序法不修改结点的值仅改变指针指向完成链表的排序

前言

期末的上机实验是这题目,觉得难度较大,难度大主要在于用的是选择排序,不用个后驱的指针我做不出来。
上机实验用的是Turbo C ,辣眼睛的蓝底黄字看得不舒服。

做题思路

难点一:选择排序时要分类讨论,交换时是否涉及头指针,交换时两个结点是否相邻即可。
难点二:因为要求排序过程不能修改结点内的值,所以得通过改变指针的指向来进行排序。

题目

1.实验报告正文
多文件多函数编程llistS.prj. -->LListS. exe
链表节点基类型struct Node {int x ; struct Node* next };。
1、输入若干个整数,以0结束,把这些整数(不包括0)放入一个单向动态链表中,再按选择排序法对链表节点的数据从大倒小的次序排列,最后将排好序的链表输出。
L1.c:生成新的动态链表,返回链表的头指针,新节点的插入位置在链表末尾,输出链表的头指针.函数原型: struct Node* CreateLLlist ( );
L2. c:将链表的节点数据按选择排序法从大到小的次序排列,输出链表的头指针,输出链表的头指针函教原型: struct Node* SortIList (struct Node* head);
L3. c:将链表的节点数据输出
函数原型: void PrintLList (struct Node* head);
L4.c:主调函数
int n; /* n为全局变量,用来记录链表节点的数量*/
void main()
{
struct Hode* h1, *h2;
h1=CrateLList (); PrintLList (h1); h1=SortLList (h1); PrintLList (h1);
h2= CreateLList (); PrintLList (h2); h2=SortLList (h2); PrintLList (h2);
}
测试用例1:输入的数据{1,3,5,7,11,12,5,6,0}排序后{12,11,7,6,5,5,3,1}.
测试用例2:输入的数据{-8,9,12,7,6, 100,0} 排序后{100,12,9,7,6,-8}
要求1:L2c只能使用一个二重循环实现用选择排序法排序的过程
要求2:L2c排序的过程当中,每个节点的数据域成员不发生变化。
要求3:L2c排序的过程当中,只允许按需进行动态内存分配,不容许有冗余空间作为额外存储处理。

初代码

一开始打的冗杂,没有想得太多,以完成功能为目的,打算打完再简化代码。
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Node)
int n;
struct Node *CreateLList();
struct Node *SortLList(struct Node*head);
void PrintLList(struct Node *head);
struct Node
{
	int x;
	struct Node*next;
};
int main()
{
	struct Node*h1, *h2;
	h1 = CreateLList(); PrintLList(h1); h1 = SortLList(h1); PrintLList(h1);
	h2 = CreateLList(); PrintLList(h2); h1 = SortLList(h2); PrintLList(h2);
	return 0;
}
struct Node *CreateLList()
{
	n = 0;
	struct Node *head, *p1, *p2;
	p1 = p2 = (struct Node*)malloc(LEN);
	scanf_s("%d", &p1->x);
	for(head = NULL; p1->x != 0;)
	{
		n ++;
		if (n == 1)head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct Node*)malloc(LEN);
		scanf_s("%d", &p1->x);
	}
	p2->next = NULL;
	return head;
}
struct Node *SortLList(struct Node*head)
{
	struct Node *p1, *pre2, *p2, *prem, *pm;
	p1 = p2 = head;
	for (int i = 0; i < n - 1; i++)
	{
		for (pm = p2, p1 = p2->next; p1 != NULL; p1 = p1->next)//找到最大值
			if (p1->x > pm->x)
				pm = p1;
		if (pm == p2)//最大值是p2时,进入下一次
		{
			p2 = p2->next;
			p1 = p2;
			continue;
		}
		for (pre2 = head; pre2->next != p2 && pre2 != p2; pre2 = pre2->next);
		for (prem = p2; prem->next != pm; prem = prem->next);
		if (i == 0)//涉及到头指针
		{
			if (p2->next== pm)//相邻,简单的交换 
			{
				p2->next = pm->next;
				head = pm;
				pm->next = p2;
			}
			else {	//不相邻,需要prem
				prem->next = pm->next;
				head = pm;
				pm->next = p2->next;
				p2->next = prem->next;
				prem->next = p2;
			}
			PrintLList(head);
		}
		else //不涉及头指针(其实也就是把上面的head换成Pre2->next),然后看是否相邻,相邻的时候不需要prem
		{
			if (p2->next == pm)//相邻,简单的交换 
			{
				p2->next = pm->next;
				pre2->next = pm;
				pm->next = p2;
			}
			else {//不相邻,需要prem
				prem->next = pm->next;
				pre2->next = pm;
				pm->next = p2->next;
				p2->next = prem->next;
				prem->next = p2;
			}
			PrintLList(head);
		}
		//借助pre2保持一个一个排下来
		if (i == 0)
		{
			pre2 = head;
			p2 = pre2->next;
			p1 = p2;
		}
		else {
			p2 = pre2->next;
			p2 = p2->next;
			p1 = p2;
		}
	}
	return head;
}
void PrintLList(Node * head)
{
	for(struct Node *p1 = head; p1 != NULL; p1 = p1->next)
		printf("%d	", p1->x);
	printf("\n");
}

改进的代码

改进后代码量较少,合并了交换时重复的代码。不过就不如上面的代码容易看懂。另外这里还是有很大的优化空间,例如两个找后驱结点的指针的for循环。

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Node)
int n;
struct Node *CreateLList();
struct Node *SortLList(struct Node*head);
void PrintLList(struct Node *head);
struct Node
{
	int x;
	struct Node*next;
};
int main()
{
	struct Node*h1, *h2;
	h1 = CreateLList(); PrintLList(h1); h1 = SortLList(h1); PrintLList(h1);
	h2 = CreateLList(); PrintLList(h2); h2 = SortLList(h2); PrintLList(h2);
	return 0;
}
struct Node *CreateLList()
{
	n = 0;
	struct Node *head, *p1, *p2;
	p1 = p2 = (struct Node*)malloc(LEN);
	for(head = NULL; scanf_s("%d", &p1->x),p1->x != 0;)
	{
		n ++;
		if (n == 1)head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct Node*)malloc(LEN);
	}
	p2->next = NULL;
	return head;
}
struct Node *SortLList(struct Node*head)
{
	struct Node *p1=head, *pre2, *p2=head, *prem, *pm;
	for (int i = 0; i < n - 1; i++){
		for (pm = p2, p1 = p2->next; p1 != NULL; p1 = p1->next)
			if (p1->x > pm->x)
				pm = p1;
		if (pm == p2){
			p1= p2 = p2->next;
			continue;
		}
		for (pre2 = head; pre2->next != p2 && pre2 != p2; pre2 = pre2->next);
		for (prem = p2; prem->next != pm; prem = prem->next);
			if (p2->next== pm){
				p2->next = pm->next;
				if (i == 0)head = pm;
				else pre2->next = pm;
				pm->next = p2;
			}
			else {			
				prem->next = pm->next;
				if (i == 0)head = pm;
				else pre2->next = pm;
				pm->next = p2->next;
				p2->next = prem->next;
				prem->next = p2;
			}
		if (i == 0)p2 = head->next;
		else  p2 = pre2->next->next;
		p1 = p2;
	}
	return head;
}
void PrintLList(Node * head)
{
	for(struct Node *p1 = head; p1 != NULL; p1 = p1->next)
		printf("%d  ", p1->x);
	printf("\n");
}

题目要求是用多文件,这里为了方便发,就合成一个吧。

花絮

老师上机实验课后,讲解题目时,他用的是冒泡排序,说冒泡排序比选择排序更难。其实用冒泡排序更容易,使用的指针数目会比使用选择排序所需要的少。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值