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

这篇博客详细介绍了如何使用C++模板实现单向链表,包括构造链表、获取链表长度、按序号查找节点、在任意位置添加和删除节点以及清空链表等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

using std::cout;
using std::endl;

template <typename T>
struct ListNode
{
	T data;
	ListNode<T>* link;
};

template <typename T>
class List
{
private:
	ListNode<T>* head;

public:
	List();

	int len();                     //获取链表的长度
	ListNode<T>* get(int pos);     //按序号查找节点
	void add(int pos, T theData);  //任意位置添加节点
	void del(int pos);             //删除任意位置的节点
	void clc();                    //清空链表
};
#include "List.h"

template <typename T>
List<T>::List() { head = nullptr; }

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

//-----按序号查找节点-----------------------------------//
template <typename T>
ListNode<T>* List<T>::get(int pos)
{
	if (head == nullptr)
	{
		cout << "链表为空!" << endl;
		return nullptr;
	}
	else
	{
		if (pos >= 0)
		{
			if (pos < len())  //序号不能超出链表实际长度
			{
				ListNode<T>* node = head;
				for (int i = 0; i < pos; i++)
				{
					node = node->link;
				}
				return node;
			}
			else
			{
				cout << "序号超出范围!" << endl;
				return nullptr;
			}
		}
		else
		{
			cout << "序号不能为负数!" << endl;
			return nullptr;
		}
	}
}

//-----在任意位置添加节点-------------------------------//
template <typename T>
void List<T>::add(int pos, T theData)
{
	if (head == nullptr)  //链表为空的情况
	{
		if (pos == 0)
			head = new ListNode<T>{ theData, nullptr };
		else if (pos > 0)
			cout << "序号超出范围!" << endl;
		else
			cout << "序号不能为负数!" << endl;
	}
	else  //链表不为空的情况
	{
		if (pos == 0)  //在链表头部插入节点
			head = new ListNode<T>{ theData, head };
		else if (pos > 0)
		{
			if (pos <= len())  //在链表中间或尾部插入节点
			{
				ListNode<T>* node = get(pos - 1);  //找到插入点的前一个节点
				node->link = new ListNode<T>{ theData, node->link };
			}
			else
				cout << "序号超出范围!" << endl;
		}
		else
			cout << "序号不能为负数!" << endl;
	}
}

//-----删除任意位置的节点-------------------------------//
template <typename T>
void List<T>::del(int pos)
{
	if (head == nullptr)
		cout << "链表为空!" << endl;
	else  //链表不为空的情况
	{
		if (pos == 0)  //删除头部节点的情况
		{
			ListNode<T>* node = head;
			head = head->link;
			delete node;
		}
		else if (pos > 0)
		{
			if (pos < len())  //删除中间或尾部节点的情况
			{
				ListNode<T>* current = get(pos);  //找到要删除的节点
				ListNode<T>* last = get(pos - 1);  //找到要删除的节点的前一个节点
				last->link = current->link;
				delete current;
			}
			else
				cout << "序号超出范围!" << endl;
		}
		else
			cout << "序号不能为负数!" << endl;
	}
}

//-----清空链表-----------------------------------------//
template <typename T>
void List<T>::clc()
{
	int length = len();
	for (int i = 0; i < length; i++)
		del(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值