c++类模板实现的双向链表
#ifndef _MYLIST_H_
#define _MYLIST_H_
template <class T>
class ListNode
{
public :
ListNode* pHead = NULL;
ListNode* pTail = NULL;
T value;
};
template <class T>
class ListHead
{
public :
ListNode<T>* pNext = NULL;
int length = 0 ;
};
template <class T>
class MyList
{
private :
ListHead<T> listHead;
ListNode<T>* pCurrentNode = NULL;
public :
MyList();
MyList(const MyList<T>& pList);
~MyList();
int copy(const MyList<T>& pListHead);
int deepCopy(const MyList<T>& pList);
int push_back(const T& node);
int push_front(const T& node);
int pop_back();
int pop_front();
void earse();
T& operator [](int i);
int empty();
int size();
};
#endif
#ifndef _MYLIST_HPP_
#define _MYLIST_HPP_
#include <iostream>
#include "myList.h"
template <class T>
MyList<T>::MyList()
{
this ->listHead.pNext = NULL;
this ->listHead.length = 0 ;
this ->pCurrentNode = NULL;
};
template <class T>
MyList<T>::MyList(const MyList<T>& list )
{
deepCopy(list );
};
template <class T>
MyList<T>::~MyList()
{
this ->earse();
}
template <class T>
int MyList<T>::copy(const MyList<T>& list )
{
this ->earse();
this ->listHead = list .listHead;
this ->pCurrentNode = list .pCurrentNode;
return 0 ;
}
template <class T>
int MyList<T>::deepCopy(const MyList<T>& list )
{
this ->earse();
ListNode<T>* m_pCurrentNode = list .listHead.pNext;
for (int i=0 ; i<list .listHead.length; i++)
{
int isSucess = this ->push_back(m_pCurrentNode->value);
if (isSucess == -1 )
{
this ->earse();
return -1 ;
}
m_pCurrentNode = m_pCurrentNode->pTail;
}
return 0 ;
};
template <class T>
int MyList<T>::push_back(const T& value)
{
ListNode<T>* m_pNode = new ListNode<T>[1 ];
if (m_pNode == NULL)
return -1 ;
m_pNode->pTail = NULL;
m_pNode->pHead = this ->pCurrentNode;
m_pNode->value = value;
if (this ->listHead.length == 0 )
this ->listHead.pNext = m_pNode;
else
this ->pCurrentNode->pTail = m_pNode;
this ->pCurrentNode = m_pNode;
this ->listHead.length ++;
return 0 ;
};
template <class T>
int MyList<T>::push_front(const T& value)
{
ListNode<T>* m_pNode = new ListNode<T>[1 ];
if (m_pNode == NULL)
return -1 ;
if (this ->listHead.length != 0 )
this ->listHead.pNext->pHead = m_pNode;
m_pNode->value = value;
m_pNode->pHead = NULL;
m_pNode->pTail = this ->listHead.pNext;
this ->listHead.pNext = m_pNode;
this ->listHead.length ++;
return 0 ;
}
template <class T>
int MyList<T>::pop_back()
{
if (this ->listHead.length <= 0 )
return -1 ;
this ->pCurrentNode = this ->pCurrentNode->pHead;
this ->listHead.length --;
delete this ->pCurrentNode->pTail;
this ->pCurrentNode->pTail = NULL;
return 0 ;
}
template <class T>
int MyList<T>::pop_front()
{
if (this ->listHead.length <= 0 )
return -1 ;
ListNode<T>* m_pTemporaNode = this ->listHead.pNext;
this ->listHead.pNext = m_pTemporaNode->pTail;
this ->listHead.length --;
delete m_pTemporaNode;
m_pTemporaNode = NULL;
return 0 ;
}
template <class T>
void MyList<T>::earse()
{
int m_listLength = this ->listHead.length;
for (int i=0 ; i<m_listLength; i++)
{
pop_front();
}
}
template <class T>
T& MyList<T>::operator [](int num)
{
int m_number = 0 ;
if (num >= this ->size() || (-num) >= this ->size())
m_number = 0 ;
else if (num >= 0 )
m_number = num;
else
m_number = num + this ->listHead.length;
ListNode<T>* m_pCurrentNode = this ->listHead.pNext;
for (int i = 0 ; i < m_number; ++i)
{
m_pCurrentNode = m_pCurrentNode->pTail;
}
return m_pCurrentNode->value;
}
template <class T>
int MyList<T>::empty()
{
if (this .listHead.length <= 0 )
return -1 ;
return 0 ;
}
template <class T>
int MyList<T>::size()
{
return this ->listHead.length;
}
#endif
#include <iostream>
#include "myList.hpp"
int main(int argc, char ** argv) {
MyList<int > lis;
int k = 4 ;
lis.push_back(4 );
lis.push_back(6 );
lis.push_back(1 );
lis.push_back(9 );
lis.push_front(7 );
lis.push_front(55 );
MyList<int > li(lis);
for (int i=0 ; i < lis.size(); i++)
{
std ::cout << li[i] << std ::endl;
}
lis.earse();
std ::cout << "please input any key" << std ::endl;
getchar();
return 0 ;
}