定义单链表的数据类型将头插法和尾插法插入删除查找修改技术拟制输出等操作都定义成子函数的形式最后在主函数中调用它并将每一种操作前后的输出结果以查看每一种操作的效果
时间: 2024-04-29 19:23:27 浏览: 21
单链表的数据类型定义如下:
```c++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class LinkedList {
public:
LinkedList();
~LinkedList();
void insertFront(int val);
void insertEnd(int val);
void deleteNode(int val);
ListNode* searchNode(int val);
void modifyNode(int oldVal, int newVal);
void printList();
private:
ListNode* head;
};
```
其中,`ListNode` 表示单链表中的节点,包含一个整数值 `val` 和一个指向下一个节点的指针 `next`。
`LinkedList` 类表示单链表,包含一个头节点 `head`,定义了头插法、尾插法、删除节点、查找节点、修改节点和输出链表的操作。
头插法的实现如下:
```c++
void LinkedList::insertFront(int val) {
ListNode* newNode = new ListNode(val);
newNode->next = head->next;
head->next = newNode;
}
```
尾插法的实现如下:
```c++
void LinkedList::insertEnd(int val) {
ListNode* newNode = new ListNode(val);
ListNode* cur = head;
while (cur->next != nullptr) {
cur = cur->next;
}
cur->next = newNode;
}
```
删除节点的实现如下:
```c++
void LinkedList::deleteNode(int val) {
ListNode* cur = head;
while (cur->next != nullptr) {
if (cur->next->val == val) {
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
return;
}
cur = cur->next;
}
}
```
查找节点的实现如下:
```c++
ListNode* LinkedList::searchNode(int val) {
ListNode* cur = head->next;
while (cur != nullptr) {
if (cur->val == val) {
return cur;
}
cur = cur->next;
}
return nullptr;
}
```
修改节点的实现如下:
```c++
void LinkedList::modifyNode(int oldVal, int newVal) {
ListNode* cur = head->next;
while (cur != nullptr) {
if (cur->val == oldVal) {
cur->val = newVal;
return;
}
cur = cur->next;
}
}
```
输出链表的实现如下:
```c++
void LinkedList::printList() {
ListNode* cur = head->next;
while (cur != nullptr) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
```
在主函数中,可以按照需要调用各种操作函数,例如:
```c++
int main() {
LinkedList list;
list.insertEnd(1);
list.insertEnd(2);
list.insertEnd(3);
list.printList(); // 输出:1 2 3
list.insertFront(0);
list.printList(); // 输出:0 1 2 3
list.deleteNode(2);
list.printList(); // 输出:0 1 3
ListNode* node = list.searchNode(1);
if (node) {
cout << "Node found: " << node->val << endl;
} else {
cout << "Node not found" << endl;
}
list.modifyNode(3, 4);
list.printList(); // 输出:0 1 4
return 0;
}
```
输出结果为:
```
1 2 3
0 1 2 3
0 1 3
Node found: 1
0 1 4
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)