链表——增删查改
题目
题目解析
一个很基础的关于链表的增删查改题目,学过数据结构的应该都蛮熟的吧(^^),为了更好的完成题目,我们可以将其变为一个菜单,直接使用。
代码
增
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | bool Push(ListNode** head, int num) { ListNode* new_node = new ListNode{ num, NULL }; if (!new_node) { return false ; } if (*head == NULL) { *head = new_node; } else { ListNode* current = *head; while (current->next_node) { current = current->next_node; } current->next_node = new_node; } return true ; } |
删
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | bool Pop(ListNode** head, int num) { bool found = false ; ListNode dummy{ num,NULL }; // 创建一个dummy节点,其值设置为0 dummy.next_node = *head; ListNode* current = &dummy; while (current->next_node != NULL) { if (current->next_node->num == num) { ListNode* temp = current->next_node; current->next_node = current->next_node->next_node; delete temp; found = true ; break ; // 找到后退出循环 } current = current->next_node; } *head = dummy.next_node; // 更新头节点 return found; } |
查
1 2 3 4 5 6 7 8 9 10 11 12 13 | ListNode* Find(ListNode *head, int num) { ListNode* current = head; while (current) { if (current->num == num) { return current; } current = current->next_node; } return NULL; } |
改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | bool Updata(ListNode** head, int num1, int num2) { ListNode* current = *head; while (current) { if (current->num == num1) { current->num = num2; return true ; } current = current->next_node; } return false ; } |
总代码
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | /*2024-3-15 rou*/ #include<iostream> #include<string> using namespace std; struct ListNode { int num; struct ListNode *next_node; }; //增 bool Push(ListNode** head, int num) { ListNode* new_node = new ListNode{ num, NULL }; if (!new_node) { return false ; } if (*head == NULL) { *head = new_node; } else { ListNode* current = *head; while (current->next_node) { current = current->next_node; } current->next_node = new_node; } return true ; } //删 bool Pop(ListNode** head, int num) { bool found = false ; ListNode dummy{ num,NULL }; // 创建一个dummy节点,其值设置为0 dummy.next_node = *head; ListNode* current = &dummy; while (current->next_node != NULL) { if (current->next_node->num == num) { ListNode* temp = current->next_node; current->next_node = current->next_node->next_node; delete temp; found = true ; break ; // 找到后退出循环 } current = current->next_node; } *head = dummy.next_node; // 更新头节点 return found; } //查 ListNode* Find(ListNode *head, int num) { ListNode* current = head; while (current) { if (current->num == num) { return current; } current = current->next_node; } return NULL; } //改 bool Updata(ListNode** head, int num1, int num2) { ListNode* current = *head; while (current) { if (current->num == num1) { current->num = num2; return true ; } current = current->next_node; } return false ; } //打印链表 bool PrintList(ListNode** head) { cout << "是否需要打印链表?(1.是 ,2.不是)" << endl; int yes_or_no; cin >> yes_or_no; if (yes_or_no == 1) { ListNode* current = *head; while (current) { cout << current->num << " " ; current=current->next_node; } cout << endl; return true ; } else if (yes_or_no == 2) { return false ; } else { cout << "输入错误,取消打印" << endl; return false ; } } bool Clear(ListNode** head) { ListNode* temp; while (*head) { temp = *head; *head = (*head)->next_node; delete temp; } return true ; } int main() { ListNode* head = NULL; bool keepRunning = true ; cout << "请输入要插入的数据数量(数量在100以内):" << endl; int count; cin >> count; if (count > 100 || count < 0) { cout << "输入的数据数量不在有效范围内,请重新输入:" << endl; return 1; } for ( int i = 0; i < count; ++i) { int num; cout << "请输入第 " << i + 1 << " 个数据:" << endl; cin >> num; if (!Push(&head, num)) { cout << "内存分配失败,无法插入数据。" << endl; Clear(&head); return 1; } } PrintList(&head); while (keepRunning) { int choice; do { cout << "请选择要执行的操作:" << endl; cout << "1.增加数据" << endl; cout << "2.删除数据" << endl; cout << "3.查询数据" << endl; cout << "4.修改数据" << endl; cout << "5.退出程序" << endl; cin >> choice; switch (choice) { case 1: { cout << "请输入要增加的数据数量:" << endl; int addCount; cin >> addCount; for ( int i = 0; i < addCount; ++i) { int num; cout << "请输入要添加的数据:" << endl; cin >> num; if (!Push(&head, num)) { cout << "内存分配失败,无法插入数据" << endl; break ; } } break ; } case 2: { cout << "请输入要删除的数据:" << endl; int num; cin >> num; if (Pop(&head, num)) { cout << "删除数据" << num << "成功" << endl; } else { cout << "删除数据" << num << "失败" << endl; } break ; } case 3: { cout << "请输入要查询的数据:" << endl; int num; cin >> num; ListNode* found = Find(head, num); if (found) { cout << "找到数据 " << num << endl; } else { cout << "未找到数据 " << num << endl; } break ; } case 4: { cout << "请输入要将数据num1修改为数据num2的数值:" << endl; int num1, num2; cout << "num1:" ; cin >> num1; cout << endl << "num2:" ; cin >> num2; if (Updata(&head, num1, num2)) { cout << "数据 " << num1 << " 修改为 " << num2 << " 成功" << endl; } else { cout << "数据 " << num1 << " 未找到,修改失败" << endl; } break ; } case 5: cout << "退出程序。" << endl; keepRunning = false ; break ; default : cout << "无效的选择,请重新选择" << endl; break ; } // 打印链表 PrintList(&head); } while (choice != 5); } // 清除链表 Clear(&head); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】