利用C++实现单链表的头插法
时间: 2023-03-11 11:34:15 浏览: 80
可以使用以下代码来提取 CSV 文件的前十行数据并绘制柱状图:
```
import pandas as pd
import matplotlib.pyplot as plt
# 读取 CSV 文件
df = p
相关问题
C++实现单链表的插入删除
单链表的插入删除可以通过以下代码实现:
```c++
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// 在链表头插入节点
ListNode* insertAtHead(ListNode* head, int val) {
ListNode* node = new ListNode(val);
node->next = head;
return node;
}
// 在链表尾插入节点
ListNode* insertAtTail(ListNode* head, int val) {
ListNode* node = new ListNode(val);
if (head == NULL) {
head = node;
} else {
ListNode* cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = node;
}
return head;
}
// 在链表中删除节点
ListNode* deleteNode(ListNode* head, int val) {
if (head == NULL) {
return NULL;
} else if (head->val == val) {
ListNode* newHead = head->next;
delete head;
return newHead;
} else {
ListNode* cur = head;
while (cur->next != NULL && cur->next->val != val) {
cur = cur->next;
}
if (cur->next != NULL) {
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
}
return head;
}
}
// 打印链表
void printList(ListNode* head) {
ListNode* cur = head;
while (cur != NULL) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
int main() {
ListNode* head = NULL;
// 在链表头插入节点
head = insertAtHead(head, 1);
head = insertAtHead(head, 2);
head = insertAtHead(head, 3);
printList(head); // 3 2 1
// 在链表尾插入节点
head = insertAtTail(head, 4);
head = insertAtTail(head, 5);
head = insertAtTail(head, 6);
printList(head); // 3 2 1 4 5 6
// 在链表中删除节点
head = deleteNode(head, 3);
head = deleteNode(head, 6);
printList(head); // 2 1 4 5
return 0;
}
```
c++单链表的头插法和尾插法
单链表是一种常见的数据结构,它由节点组成,每个节点包含一个数据项和一个指向下一个节点的指针。
头插法和尾插法是在单链表中插入新节点的两种常见方法。
头插法是将新节点插入到链表的头部,即作为新的第一个节点。具体步骤如下:
1. 创建一个新节点,将新节点的数据项赋值为要插入的数据。
2. 将新节点的指针指向当前链表的第一个节点。
3. 更新链表的头指针,使其指向新节点。
尾插法是将新节点插入到链表的尾部,即作为新的最后一个节点。具体步骤如下:
1. 创建一个新节点,将新节点的数据项赋值为要插入的数据。
2. 如果链表为空,则将新节点作为第一个节点。
3. 否则,从链表的头节点开始,沿着链表的指针找到最后一个节点。
4. 将最后一个节点的指针指向新节点,使其成为最后一个节点。
头插法和尾插法的区别在于插入的位置不同。头插法适用于需要快速插入并获取最新数据的场景,例如日志记录;尾插法适用于需要按顺序插入数据的场景,例如创建有序链表。
需要注意的是,使用头插法或尾插法插入节点时,要确保更新链表的头指针和尾指针,以确保链表的完整性。同时,还要考虑边界条件,例如链表为空时的插入操作。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)