题目
题目解析
一个很基础的关于链表的增删查改题目,学过数据结构的应该都蛮熟的吧(^^),为了更好的完成题目,我们可以将其变为一个菜单,直接使用。
代码
增
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; }
总代码
/*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; }
标签:node,current,head,ListNode,next,链表,num,查改,增删 From: https://www.cnblogs.com/hcrzhi/p/18092260