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

链表

这里只记录.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);
}
相关推荐
Inverse16216 分钟前
C语言_自定义类型:结构体
c语言·开发语言·算法
Musennn31 分钟前
102. 二叉树的层序遍历详解:队列操作与层级分组的核心逻辑
java·数据结构·算法·leetcode
越来越无动于衷37 分钟前
java数组题(5)
java·算法
理论最高的吻39 分钟前
77. 组合【 力扣(LeetCode) 】
c++·算法·leetcode·深度优先·剪枝·回溯法
学习中的码虫39 分钟前
c 中的哈希表
数据结构·哈希算法·散列表
强盛小灵通专卖员4 小时前
分类分割详细指标说明
人工智能·深度学习·算法·机器学习
IT猿手7 小时前
基于强化学习 Q-learning 算法求解城市场景下无人机三维路径规划研究,提供完整MATLAB代码
神经网络·算法·matlab·人机交互·无人机·强化学习·无人机三维路径规划
万能程序员-传康Kk10 小时前
旅游推荐数据分析可视化系统算法
算法·数据分析·旅游
PXM的算法星球10 小时前
【并发编程基石】CAS无锁算法详解:原理、实现与应用场景
算法
ll77881110 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法