单链表的插入和删除示意图
时间: 2023-11-06 10:06:32 浏览: 46
单链表的插入示意图如下:
初始状态 插入结点
┌───┐ ┌───┐ ┌───┐
head→│ │→node1 head→│ │→node1→│new│→node2
└───┘ └───┘ └───┘
单链表的删除示意图如下:
初始状态 删除结点
┌───┐ ┌───┐ ┌───┐
head→│ │→node1 head→│ │─────────│node2│
└───┘ └───┘ └───┘
相关问题
单链表插入删除和访问元素代码c++
以下是单链表的插入、删除和访问元素的 C++ 代码:
```c++
#include <iostream>
using namespace std;
// 定义链表节点
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// 插入节点
void insertNode(ListNode *&head, int val) {
ListNode *newNode = new ListNode(val);
if (head == NULL) {
head = newNode;
} else {
ListNode *cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
}
}
// 删除节点
void deleteNode(ListNode *&head, int val) {
if (head == NULL) {
return;
}
if (head->val == val) {
ListNode *tmp = head;
head = head->next;
delete tmp;
} else {
ListNode *cur = head;
while (cur->next != NULL && cur->next->val != val) {
cur = cur->next;
}
if (cur->next != NULL) {
ListNode *tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}
}
}
// 访问节点
int getVal(ListNode *head, int index) {
if (head == NULL) {
return -1;
}
ListNode *cur = head;
while (index > 0 && cur->next != NULL) {
cur = cur->next;
index--;
}
if (index == 0) {
return cur->val;
} else {
return -1;
}
}
int main() {
ListNode *head = NULL;
// 插入节点
insertNode(head, 1);
insertNode(head, 2);
insertNode(head, 3);
insertNode(head, 4);
insertNode(head, 5);
// 删除节点
deleteNode(head, 3);
deleteNode(head, 5);
// 访问节点
cout << getVal(head, 0) << endl; // 输出 1
cout << getVal(head, 1) << endl; // 输出 2
cout << getVal(head, 2) << endl; // 输出 4
return 0;
}
```
单链表的插入和删除c++
单链表的插入和删除操作是链表的基本操作之一。在C语言中,可以通过以下代码实现单链表的插入和删除:
插入操作的代码如下:
```c
void InsertNode(struct Node **head, int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
```
删除操作的代码如下:
```c
void DeleteNode(struct Node **head, int data) {
if (*head == NULL) {
return;
}
struct Node *current = *head;
struct Node *prev = NULL;
if (current->data == data) {
*head = current->next;
free(current);
return;
}
while (current != NULL && current->data != data) {
prev = current;
current = current->next;
}
if (current == NULL) {
return;
}
prev->next = current->next;
free(current);
}
```
以上代码分别实现了单链表的插入和删除操作。插入操作将新节点插入到链表的末尾,如果链表为空,则将新节点作为头节点;删除操作根据给定的数据值删除相应的节点,如果找到匹配的节点,则将其从链表中移除。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)