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

链表

这里只记录.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);
}

相关推荐

  1. 数据结构算法】--

    2024-05-09 12:48:05       36 阅读
  2. 数据结构算法的简单实现

    2024-05-09 12:48:05       65 阅读
  3. 数据结构算法构造相关代码理解

    2024-05-09 12:48:05       44 阅读
  4. 算法3&4_数据结构&数组

    2024-05-09 12:48:05       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 12:48:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 12:48:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 12:48:05       87 阅读
  4. Python语言-面向对象

    2024-05-09 12:48:05       96 阅读

热门阅读

  1. websocket

    websocket

    2024-05-09 12:48:05      30 阅读
  2. vue触发原生form提交到指定action地址

    2024-05-09 12:48:05       31 阅读
  3. c++中constexpr的一个用法——在泛型编程中的作用

    2024-05-09 12:48:05       33 阅读
  4. docker 部署并运行一个微服务

    2024-05-09 12:48:05       34 阅读
  5. Stylus:深入解析与实战引入

    2024-05-09 12:48:05       37 阅读
  6. 【Leetcode 每日一题】26. 删除有序数组中的重复项

    2024-05-09 12:48:05       32 阅读
  7. qt day 3

    qt day 3

    2024-05-09 12:48:05      30 阅读
  8. Android中gradle.properties 和 gradle-wrapper.properties 作用

    2024-05-09 12:48:05       36 阅读