前言
期末的上机实验是这题目,觉得难度较大,难度大主要在于用的是选择排序,不用个后驱的指针我做不出来。
上机实验用的是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");
}
题目要求是用多文件,这里不方便,就合成一个了。
花絮
老师上机实验课后,讲解题目时,他用的是冒泡排序,说冒泡排序比选择排序更难。
其实冒泡排序更容易,用的指针数目较选择排序的少。