ListNode.h
template<typename Type>class DoublyList;
template<typename Type>class ListNode{
private:
Type m_data;
ListNode *m_pprior;
ListNode *m_pnext;
public:
Type GetData();
private:
friend class DoublyList<Type>;
ListNode():m_pprior(NULL),m_pnext(NULL){};
ListNode(const Type item,ListNode<Type> *prior=NULL,ListNode<Type> *next = NULL)
:m_data(item),m_pprior(prior),m_pnext(next){}
~ListNode(){
m_pprior = NULL;
m_pnext = NULL;
}
Type GetData(){return m_data;}
};
DoublyList.h
#include "ListNode.h"
template<typename Type>class DoublyList{
public:
DoublyList():head(new ListNode<Type>){
head->m_pprior = head;
head->m_pnext = head;
}
~DoublyList(){
MakeEmpty();
delete head;
}
public:
void MakeEmpty();
int Length();
ListNode<Type> *Find(int n = 0);
ListNode<Type> *FindData(Type item);
bool Insert(Type item,int n = 0);
Type Remove(int n = 0);
Type Get(int n = 0);
void Print();
private:
ListNode<Type> *head;
};
DoublyList.cpp
template<typename Type> void DoublyList<Type>::MakeEmpty(){
ListNode<Type>* p;
while(head->m_pnext != head)
{
p = head->m_pnext;
head->m_pnext = p->m_pnext;
p->m_pnext->m_pprior = head;
delete p;
}
}
template<typename Type> int DoublyList<Type>::Length(){
int count = 0;
ListNode<Type>* p = head;
while(p->m_pnext != head)
{
p = p->m_pnext;
count++;
}
return count;
}
template<typename Type> ListNode<Type>* DoublyList<Type>::Find(int n = 0){
if(n < 0)
{
cout<<"The n is out of boundary"<<endl;
return NULL;
}
int count = 0;
ListNode<Type> *p = head;
while(p->m_pnext != head)
{
p=p->m_pnext;
if(count == n)
return p;
count++;
}
cout<<"The n is out of boundary"<<endl;
return NULL;
}
template<typename Type> bool DoublyList<Type>::Insert(Type item,int n){
if(n < 0)
{
cout<<"The n is out of boundary"<<endl;
return 0;
}
int count = 0;
ListNode<Type> *p = head;
while(1)
{
if(count == n)
{
ListNode<Type> *q = new ListNode<Type>(item,p,p->m_pnext);
p->m_pnext->m_pprior = q;
p->m_pnext = q;
return 1;
}
p = p->m_pnext;
count++;
if(p == head)
break;
}
cout<<"The n is out of boundary"<<endl;
return 0;
}
template<typename Type> Type DoublyList<Type>::Remove(int n = 0){
if(n < 0)
{
cout<<"The n is out of boundary"<<endl;
return 0;
}
int count = 0;
ListNode<Type> *p = head;
while(1)
{
if(count == n)
{
ListNode<Type> *q = p->m_pnext;
p->m_pnext = q->m_pnext;
p->m_pnext->m_pprior = p;
Type temp = q->GetData();
delete q;
return temp;
}
p = p->m_pnext;
count++;
if(p == head)
break;
}
cout<<"The n is out of boundary"<<endl;
return 0;
}
template<typename Type> Type DoublyList<Type>::Get(int n = 0){
if(n < 0)
{
cout<<"The n is out of boundary"<<endl;
return 0;
}
int count = 0;
ListNode<Type> *p = head;
while(p->m_pnext != head)
{
p = p->m_pnext;
if(count == n)
return p->GetData();
count++;
}
cout<<"The n is out of boundary"<<endl;
return 0;
}
template<typename Type> void DoublyList<Type>::Print(){
ListNode<Type> *p = head;
while(p->m_pnext != head)
{
p = p->m_pnext;
cout<<p->GetData()<<' ';
}
cout<<endl;
}
template<typename Type> ListNode<Type>* DoublyList<Type>::FindData(Type item){
ListNode<Type> *p = head;
while(p->m_pnext != head)
{
p = p->m_pnext;
if(p->GetData() == item)
return p;
}
cout<<"can't find the element"<<endl;
return 0;
}
Test.cpp
#include <iostream>
#include "DoubleList.h"
using namespace std;
int main()
{
DoublyList<int> list;
for(int i=0;i<20;i++){
list.Insert(i*3,i);
}
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();
for(int i=0;i<5;i++){
list.Insert(3,i*3);
}
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();
list.Remove(5);
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();
cout<<list.FindData(54)->GetData()<<endl;
cout<<"The third element is "<<list.Get(3)<<endl;
list.MakeEmpty();
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();
return 0;
}