

复制代码
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
1611 //成绩最高结点放在第一个结点 最低放在最后一个结点 2 // 3 //2017.3.9 4 // 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 typedef struct student su; 10 struct student 11 { 12 int num;//学号 13 int score;//分数 14 struct student *next; 15 }; 16 //初始化链表 17 su* init() 18 { 19 su *p = (su*)malloc(sizeof(su));//开辟空间 20 if (NULL == p)//养成习惯 判断 21 { 22 return NULL; 23 } 24 else 25 { 26 p->next = NULL;//第一个结点 不需要下一个地址 也没有目前 27 } 28 return p;//这样做的原因是保证多有的调用一致性 29 } 30 31 su* insert(su *head, int num, int score) 32 { 33 su *p = (su*)malloc(sizeof(su));//开辟空间 34 if (NULL == p) 35 { 36 printf("空"); 37 return NULL; 38 } 39 else 40 { 41 su *pp = head;//从头开始 42 if (pp==NULL) 43 { 44 return NULL; 45 } 46 else 47 { 48 while (pp->next != NULL) 49 { 50 pp = pp->next;//向后移动 51 } 52 p->score = score; 53 p->num = num; 54 p->next = NULL; 55 pp->next = p;//与前一个建立链接 56 } 57 } 58 } 59 60 //输出 61 void print(su *head) 62 { 63 su *index = head; 64 if (NULL == index) 65 { 66 return; 67 } 68 else 69 { 70 while (index->next != NULL) 71 { 72 printf("%d %d **n", index->next->num, index->next->score); 73 index = index->next; 74 } 75 { 76 77 } 78 } 79 } 80 81 //查找最大值 82 su *findmax(su *head) 83 { 84 su *index = head; 85 su *max = head;//假设开始为最大 86 if (index == NULL) 87 { 88 return NULL; 89 } 90 else 91 { 92 while (index->next!=NULL) 93 { 94 if (max->next->score < index->next->score) 95 { 96 max = index;//最大值结点 97 } 98 index = index->next; 99 100 } 101 } 102 return max; 103 } 104 105 //查找最小值 106 su *findmin(su *head) 107 { 108 su *index = head; 109 su *min = head; 110 if (index == NULL) 111 { 112 return NULL; 113 } 114 else 115 { 116 while (index->next != NULL) 117 { 118 if (min->next->score > index->next->score) 119 { 120 min = index;//最小值结点 121 } 122 index = index->next; 123 124 } 125 } 126 return min; 127 } 128 void Adjustlist(su * head) 129 { 130 su *pmin = findmin(head); // 查找成绩最小节点 131 su * temp = pmin->next; // temp为pmin的后一个节点 132 pmin->next = temp->next; // 把temp节点删除 133 su * index = head; // 搜索尾巴节点 134 while (index->next != NULL) 135 index = index->next; 136 index->next = temp; // 把temp放在最尾部 137 temp->next = NULL; 138 su *pmax = findmax(head); // 再找max节点 139 su * tempmax = pmax->next; // 把max节点放到链表头 140 pmax->next = tempmax->next; 141 tempmax->next = head->next; 142 head->next = tempmax; 143 } 144 145 void main() 146 { 147 su *p = init(); 148 insert(p, 1, 10); // 初始化链表节点 149 insert(p, 2, 1); 150 insert(p, 18, 13); 151 insert(p, 27, 14); 152 insert(p, 51, 16); 153 154 insert(p, 18, 11); 155 insert(p, 19, 17); 156 print(p); // 打印初始化结果 157 Adjustlist(p); // 调整位置,按照题目要求 158 printf("nnn"); 159 print(p); // 打印调整位置后结果 160 system("pause"); 161 }
转载于:https://www.cnblogs.com/lanjianhappy/p/6533408.html
最后
以上就是受伤花瓣最近收集整理的关于链表实现 最大值放在头 最小值放在尾的全部内容,更多相关链表实现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复