[数据结构与算法] - 链表
最编程
2024-05-05 22:24:30
...
#include "MyList.hpp"
#include <iostream>
using namespace std;
void printList(pNode headNode)
{
cout << "*** printList ****" << endl;
pNode tempNode, curNode;
if (nullptr == headNode)
{
cout << "*** printList error ****" << endl;
return ;
}
curNode = headNode->next;
cout << "*** [printList]: ";
while (curNode)
{
cout << curNode->data << " , ";
tempNode = curNode;
curNode = tempNode->next;
}
cout << endl;
return ;
}
pNode initList(int num)
{
cout << "*** InitList ****" << endl;
pNode headNode, tempNode, curNode;
headNode = (pNode)malloc(sizeof(Node));
if (nullptr == headNode)
{
cout << "*** init error ****" << endl;
return nullptr;
}
headNode->next = nullptr;
curNode = headNode;
for (int i = 1; i <= num; i++)
{
tempNode = (pNode)malloc(sizeof(Node));
if (nullptr == tempNode)
{
cout << "*** init error ****" << endl;
return nullptr;
}
tempNode->data = i;
tempNode->next = nullptr;
curNode->next = tempNode;
curNode = tempNode;
tempNode = nullptr;
}
return headNode;
}
pNode searchList(pNode headNode, int data)
{
cout << "*** searchList ****" << endl;
pNode tempNode, curNode;
if (nullptr == headNode)
{
cout << "[searchList] error." << endl;
return nullptr;
}
curNode = headNode->next;
while (curNode)
{
if (data == curNode->data)
{
cout << "[searchList] suc. data: " << data << endl;
return curNode;
}
tempNode = curNode;
curNode = tempNode->next;
}
cout << "[searchList] not find. data: " << data << endl;
return nullptr;
}
void reverseList(pNode node)
{
cout << "*** reverseList ****" << endl;
pNode curNode, curNextNode;
if (nullptr == node)
{
cout << "*** reverseList error ****" << endl;
return;
}
curNode = node->next;
node->next = nullptr;
while (curNode)
{
curNextNode = curNode->next;
curNode->next = node->next;
node->next = curNode;
curNode = curNextNode;
}
cout << "*** reverseList ok ****" << endl;
return;
}
void testList(void)
{
cout << "*** Test_List ****" << endl;
pNode headNode = initList(5);
if (nullptr == headNode)
{
cout << "*** InitList error ****" << endl;
}
printList(headNode);
pNode node = searchList(headNode, 2);
reverseList(node);
printList(headNode);
}
推荐阅读
-
[数据结构与算法] - 链表
-
数据结构与算法之美 | 字符串匹配算法原理详解(哈希、KMP、BM、星期日)
-
字符串匹配算法:KMP 与 BM
-
利用链表数据结构优化页面渲染
-
[数据结构] 算法的效率(时间复杂性和空间复杂性) - V.oj 习题的复杂性
-
二叉树, 329. 矩阵中的最长递增路径, 备忘递归表填充, [73] [算法分析与设计] 516.
-
[算法] [计算机算法设计与分析笔记(第 5 版)] 第 1 章:算法概述
-
大公司使用的 12 种切分算法,一口搞定库与表的分割
-
基于 YOLOv7 算法(PyTorch+Pyside6+YOLOv7)的高精度实时深海鱼类目标检测与识别系统
-
<算法入门> 高级数据结构--构建、添加、删除和检查 B 树(平衡树)之我见