单链表头插法c++实现
时间: 2023-11-08 20:06:54 浏览: 130
单链表头插法是一种在链表头部插入新节点的方法,它的实现方法如下:
1. 定义一个头节点,将其next指针指向NULL。
2. 对于每个要插入的节点,将其next指针指向头节点的next指针所指向的节点,然后将头节点的next指针指向该节点。
3. 插入完成后,头节点的next指针所指向的节点即为链表的第一个节点。
下面是单链表头插法的C++实现代码:
```
struct Node {
int data;
Node* next;
};
Node* create() {
Node* first = new Node;
first->next = NULL;
return first;
}
void insert(Node* first, int data) {
Node* node = new Node;
node->data = data;
node->next = first->next;
first->next = node;
}
```
其中,create函数用于创建头节点,insert函数用于插入新节点。使用时,先调用create函数创建头节点,然后调用insert函数插入新节点即可。
相关问题
单链表头插法逆置C++
单链表头部插入并逆序操作在C++中通常通过双指针法来实现。首先创建一个新的节点作为新的头结点,然后遍历原链表,将每个节点的数据插入到新链表的尾部,同时保持原链表的连接。以下是基本步骤:
1. 定义两个指针 `prev` 和 `current`,初始值都设为 NULL。
2. 创建一个临时头节点 `new_head`,并将其指向 NULL。
3. 遍历原链表:
a. 将当前节点 (`current`) 的数据赋值给 `temp`,然后移动 `current` 指向下一个节点。
b. 如果 `current` 还不是空节点,将 `current` 更新为 `prev`,然后将 `prev` 设置为 `temp`,因为我们要把 `temp` 插入到 `prev` 的后面。
4. 当遍历结束后,`prev` 就指向了原链表的最后一个节点,而 `new_head` 就是新的头节点。
C++伪代码示例:
```cpp
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* new_head = nullptr; // 新链表的头
while (current != nullptr) {
Node* temp = current->data; // 保存当前节点数据
current = current->next; // 移动current
if (prev != nullptr)
prev->next = temp; // 将temp插到prev后面
else
new_head = temp; // 如果prev为空,设置new_head
prev = temp; // prev移动到temp位置
}
return new_head;
}
```
单链表头插法逆置C++完整代码
单链表头部插入逆序的操作并不常见,因为通常我们更倾向于对整个链表进行反转,而不是只通过头插法。但是如果你确实需要这样做,你可以创建一个新的链表,将原链表的所有节点逐个添加到新链表的末尾。以下是C++的一种简单实现,假设有一个名为`Node`的链表节点结构:
```cpp
#include <iostream>
// 定义链表节点结构
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// 反转链表函数
Node* reverseList(Node* head) {
if (head == nullptr || head->next == nullptr) { // 如果链表为空或只有一个元素,直接返回
return head;
}
// 创建新的头结点
Node* newHead = new Node(0);
Node* current = head; // 初始化当前指针
Node* prev = newHead; // 初始化前驱指针
while (current != nullptr) { // 遍历原链表
Node* temp = current->next; // 保存下一个节点
current->next = prev; // 将当前节点连接到前驱
prev = current; // 更新前驱指针
current = temp; // 移动当前指针
}
return newHead->next; // 返回新链表的头节点
}
// 测试代码
int main() {
Node* originalList = new Node(1); // 假设原始链表为:1 -> NULL
originalList->next = new Node(2);
originalList->next->next = new Node(3);
Node* reversedList = reverseList(originalList);
// 输出反转后的链表
Node* temp = reversedList;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
return 0;
}
```
阅读全文
相关推荐
![](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://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)