根据你提供的代码,当输入的插入位置不合法时并不会输出字符串”error”,这是因为你只在函数insertNode()
中加了一个cout << "error" << endl;
语句,但没有处理错误情况的返回。以下是修改后的代码:
#include <iostream>
using namespace std;
// 单链表节点定义
struct ListNode {
int data;
ListNode* next;
};
// 尾插法创建单链表
ListNode* createList(int n) {
ListNode* head = new ListNode();
head->next = nullptr;
ListNode* tail = head;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
ListNode* newNode = new ListNode();
newNode->data = num;
newNode->next = nullptr;
tail->next = newNode; // 新节点添加到链表末尾
tail = newNode; // 更新尾节点
}
return head;
}
// 打印单链表
void printList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
cout << "empty" << endl;
return;
}
ListNode* cur = head->next;
while (cur != nullptr) {
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
// 获取链表长度
int getListLength(ListNode* head) {
int length = 0;
ListNode* cur = head->next;
while (cur != nullptr) {
length++;
cur = cur->next;
}
return length;
}
// 根据位置插入元素到链表中
void insertNode(ListNode*& head, int position, int targetPosition) {
if (position <= 0 || position > getListLength(head)) { // 检查位置合法性
cout << "error" << endl; // 输出错误提示并返回
return;
}
if (position == targetPosition) { // 若位置相同,链表不会发生改变
printList(head);
return;
}
ListNode* prev = nullptr;
ListNode* cur = head->next;
for (int i = 1; i < position; ++i) {
prev = cur;
cur = cur->next;
}
if (prev != nullptr) {
prev->next = cur->next; // 移除当前节点
ListNode* insertPosPrev = nullptr;
ListNode* insertPosCur = head->next;
for (int i = 1; i < targetPosition; ++i) { // 找到插入位置的前一个节点
insertPosPrev = insertPosCur;
insertPosCur = insertPosCur->next;
}
if (insertPosPrev != nullptr)
insertPosPrev->next = cur;
cur->next = insertPosCur;
if (insertPosCur == head->next)
head->next = cur;
}
printList(head); // 输出移动后的链表
}
int main() {
int n, t;
cin >> n;
ListNode* listHead = createList(n);
printList(listHead);
cin >> t;
for (int i = 0; i < t; ++i) {
int pos, targetPos;
cin >> pos >> targetPos;
insertNode(listHead, pos, targetPos);
}
delete listHead;
return 0;
}
在insertNode()
函数中,当位置不合法时,我们使用return;
语句提前结束函数的执行,并没有输出字符串”error”。要修改这个问题,可以将cout << "error" << endl;
语句改为返回一个特定的值,然后在主函数中判断该值来输出”error”。
以下是修改后的代码:
#include <iostream>
using namespace std;
// 单链表节点定义
struct ListNode {
int data;
ListNode* next;
};
// 尾插法创建单链表
ListNode* createList(int n) {
ListNode* head = new ListNode();
head->next = nullptr;
ListNode* tail = head;
for (int i = 0; i < n; ++i) {
int num;
cin >> num;
ListNode* newNode = new ListNode();
newNode->data = num;
newNode->next = nullptr;
tail->next = newNode; // 新节点添加到链表末尾
tail = newNode; // 更新尾节点
}
return head;
}
// 打印单链表
void printList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
cout << "empty" << endl;
return;
}
ListNode* cur = head->next;
while (cur != nullptr) {
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
// 获取链表长度
int getListLength(ListNode* head) {
int length = 0;
ListNode* cur = head->next;
while (cur != nullptr) {
length++;
cur = cur->next;
}
return length;
}
// 根据位置插入元素到链表中
bool insertNode(ListNode*& head, int position, int targetPosition) {
if (position <= 0 || position > getListLength(head)) { // 检查位置合法性
return false; // 返回错误提示
}
if (position == targetPosition) { // 若位置相同,链表不会发生改变
printList(head);
return true;
}
ListNode* prev = nullptr;
ListNode* cur = head->next;
for (int i = 1; i < position; ++i) {
prev = cur;
cur = cur->next;
}
if (prev != nullptr) {
prev->next = cur->next; // 移除当前节点
ListNode* insertPosPrev = nullptr;
ListNode* insertPosCur = head->next;
for (int i = 1; i < targetPosition; ++i) { // 找到插入位置的前一个节点
insertPosPrev = insertPosCur;
insertPosCur = insertPosCur->next;
}
if (insertPosPrev != nullptr)
insertPosPrev->next = cur;
cur->next = insertPosCur;
if (insertPosCur == head->next)
head->next = cur;
}
printList(head); // 输出移动后的链表
return true;
}
int main() {
int n, t;
cin >> n;
ListNode* listHead = createList(n);
printList(listHead);
cin >> t;
for (int i = 0; i < t; ++i) {
int pos, targetPos;
cin >> pos >> targetPos;
if (!insertNode(listHead, pos, targetPos)) { // 插入失败时输出错误提示
cout << "error" << endl;
}
}
delete listHead;
return 0;
}
在主函数的for
循环中,使用 if (!insertNode(listHead, pos, targetPos))
来判断插入是否成功,并在插入失败时输出字符串”error”。
内容由零声教学AI助手提供,问题来源于学员提问