1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 struct Student 6 { 7 string name; 8 int score; 9 Student* next; 10 }; 11 Student* head = NULL; 12 void addNode(string n, int s); 13 void printNodeInfo(Student* temp); 14 void releaseNode(Student* temp); 15 double average(Student* temp); 16 void sortNodeByScore(Student* temp); 17 void deleteNodeByName(Student* temp, string n); 18 19 int main() 20 { 21 addNode("zhangsan", 40); 22 addNode("lisi", 90); 23 addNode("wangwu", 80); 24 addNode("sunliu", 70); 25 addNode("qiangqi", 50); 26 printNodeInfo(head); 27 //double result = average(head); 28 //cout << "result: " << result << endl; 29 //sortNodeByScore(head); 30 //cout << "after sort\n"; 31 //printNodeInfo(head); 32 33 cout << "delete node by name\n"; 34 deleteNodeByName(head,"sunliu"); 35 printNodeInfo(head); 36 deleteNodeByName(head,"xiaowangzi"); 37 38 releaseNode(head); 39 system("pause"); 40 return 0; 41 } 42 void addNode(string n, int s) 43 { 44 //创建临时节点 45 Student* temp = NULL; 46 Student* stu = new Student; 47 stu->name = n; 48 stu->score = s; 49 if (head == NULL) 50 { 51 head = stu; 52 head->next = NULL; 53 cout << "添加节点成功\n"; 54 } 55 else 56 { 57 temp = head; 58 while (temp->next != NULL) 59 { 60 temp = temp->next; 61 } 62 temp->next = stu; 63 stu->next = NULL; 64 cout << "添加节点成功\n"; 65 } 66 } 67 void printNodeInfo(Student* temp) 68 { 69 if (temp == NULL) 70 { 71 cout << "链表为空\n"; 72 } 73 else 74 { 75 while (temp != NULL) 76 { 77 cout << "name: " << temp->name << " score: " << temp->score << endl; 78 temp = temp->next; 79 } 80 } 81 } 82 void releaseNode(Student* temp) 83 { 84 Student* newP = NULL; 85 if (temp == NULL) 86 { 87 cout << "链表节点内存为空\n"; 88 } 89 else 90 { 91 while (temp != NULL) 92 { 93 newP = temp; 94 temp = temp->next; 95 delete newP; 96 cout << "节点内存清除成功\n"; 97 } 98 } 99 } 100 //遍历 求成绩平均值 101 double average(Student* temp) 102 { 103 double average; 104 double sum = 0; 105 int count = 0; 106 if (temp == NULL) 107 { 108 cout << "链表为空\n"; 109 //处理除数为0的情况 110 return 0; 111 } 112 else 113 { 114 while (temp != NULL) 115 { 116 sum += temp->score; 117 count++; 118 temp = temp->next; 119 } 120 } 121 return sum / count; 122 123 } 124 //void sortNodeByScore(Student* temp) 125 //{ 126 // //创建一个临时节点 127 // Student* newP = NULL; 128 // //创建内层循环节点 129 // Student* newPIn = NULL; 130 // if (temp == NULL) 131 // { 132 // cout << "链表为空,不需要排序\n"; 133 // } 134 // else 135 // { 136 // //创建双重循环来实现按score排序 137 // newP = temp; 138 // while (newP->next != NULL) 139 // { 140 // newPIn = newP; 141 // while (newPIn->next != NULL) 142 // { 143 // if (newP->score > newPIn->next->score) 144 // { 145 // //交换节点的score值 146 // int tempValue; 147 // tempValue = newP->score; 148 // newP->score = newPIn->next->score; 149 // newPIn->next->score = tempValue; 150 // } 151 // newPIn = newPIn->next; 152 // } 153 // newP = newP->next; 154 // } 155 // //如果只有一个节点 代码没问题 156 // } 157 //} 158 //代码优化 创建一个临时节点即可 159 void sortNodeByScore(Student* temp) 160 { 161 //创建内层循环节点 162 Student* newPIn = NULL; 163 if (temp == NULL) 164 { 165 cout << "链表为空,不需要节点\n"; 166 } 167 else 168 { 169 //创建双重循环按score排序 170 while (temp->next != NULL) 171 { 172 newPIn = temp; 173 while (newPIn->next != NULL) 174 { 175 if (temp->score > newPIn->next->score) 176 { 177 int tempValue; 178 tempValue = temp->score; 179 temp->score = newPIn->next->score; 180 newPIn->next->score = tempValue; 181 } 182 newPIn = newPIn->next; 183 } 184 temp = temp->next; 185 } 186 } 187 } 188 void deleteNodeByName(Student* temp, string n) 189 { 190 //创建一个临时节点 用来保存要删除节点的前一个节点 191 Student* newP = NULL; 192 //创建一个节点 用来保存要删除的节点 193 Student* newPDelete = NULL; 194 if (temp == NULL) 195 { 196 cout << "链表为空,没有可以删除的节点\n"; 197 } 198 else 199 { 200 newP = temp; 201 //如果只有一个节点 头节点 202 if (newP->next == NULL) 203 { 204 if (newP->name.compare(n) == 0) 205 { 206 delete newP; 207 return; 208 } 209 } 210 while (newP->next != NULL) 211 { 212 if (newP->next->name.compare(n) == 0) 213 { 214 newPDelete = newP->next; 215 //这句代码还要好好考虑 216 //如果newPDelete->next为null 代码没问题么 217 newP->next = newPDelete->next; 218 delete newPDelete; 219 //方法结束 220 return; 221 } 222 newP = newP->next; 223 } 224 //链表节点中没有该name 225 cout << "the link nodes don't have you input name " << n << endl; 226 } 227 }