1.以函数形式实现单链表的建立、插入、删除、查找运算的思路结构。 2.以函数形式实现循环单链表的建立、插入、删除、查找运算的思路结构。
时间: 2023-12-13 08:05:48 浏览: 34
1. 单链表的建立、插入、删除、查找运算思路结构:
定义单链表节点结构体:
```
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
1.1 单链表的建立
```
ListNode* createList(vector<int>& nums) {
ListNode* head = new ListNode(0);
ListNode* cur = head;
for (int i = 0; i < nums.size(); i++) {
ListNode* newNode = new ListNode(nums[i]);
cur->next = newNode;
cur = newNode;
}
return head->next;
}
```
1.2 单链表的插入
```
void insertNode(ListNode* head, int val) {
ListNode* newNode = new ListNode(val);
newNode->next = head->next;
head->next = newNode;
}
```
1.3 单链表的删除
```
void deleteNode(ListNode* head, int val) {
ListNode* cur = head;
while (cur->next != NULL) {
if (cur->next->val == val) {
ListNode* temp = cur->next;
cur->next = temp->next;
delete temp;
break;
}
cur = cur->next;
}
}
```
1.4 单链表的查找
```
ListNode* searchNode(ListNode* head, int val) {
ListNode* cur = head->next;
while (cur != NULL) {
if (cur->val == val) {
return cur;
}
cur = cur->next;
}
return NULL;
}
```
2. 循环单链表的建立、插入、删除、查找运算思路结构:
定义循环单链表节点结构体:
```
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
2.1 循环单链表的建立
```
ListNode* createList(vector<int>& nums) {
ListNode* head = new ListNode(0);
ListNode* cur = head;
for (int i = 0; i < nums.size(); i++) {
ListNode* newNode = new ListNode(nums[i]);
cur->next = newNode;
cur = newNode;
}
cur->next = head;
return head->next;
}
```
2.2 循环单链表的插入
```
void insertNode(ListNode* head, int val) {
ListNode* newNode = new ListNode(val);
ListNode* cur = head->next;
while (cur->next != head) {
cur = cur->next;
}
newNode->next = head;
cur->next = newNode;
head = newNode;
}
```
2.3 循环单链表的删除
```
void deleteNode(ListNode* head, int val) {
ListNode* cur = head;
while (cur->next != head) {
if (cur->next->val == val) {
ListNode* temp = cur->next;
cur->next = temp->next;
delete temp;
break;
}
cur = cur->next;
}
}
```
2.4 循环单链表的查找
```
ListNode* searchNode(ListNode* head, int val) {
ListNode* cur = head->next;
while (cur != head) {
if (cur->val == val) {
return cur;
}
cur = cur->next;
}
return NULL;
}
```
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)