链表排序

自己做的一个链表排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
typedef struct grade
{
string number;
string name;
int chinese;
int math;
int english;
struct grade* next;
}grade;
typedef struct point
{
grade* head;
grade* tail;
grade* target1;
grade* target2;
grade* tmp1;
grade* tmp2;
grade* now;
grade* tt;
int cnt;
}point;
void 读取文件(point* p);
void 打印成绩(point* p);
void 释放空间(point* p);
void 搜索目标(point* p);
void 链表排序(point* p);
int main()
{
point point;
读取文件(&point);
打印成绩(&point);
链表排序(&point);
打印成绩(&point);
释放空间(&point);
}
void 读取文件(point* p)
{
ifstream fin("111.txt");
p->head = nullptr;
p->tail = nullptr;
p->cnt = 0;
grade* now = nullptr;
if (fin.is_open())
{
while (!fin.eof())
{
now = new grade;
fin >> now->number;
fin >> now->name;
fin >> now->chinese;
fin >> now->english;
fin >> now->math;
now->next = nullptr;
if (p->head == nullptr)
{
p->head = now;
}
else
{
p->tail->next = now;
}
p->tail = now;
(p->cnt)++;
}
fin.close();
}
else
cout << "打不开文件!";
}
void 打印成绩(point* p)
{
grade* now = p->head;
while (now)
{
cout << now->number << "\t";
cout << now->name << "\t\t";
cout << now->chinese << "\t";
cout << now->math << "\t";
cout << now->english << endl;
now = now->next;
}
cout << "共有" << p->cnt << "名学生。\n";
}
void 释放空间(point* p)
{
grade* now = p->head, * tmp = nullptr;
while (now)
{
tmp = now;
now = now->next;
delete tmp;
}
}
void 链表排序(point* p)
{
p->target1 = p->target2 = p->head;
while (p->target1)
{
p->tmp2 = p->target1;
p->target2 = p->target1->next;
p->tt = p->target1;
搜索目标(p);
if (p->target2!=p->target1)
{
if (p->target1->next == p->target2)
{
if (p->target1 == p->head)
{
p->target1->next = p->target2->next;
p->target2->next = p->target1;
p->head = p->target2;
}
else
{
p->tmp1->next = p->target2;
p->target1->next = p->target2->next;
p->target2->next = p->target1;
}
}
else
{
if (p->target1 == p->head)
{
p->target2->next = p->target1->next;
p->target1->next = p->now;
p->tmp2->next = p->target1;
p->head = p->target2;
}
else
{
p->target2->next = p->target1->next;
p->tmp1->next = p->target2;
p->tmp2->next = p->target1;
p->target1->next = p->now;
}
}
p->now = p->target1;
p->target1 = p->target2;
p->target2 = p->now;
}
p->tmp1 = p->target1;
p->target1 = p->target1->next;
}

}
void 搜索目标(point* p)
{
while (p->target2)
{
if (p->tt->chinese < p->target2->chinese)
{
p->now = p->tmp2;
p->tt = p->target2;
}
p->tmp2 = p->tmp2->next;
p->target2 = p->target2->next;
}
p->target2 = p->tt;
p->tmp2 = p->now;
p->now = p->target2->next;
}
作者

我是呆呆啊

发布于

2021-01-09

更新于

2021-01-09

许可协议

评论