C++实现单向链表(1)

需要在你的主程序中添加

#include "ListNode.cpp"

ListNode.h

#pragma once
#include <iostream>
using namespace std;


template <typename T>
class ListNode
{
private:
	T data;
	ListNode<T>* link;

public:
	ListNode(T theData, ListNode<T>* theLink)
	{
		data = theData;
		link = theLink;
	}

	void setLink(ListNode<T>* theLink) { link = theLink; }

	T getData() { return data; }
	ListNode<T>* getLink() { return link; }
};


//获取链表的长度
template <typename T>
int Length(ListNode<T>* theHead);


//按标号查找节点
template <typename T>
ListNode<T>* LookNode(ListNode<T>* theHead, int pos);


//在任意位置添加节点
template <typename T>
void AddNode(ListNode<T>*& theHead, int pos, T theData);


//删除任意位置的节点
template <typename T>
void DelNode(ListNode<T>*& theHead, int pos);


//清空链表
template <typename T>
void EmptyList(ListNode<T>*& theHead);

ListNode.cpp

#include "ListNode.h"


//-----获取链表的长度-----------------------------------//
template <typename T>
int Length(ListNode<T>* theHead)
{
	if (theHead == nullptr)
	{
		//头指针为空的话返回0
		return 0;
	}
	else
	{
		int count = 0;
		ListNode<T>* theNode = theHead;
		while (theNode != nullptr)
		{
			count++;
			theNode = theNode->getLink(); 
		}
		return count;  //返回节点个数
	}
}


//-----按标号查找节点-----------------------------------//
template <typename T>
ListNode<T>* LookNode(ListNode<T>* theHead, int pos)
{
	if (theHead == nullptr)
	{
		cout << "输入错误,头指针不能为空!" << endl;
		return nullptr;
	}
	else
	{
		if (pos == 0)  //查找头节点
		{
			return theHead;
		}
		else if (pos > 0)  //非头节点的情况
		{
			if (pos < Length(theHead))  //标号不能超出链表实际长度
			{
				ListNode<T>* theNode = theHead;
				for (int i = 0; i < pos; i++)
				{
					theNode = theNode->getLink();
				}
				return theNode;
			}
			else
			{
				cout << "输入错误,标号超出链表实际长度!" << endl;
				return nullptr;
			}
		}
		else
		{
			cout << "输入错误,标号不能为负数!" << endl;
			return nullptr;
		}
	}
}


//-----在任意位置添加节点-------------------------------//
template <typename T>
void AddNode(ListNode<T>*& theHead, int pos, T theData)
{
	if (theHead == nullptr)  //链表为空的情况
	{
		if (pos == 0)
		{
			theHead = new ListNode<T>(theData, nullptr);
		}
		else if (pos > 0)
		{
			cout << "输入错误,标号超出链表实际长度!" << endl;
		}
		else
		{
			cout << "输入错误,标号不能为负数!" << endl;
		}
	}
	else  //链表不为空的情况
	{
		if (pos == 0)  //在链表头部插入节点
		{
			theHead = new ListNode<T>(theData, theHead);
		}
		else if (pos > 0)
		{
			if (pos < Length(theHead))  //在链表中间插入节点
			{
				//找到插入点的前一个节点
				ListNode<T>* theNode = LookNode(theHead, pos - 1);
				theNode->setLink(new ListNode<T>(theData, theNode->getLink()));
			}
			else if (pos == Length(theHead))  //在链表尾部插入节点
			{
				//找到尾部节点
				ListNode<T>* theNode = LookNode(theHead, pos - 1);
				theNode->setLink(new ListNode<T>(theData, nullptr));
			}
			else
			{
				cout << "输入错误,标号超出链表实际长度!" << endl;
			}
		}
		else
		{
			cout << "输入错误,标号不能为负数!" << endl;
		}
	}
}


//-----删除任意位置的节点-------------------------------//
template <typename T>
void DelNode(ListNode<T>*& theHead, int pos)
{
	if (theHead == nullptr)
	{
		cout << "输入错误,头指针不能为空!" << endl;
	}
	else  //链表不为空的情况
	{
		if (pos == 0)  //删除头部节点的情况
		{
			if (Length(theHead) == 1)  //若链表只有一个节点
			{
				delete theHead;
				theHead = nullptr;
			}
			else  //链表有多个节点的情况
			{
				ListNode<T>* theNode = theHead;
				theHead = theHead->getLink();
				delete theNode;
			}
		}
		else if (pos > 0)
		{
			if (pos < Length(theHead) - 1)  //删除中间节点的情况
			{
				//找到要删除的节点
				ListNode<T>* theCurrent = LookNode(theHead, pos);
				//找到要删除的节点的前一个节点
				ListNode<T>* thePrev = LookNode(theHead, pos - 1);
				thePrev->setLink(theCurrent->getLink());
				delete theCurrent;
			}
			else if (pos == Length(theHead) - 1)  //删除尾部节点的情况
			{
				//找到尾部节点
				ListNode<T>* theLast = LookNode(theHead, pos);
				//找到尾部节点的前一个节点
				ListNode<T>* thePrev = LookNode(theHead, pos - 1);
				thePrev->setLink(nullptr);
				delete theLast;
			}
			else
			{
				cout << "输入错误,标号超出链表实际长度!" << endl;
			}
		}
		else
		{
			cout << "输入错误,标号不能为负数!" << endl;
		}
	}
}


//-----清空链表-----------------------------------------//
template <typename T>
void EmptyList(ListNode<T>*& theHead)
{
	int length = Length(theHead);
	for (int i = 0; i < length; i++)
	{
		DelNode(theHead, 0);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值