C++实现双链表(使用模板类)

双链表

结点定义

template <class T>
class DNode
{
public:
	T data;
	DNode<T>* next,* pre;
};

方法定义

template <class T>
class DLink_List
{
private:
	DNode<T>* head;

public:
	int size;

	DLink_List();
	bool isEmpty();
	bool Insert(int pos, T elem);
	bool Delete(int pos, T& elem);
	bool Find(int pos, T& elem);
	void show();
	~DLink_List();
};

总体实现

//双链表
#include <iostream>
#include "DLink_List.h"

using namespace std;

template <class T>
DLink_List<T>::DLink_List()
{
	head = new DNode<T>;
	head->next = NULL;
	head->pre = NULL;
	this->size = 0;
}

template <class T>
bool DLink_List<T>::isEmpty()
{
	return this->size == 0;
}

template <class T>
bool DLink_List<T>::Insert(int pos, T elem)
{
	if (pos < 0 || pos > this->size)
		return false;
	
	DNode<T>* p = new DNode<T>;
	p->data = elem;
	p->pre = p->next = NULL;
	DNode<T>* temp = head;
	if (pos == 0)
	{
		if (head->next == NULL)
		{
			p->pre = head;
			head->next = p;
		}
		else
		{
			p->next = head->next;
			head->next->pre = p;
			p->pre = head;
			head->next = p;
		}		
	}
	else if (pos == this->size)
	{
		while (temp->next != NULL)
			temp = temp->next;
		temp->next = p;
		p->pre = temp;
	}
	else
	{
		while (pos-- > 0)
			temp = temp->next;
		p->next = temp->next;
		p->pre = temp;
		temp->next->pre = p;
		temp->next = p;
	}

	this->size++;
	return true;
}

template <class T>
bool DLink_List<T>::Delete(int pos, T& elem)
{
	if (pos <= 0 || pos > this->size || isEmpty())
		return false;
	DNode<T>* p = head;
	if (pos == 1)
	{
		if (head->next->next == NULL)
		{
			p = p->next;
			elem = p->data;
			head->next = NULL;
			delete p;
		}
		else
		{
			p = p->next;
			elem = p->data;
			head->next = p->next;
			p->next->pre = head;
		}
	}
	else if (pos == this->size)
	{
		while (p->next != NULL)
			p = p->next;
		elem = p->data;
		p->pre->next = NULL;
		delete p;
	}
	else
	{
		while (pos-- > 0)
			p = p->next;
		elem = p->data;
		p->pre->next = p->next;
		p->next->pre = p->pre;
		delete p;
	}

	this->size--;
	return true;
}

template <class T>
bool DLink_List<T>::Find(int pos, T& elem)
{
	if (pos <= 0 || pos > this->size || isEmpty())
		return false;
	DNode<T>* p = head;
	while (pos-- > 0)
		p = p->next;
	elem = p->data;
	return true;
}

template <class T>
void DLink_List<T>::show()
{
	DNode<T>* p = head;
	while (p->next != NULL)
	{
		p = p->next;
		cout << p->data << " ";
	}
	cout << endl;
}

template <class T>
DLink_List<T>::~DLink_List()
{
	delete head;
}

分块

初始化

template <class T>
DLink_List<T>::DLink_List()
{
	head = new DNode<T>;
	head->next = NULL;
	head->pre = NULL;
	this->size = 0;
}

判空

template <class T>
bool DLink_List<T>::isEmpty()
{
	return this->size == 0;
}

插入

