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

链表

这里只记录.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);
}
相关推荐
A~MasterYi5 小时前
深入理解 Microscaling (MX) 格式:从浮点基础到共享指数矩阵乘法
算法·矩阵
环黄金线HHJX.5 小时前
《Tuan(拼音字母)⇆团(Group)/&湍(Turbulence)/&双结构链路道/&文字、符号、语言/&源点设计、连接起:人类与自然+AICosmOS》
开发语言·人工智能·算法·编辑器
有时间要学习5 小时前
面试150——第七周
算法·面试·深度优先
AI科技星6 小时前
万能学习方法论的理论建构与多领域适配性研究(乖乖数学)
人工智能·学习·算法·机器学习·平面·数据挖掘
Ashore11_6 小时前
蓝桥杯16届Java研究生组
java·算法·蓝桥杯
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 76. 最小覆盖子串 | C++ 滑动窗口题解
c++·算法·leetcode
像素猎人6 小时前
蓝桥杯OJ2049蓝桥勇士【动态规划】【dp[n]不是符合题意的答案,只是以an结尾的子问题的答案】
c++·算法·蓝桥杯·动态规划·区间dp
羊小猪~~6 小时前
LLM--SFT简介
python·考研·算法·ai·大模型·llm·微调
广州灵眸科技有限公司6 小时前
瑞芯微(EASY EAI)RV1126B 人脸98关键点算法识别
开发语言·科技·嵌入式硬件·物联网·算法·php
篮子里的玫瑰6 小时前
FreeRTOS:信号量与互斥量在DMA串口发送中的实战剖析
stm32·单片机·嵌入式硬件·算法