#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;
}
}