template <class T>
bool DLink_List<T>::Insert(int pos, T elem)
{
	if (pos < 0 || pos > this->size)
		return false;
	
	DNode<T>* p = new DNode<T>;
	p->data = elem;
	p->pre = p->next = NULL;
	DNode<T>* temp = head;
	if (pos == 0)
	{
		if (head->next == NULL)
		{
			p->pre = head;
			head->next = p;
		}
		else
		{
			p->next = head->next;
			head->next->pre = p;
			p->pre = head;
			head->next = p;
		}		
	}
	else if (pos == this->size)
	{
		while (temp->next != NULL)
			temp = temp->next;
		temp->next = p;
		p->pre = temp;
	}
	else
	{
		while (pos-- > 0)
			temp = temp->next;
		p->next = temp->next;
		p->pre = temp;
		temp->next->pre = p;
		temp->next = p;
	}

	this->size++;
	return true;
}

删除

template <class T>
bool DLink_List<T>::Delete(int pos, T& elem)
{
	if (pos <= 0 || pos > this->size || isEmpty())
		return false;
	DNode<T>* p = head;
	if (pos == 1)
	{
		if (head->next->next == NULL)
		{
			p = p->next;
			elem = p->data;
			head->next = NULL;
			delete p;
		}
		else
		{
			p = p->next;
			elem = p->data;
			head->next = p->next;
			p->next->pre = head;
		}
	}
	else if (pos == this->size)
	{
		while (p->next != NULL)
			p = p->next;
		elem = p->data;
		p->pre->next = NULL;
		delete p;
	}
	else
	{
		while (pos-- > 0)
			p = p->next;
		elem = p->data;
		p->pre->next = p->next;
		p->next->pre = p->pre;
		delete p;
	}

	this->size--;
	return true;
}

按位序查找

template <class T>
bool DLink_List<T>::Find(int pos, T& elem)
{
	if (pos <= 0 || pos > this->size || isEmpty())
		return false;
	DNode<T>* p = head;
	while (pos-- > 0)
		p = p->next;
	elem = p->data;
	return true;
}

遍历

template <class T>
void DLink_List<T>::show()
{
	DNode<T>* p = head;
	while (p->next != NULL)
	{
		p = p->next;
		cout << p->data << " ";
	}
	cout << endl;
}

销毁

template <class T>
DLink_List<T>::~DLink_List()
{
	delete head;
}

测试代码

#include <iostream>
#include "DLink_List.h"
#include "DLink_List.cpp"

using namespace std;

int main()
{
	DLink_List<int> dll;
	for (int i = 0; i < 5; i++)
	{
		dll.Insert(0, i + 1);
		cout << "头插:" << i + 1 << ":";
		dll.show();
	}
	for (int i = 0; i < 5; i++)
	{
		dll.Insert(dll.size, i + 11);
		cout << "尾插:" << i + 11 << ":";
		dll.show();
	}
	for (int i = 0; i < 3; i++)
	{
		dll.Insert(2, i + 101);
		cout << "在第2个位置插入" << i + 101 << ":";
		dll.show();
	}

	getchar();
	system("cls");
	cout << "当前剩余:";
	dll.show();

	int val;
	cout << "头删:";
	dll.Delete(1, val);
	cout << val << endl;
	dll.show();
	cout << "头删:";
	dll.Delete(1, val);
	cout << val << endl;
	dll.show();
	cout << "头删:";
	dll.Delete(1, val);
	cout << val << endl;
	dll.show();
	cout << "尾删:";
	dll.Delete(dll.size, val);
	cout << val << endl;
	dll.show();
	cout << "尾删:";
	dll.Delete(dll.size, val);
	cout << val << endl;
	dll.show();
	cout << "尾删:";
	dll.Delete(dll.size, val);
	cout << val << endl;
	dll.show();
	cout << "在第2个位置删除:";
	dll.Delete(2, val);
	cout << val << endl;
	dll.show();
	cout << "在第2个位置删除:";
	dll.Delete(2, val);
	cout << val << endl;
	dll.show();

	getchar();
	system("cls");
	cout << "当前剩余:";
	dll.show();

	dll.Find(3, val);
	cout << "3号位置为:" << val << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

逸人止

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

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

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

打赏作者

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

抵扣说明:

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

余额充值