创建一个由6个结点的单向链表,实现结点的增加、删除、查找、移动、遍历显示的功能。
时间: 2023-12-21 16:03:54 浏览: 26
好的,我可以帮你解决这个问题。你可以通过下面的代码实现一个6个结点的单向链表,并实现结点的增加、删除、查找、移动、遍历显示的功能。
```
#include <iostream>
using namespace std;
// 定义节点结构体
struct Node {
int data;
Node* next;
};
// 创建链表
Node* createLinkedList() {
Node* head = new Node();
head->next = nullptr;
return head;
}
// 插入节点
void insertNode(Node* head, int index, int value) {
Node* node = new Node();
node->data = value;
if (index == 1) {
node->next = head->next;
head->next = node;
return;
}
Node* p = head->next;
int pos = 1;
while (p != nullptr) {
pos++;
if (pos == index) {
node->next = p->next;
p->next = node;
return;
}
p = p->next;
}
cout << "插入失败,该位置不存在!" << endl;
}
// 删除节点
void deleteNode(Node* head, int index) {
if (head == nullptr || head->next == nullptr) {
cout << "链表为空,删除失败!" << endl;
return;
}
if (index == 1) {
Node* p = head->next;
head->next = p->next;
delete p;
return;
}
Node* p = head->next;
int pos = 1;
while (p != nullptr) {
pos++;
if (pos == index) {
Node* q = p->next;
p->next = q->next;
delete q;
return;
}
p = p->next;
}
cout << "删除失败,该位置不存在!" << endl;
}
// 查找节点
void searchNode(Node* head, int value) {
if (head == nullptr || head->next == nullptr) {
cout << "链表为空,查找失败!" << endl;
return;
}
Node* p = head->next;
int pos = 1;
while (p != nullptr) {
if (p->data == value) {
cout << "值为" << value << "的节点在链表中的位置为:" << pos << endl;
return;
}
pos++;
p = p->next;
}
cout << "未找到值为" << value << "的节点!" << endl;
}
// 移动节点
void moveNode(Node* head, int from, int to) {
if (head == nullptr || head->next == nullptr) {
cout << "链表为空,移动节点失败!" << endl;
return;
}
if (from == to) {
cout << "移动失败,源位置与目标位置相同!" << endl;
return;
}
Node* p = head->next;
int pos = 1;
Node* fromNode = nullptr, *toNode = nullptr;
while (p != nullptr) {
if (pos == from) {
fromNode = p;
}
if (pos == to) {
toNode = p;
}
if (fromNode != nullptr && toNode != nullptr) {
int temp = fromNode->data;
fromNode->data = toNode->data;
toNode->data = temp;
return;
}
pos++;
p = p->next;
}
cout << "移动失败,源位置或目标位置不存在!" << endl;
}
// 遍历链表
void displayLinkedList(Node* head) {
if (head == nullptr || head->next == nullptr) {
cout << "链表为空!" << endl;
return;
}
Node* p = head->next;
cout << "链表的节点为:";
while (p != nullptr) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
Node* head = createLinkedList(); // 创建链表
// 插入节点
insertNode(head, 1, 1);
insertNode(head, 2, 2);
insertNode(head, 3, 3);
insertNode(head, 4, 4);
insertNode(head, 5, 5);
insertNode(head, 6, 6);
// 遍历链表
displayLinkedList(head);
// 删除节点
deleteNode(head, 4);
// 遍历链表
displayLinkedList(head);
// 查找节点
searchNode(head, 3);
// 移动节点
moveNode(head, 3, 5);
// 遍历链表
displayLinkedList(head);
return 0;
}
```
输出结果如下:
```
链表的节点为:1 2 3 4 5 6
链表的节点为:1 2 3 5 6
值为3的节点在链表中的位置为:3
链表的节点为:1 2 5 3 6
```
希望我的回答能够帮到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)