#include<iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> *pNext;
Node<T> *pPrior;
};
template<class T>
class List
{
private:
Node<T> *pHead;
Node<T> *pTail;
int length;
public:
List(int length);
void traverseList();
void traverseListReturn();
void sortList();
bool insert(int index, T x);
bool ChangeList(int postion, int num);
void ClearList();
int DeleteList(int postion);
int GetList(int postion);
~List();
};
template<class T>
List<T>::List(int length)
{
this->length = length;
pHead = new Node<T>;
pHead->pPrior = NULL;
pTail = pHead;
for (int i = 0; i < length; i++)
{
Node<T> *temp = new Node<T>;
cout << "Please enter the no." << i + 1 << "Node's data:";
cin >> temp->data;
temp->pNext = NULL;
temp->pPrior = pTail;
pTail->pNext = temp;
pTail = temp;
}
}
template<class T>
void List<T>::traverseList()
{
Node<T> *p = pHead->pNext;
while (p != NULL)
{
cout << p->data << " ";
p = p->pNext;
}
cout << endl;
}
template<class T>
void List<T>::traverseListReturn()
{
Node<T>*p = pTail;
while (p->pPrior != NULL)
{
cout << p->data << " ";
p = p->pPrior;
}
cout << endl;
}
template<class T>
void List<T>::sortList()
{
Node<T> *p = new Node<T>;
Node<T> *q = new Node<T>;
T temp;
for (p = pHead->pNext; p->pNext != NULL; p = p->pNext)
{
for (q = p->pNext; q != NULL; q = q->pNext)
{
if (q->data < p->data)
{
temp = q->data;
q->data = p->data;
p->data = temp;
}
}
}
}
template<class T>
bool List<T>::insert(int index, T x)
{
if (index<1 || index>length)
{
return false;
}
Node<T> *p = new Node<T>;
p->data = x;
Node<T> *q = pHead;
for (int i = 0; i < index; i++)
{
q = q->pNext;
}
p->pPrior = q->pPrior;
q->pPrior->pNext = p;
p->pNext = q;
q->pPrior = p;
length++;
return true;
}
template<class T>
bool List<T>::ChangeList(int postion, int num)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return false;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
p->data = num;
}
template<class T>
void List<T>::ClearList()
{
Node<T> *q;
Node<T> *p = pHead->pNext;
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
p = NULL;
q = NULL;
}
template<class T>
int List<T>::DeleteList(int postion)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return -1;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
p->pPrior->pNext = p->pNext;
p->pNext->pPrior = p->pPrior;
delete p;
length--;
}
template<class T>
int List<T>::GetList(int postion)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return -1;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
return p->data;
}
template<class T>
List<T>::~List()
{
Node<T> *q;
Node<T> *p = pHead->pNext;
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
p = NULL;
q = NULL;
}
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> *pNext;
Node<T> *pPrior;
};
template<class T>
class List
{
private:
Node<T> *pHead;
Node<T> *pTail;
int length;
public:
List(int length);
void traverseList();
void traverseListReturn();
void sortList();
bool insert(int index, T x);
bool ChangeList(int postion, int num);
void ClearList();
int DeleteList(int postion);
int GetList(int postion);
~List();
};
template<class T>
List<T>::List(int length)
{
this->length = length;
pHead = new Node<T>;
pHead->pPrior = NULL;
pTail = pHead;
for (int i = 0; i < length; i++)
{
Node<T> *temp = new Node<T>;
cout << "Please enter the no." << i + 1 << "Node's data:";
cin >> temp->data;
temp->pNext = NULL;
temp->pPrior = pTail;
pTail->pNext = temp;
pTail = temp;
}
}
template<class T>
void List<T>::traverseList()
{
Node<T> *p = pHead->pNext;
while (p != NULL)
{
cout << p->data << " ";
p = p->pNext;
}
cout << endl;
}
template<class T>
void List<T>::traverseListReturn()
{
Node<T>*p = pTail;
while (p->pPrior != NULL)
{
cout << p->data << " ";
p = p->pPrior;
}
cout << endl;
}
template<class T>
void List<T>::sortList()
{
Node<T> *p = new Node<T>;
Node<T> *q = new Node<T>;
T temp;
for (p = pHead->pNext; p->pNext != NULL; p = p->pNext)
{
for (q = p->pNext; q != NULL; q = q->pNext)
{
if (q->data < p->data)
{
temp = q->data;
q->data = p->data;
p->data = temp;
}
}
}
}
template<class T>
bool List<T>::insert(int index, T x)
{
if (index<1 || index>length)
{
return false;
}
Node<T> *p = new Node<T>;
p->data = x;
Node<T> *q = pHead;
for (int i = 0; i < index; i++)
{
q = q->pNext;
}
p->pPrior = q->pPrior;
q->pPrior->pNext = p;
p->pNext = q;
q->pPrior = p;
length++;
return true;
}
template<class T>
bool List<T>::ChangeList(int postion, int num)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return false;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
p->data = num;
}
template<class T>
void List<T>::ClearList()
{
Node<T> *q;
Node<T> *p = pHead->pNext;
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
p = NULL;
q = NULL;
}
template<class T>
int List<T>::DeleteList(int postion)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return -1;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
p->pPrior->pNext = p->pNext;
p->pNext->pPrior = p->pPrior;
delete p;
length--;
}
template<class T>
int List<T>::GetList(int postion)
{
Node<T> *p = pHead->pNext;
if (postion > length || postion < 1)
{
return -1;
}
for (int i = 0; i < postion - 1; i++)
{
p = p->pNext;
}
return p->data;
}
template<class T>
List<T>::~List()
{
Node<T> *q;
Node<T> *p = pHead->pNext;
while (p != NULL)
{
q = p;
p = p->pNext;
delete q;
}
p = NULL;
q = NULL;
}
#include"DoubleList.h"
#include<iostream>
using namespace std;
int main(void)
{
List<float> Score(4);
Score.traverseList();
Score.traverseListReturn();
Score.sortList();
Score.traverseList();
Score.insert(3, 93);
Score.traverseList();
Score.ChangeList(2, 99);
Score.traverseList();
Score.sortList();
Score.traverseList();
cout << Score.GetList(2) << endl;
Score.traverseList();
system("pause");
return 0;
}