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

链表

这里只记录.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);
}
相关推荐
重庆小透明7 分钟前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
desssq26 分钟前
力扣:70. 爬楼梯
算法·leetcode·职场和发展
clock的时钟1 小时前
暑期数据结构第一天
数据结构·算法
小小小小王王王1 小时前
求猪肉价格最大值
数据结构·c++·算法
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
BIYing_Aurora2 小时前
【IPMV】图像处理与机器视觉:Lec13 Robust Estimation with RANSAC
图像处理·人工智能·算法·计算机视觉
martian6653 小时前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
孟大本事要学习4 小时前
算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
算法
SuperW4 小时前
数据结构——队列
数据结构
??tobenewyorker4 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode