【数据结构和算法】--链表

链表

这里只记录.cpp的测试代码

cpp 复制代码
#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);
}
相关推荐
Word码13 分钟前
leetcode260.只出现一次的数字III
算法
MM_MS25 分钟前
Halcon图像采集助手、ROI操作和画图、ROI实现区域与轮廓之间的相互转换、区域的交集差集取反
图像处理·人工智能·数码相机·算法·目标检测·计算机视觉·视觉检测
wWYy.30 分钟前
详解redis(8):数据结构Hash
数据结构·redis·哈希算法
智者知已应修善业41 分钟前
【输出一个N*N的01矩阵,表示最后的汉字点阵图】2024-10-22
c语言·数据结构·c++·经验分享·笔记·算法·矩阵
uesowys1 小时前
华为OD算法开发指导-二级索引
数据结构·算法·华为od
a程序小傲1 小时前
高并发下如何防止重复下单?
java·开发语言·算法·面试·职场和发展·状态模式
uoKent1 小时前
c++中的封装、继承与多态
开发语言·c++·算法
爱喝可乐的老王1 小时前
机器学习监督学习模型--朴素贝叶斯
人工智能·算法·机器学习
啊阿狸不会拉杆2 小时前
《机器学习》完结篇-总结
人工智能·算法·机器学习·计算机视觉·ai·集成学习·ml
Sheep Shaun2 小时前
C++11核心特性详解:从右值引用到现代C++编程
开发语言·数据结构·c++·算法