双向链表类的封装

#pragma once
#include<iostream>
template<class T>
class Node
{
public:
	T info;
	Node<T>* pNext;
	Node<T>* pLast;


public:
	Node(int info);
	~Node();
};
template<class T>
class List
{
private:
	Node<T> * pHead;
	Node<T>* pEnd;


public:
	int IsEmpty();
	void AddToTail(T info);
	void AddToHead(T info);
	void AddToNode(T info,T aim);
	void DeleteFromHead();
	void DeleteFromTail();
	void DeleteNode(T aim);
	void DestoryList();


public:
	List();
	~List();

};

template<class T>
Node<T>::Node(int info)
{
	this->info = info;
	pLast = NULL;
	pNext = NULL;
}

template<class T>
Node<T>::~Node()
{
}

template<class T>
List<T>::List()
{
	pHead = NULL;
	pEnd = NULL;
}

template<class T>
List<T>::~List()
{
	this->DestoryList();
}
template<class T>
int List<T>::IsEmpty()
{
	if (pHead == NULL)
	{
		return 0;

	}
	else
	{
		return 1;

	}
}

template<class T>
void List<T>::AddToTail(T info)
{
	Node<T>* newnode = new  typename Node<T>::Node(info);
	if (IsEmpty() == 0)
	{
		pHead = newnode;
		pEnd = pHead;
		pHead->pLast = NULL;
		pHead->pNext = NULL;

	}
	else
	{
		if (pHead->pNext != NULL)
		{
			pEnd->pNext = newnode;
			newnode->pLast = pEnd;
			pEnd = newnode;
			pEnd->pNext = NULL;
		}
		else
		{
			pEnd = newnode;
			pEnd->pLast = pHead;
			pHead->pNext = pEnd;
			pEnd->pNext = NULL;
		}
	}
}

template<class T>
void  List<T> ::AddToHead(T info)
{
	Node<T>* newnode = new typename Node<T>::Node(info);
	if (IsEmpty() == 0)
	{
		pHead = newnode;
		pEnd = pHead;
		pHead->pLast = NULL;
		pHead->pNext = NULL;
	}
	else
	{
		if (pHead->pNext != NULL)
		{
			newnode->pNext = pHead;
			pHead->pLast = newnode;
			pHead = newnode;
			newnode->pLast = NULL;
		}
		else
		{
			newnode->pNext = pHead;
			pHead->pLast = newnode;
			pEnd = pHead;
			pHead = newnode;
			pEnd->pNext = NULL;
			pHead->pLast = NULL;
		}
	}
}

template<class T>
void  List<T>::AddToNode(T info, T aim)
{
	if (IsEmpty() == 0)
	{
		cout << "This List is empty!" << endl;


	}
	else
	{
		Node<T>* tmp = pHead;
		while (tmp != NULL)
		{
			if (tmp->info == aim)
			{
				Node<T>* newnode = new Node<T>(info);
				if (tmp->pLast == NULL)
				{
					tmp->pLast = newnode;
					newnode->pNext = pHead;
					pHead = newnode;

					return;

				}
				else
				{

					tmp->pLast->pNext = newnode;
					newnode->pLast = tmp->pLast;
					newnode->pNext = tmp;
					tmp->pLast = newnode;
					tmp = NULL;
					return;
				}
			}
			else
			{
				tmp = tmp->pNext;
			}
		}
	}
}

template<class T>
void  List<T>::DeleteFromHead()
{
	Node<T>* tmp;
	if (IsEmpty() == 0)
	{

		cout << "This List is empty!" << endl;

	}
	else
	{
		tmp = pHead;
		if (pHead->pNext == NULL)
		{
			DestoryList();
			return;
		}
		else
		{
			pHead = tmp->pNext;
			pHead->pLast = NULL;
			delete tmp;
			return;
		}
	}
}

template<class T>
void  List<T>::DeleteFromTail()
{
	Node<T>* tmp;
	if (IsEmpty() == 0)
	{

		cout << "This List is empty!" << endl;

	}
	else
	{
		tmp = pEnd;
		if (pEnd->pLast == NULL)
		{
			DestoryList();
			return;
		}
		else
		{
			pEnd = pEnd->pLast;
			pEnd->pNext = NULL;
			delete tmp;
		}
	}
}

template<class T>
void  List<T>::DeleteNode(T aim)
{
	if (IsEmpty() == 0)
	{
		cout << "This List is empty!" << endl;


	}
	else
	{
		Node<T>* tmp = pHead;
		while (tmp != NULL)
		{
			if (tmp->info == aim)
			{
				if (tmp->pLast == NULL)
				{
					this->DeleteFromHead();
					return;

				}
				else if (tmp->pNext == NULL)
				{
					this->DeleteFromTail();
					return;

				}
				else
				{
					tmp->pNext->pLast = tmp->pLast;
					tmp->pLast->pNext = tmp->pNext;
					delete tmp;
					return;
				}
			}
			else
			{
				tmp = tmp->pNext;
			}
		}
	}

}

template<class T>
void  List<T>::DestoryList()
{
	while (IsEmpty() != 0)
	{
		Node<T>* tmp;
		tmp = pHead->pNext;
		delete pHead;
		pHead = tmp;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值