C++类模板实现双链表基本操作

#include <iostream>
using namespace std;

template<typename T>
struct DuLNode
{
	T val;
	DuLNode* prior;
	DuLNode* next;
};

template<typename T>
class DuList
{
public:
	DuList();
	~DuList();

	void append(T data); //头插法
	void traverse();  //遍历链表,从后往前打印链表元素
	void clear(); //清空链表
	void insert(T data, unsigned pos); //任意位置插入
	int find_pos(const T data);  //查找元素的位置
	void remove_value(const T data); //根据链表元素删除结点
	void remove_pos(unsigned pos); //根据链表位置删除结点
	DuLNode<T>* find_node(const T data);  //根据链表元素查找结点,未找到返回nullptr
	DuLNode<T>* get_head(); //返回头指针
	void reverse(); //翻转双链表
private:
	DuLNode<T>* head;
};

template<typename T>
DuList<T>::DuList()
{
	head = new DuLNode<T>;
	head->prior = nullptr;
	head->next = nullptr;
}

template<typename T>
DuList<T>::~DuList()
{
	clear();
	delete head;
}

template<typename T>
void DuList<T>::append(T data)
{
	DuLNode<T>* newnode = new DuLNode<T>;
	newnode->val = data;

	newnode->next = head->next;
	if(head->next)
		head->next->prior = newnode;
	head->next = newnode;
	newnode->prior = head;
}

template<typename T>
void DuList<T>::traverse()
{
	DuLNode<T>* cur = head->next;
	if (cur)
	{
		while (cur)
		{	
			if (cur->next == nullptr)
				break;
			cur = cur->next;
		}
		while (cur != head)
		{
			cout << cur->val << " ";
			cur = cur->prior;
		}
		cout << endl;
	}
}

template<typename T>
void DuList<T>::clear()
{
	if (head && head->next)
	{
		DuLNode<T>* cur = head->next;
		while (cur)
		{
			DuLNode<T>* t = cur->next;
			delete cur;
			cur = t;
		}
		head->next = nullptr;
	}
}

template<typename T>
void DuList<T>::insert(T data, unsigned pos)
{
	DuLNode<T>* cur = head;
	while(pos--)
	{
		cur = cur->next;
		if (cur->next == nullptr)
			break;
	}
	DuLNode<T>* newnode = new DuLNode<T>;
	newnode->val = data;
	newnode->next = cur->next;
	if (cur->next)
		cur->next->prior = newnode;
	cur->next = newnode;
	newnode->prior = cur;
}

template<typename T>
int DuList<T>::find_pos(const T data)
{
	if (head == nullptr)
		return -1;
	int pos = 1;
	DuLNode<T>* cur = head->next;
	while (cur)
	{
		if (cur->val == data)
			break;
		else
		{
			cur = cur->next;
			pos++;
		}
	}
	return pos;
}

template<typename T>
void DuList<T>::remove_value(const T data)
{
	if (head == nullptr)
		return;
	DuLNode<T>* cur = head->next;
	while (cur)
	{
		if (cur->val == data)
		{
			cur->prior->next = cur->next;
			if(cur->next)
				cur->next->prior = cur->prior;
			delete cur;
			break;
		}
		else
			cur = cur->next;
	}
}

template<typename T>
void DuList<T>::remove_pos(unsigned pos)
{
	if (head == nullptr)
		return;
	DuLNode<T>* cur = head->next;
	for (unsigned i = 1; i < pos; i++)
	{
		if (cur == nullptr)
			return;
		else
			cur = cur->next;
	}

	cur->prior->next = cur->next;
	if (cur->next)
		cur->next->prior = cur->prior;
	delete cur;
}

template<typename T>
DuLNode<T>* DuList<T>::find_node(const T data)
{
	if (head == nullptr)
		return nullptr;
	DuLNode<T>* cur = head->next;
	while (cur)
	{
		if (cur->val == data)
			return cur;
		else
			cur = cur->next;
	}
	return nullptr;
}

template<typename T>
DuLNode<T>* DuList<T>::get_head()
{
	return head;
}

template<typename T>
void DuList<T>::reverse()
{
	if (head == nullptr || head->next == nullptr || head->next->next == nullptr)
		return;
	DuLNode<T>* cur = head->next;
	head->next = nullptr;
	while (cur)
	{
		DuLNode<T>* t = cur->next;
		cur->next = head->next;
		if (head->next)
			head->next->prior = cur;
		head->next = cur;
		cur->prior = head;
		cur = t;
	}
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你看,那是海边

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值