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

链表

这里只记录.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);
}
相关推荐
老马啸西风5 分钟前
成熟企业级技术平台 MVE-010-app 管理平台
人工智能·深度学习·算法·职场和发展
lzh_2001101217 分钟前
树状数组理解
算法
历程里程碑36 分钟前
C++ 6 :string类:高效处理字符串的秘密
c语言·开发语言·数据结构·c++·笔记·算法·排序算法
蓝色汪洋38 分钟前
luogu迷宫寻路
算法
xu_yule38 分钟前
算法基础-(数据结构)
数据结构
玩转数据库管理工具FOR DBLENS40 分钟前
DBLens:开启数据库管理新纪元——永久免费,智能高效的国产化开发利器
数据结构·数据库·测试工具·数据库开发
木头左1 小时前
自适应门限动态调整算法在量化交易策略中的应用
算法
deepdata_cn1 小时前
非线性规划(NLP)算法
算法
TL滕1 小时前
从0开始学算法——第十五天(滑动窗口)
笔记·学习·算法
@小码农1 小时前
2025年全国青少年信息素养大赛 Gandi编程 小低组初赛真题
数据结构·人工智能·算法·蓝桥杯