📄 lit_list.h
字号:
///////////////////////////////////////////////////////////
// lit_list.h
// Implementation of the Class lit_list
// Created on: 18-二月-2009 17:17:15
// Original author: pangyj
///////////////////////////////////////////////////////////
#if !defined(EA_41773B59_650C_4fa0_8A1C_CFE8EC698125__INCLUDED_)
#define EA_41773B59_650C_4fa0_8A1C_CFE8EC698125__INCLUDED_
#include "lit_node.h"
template<class T>
class lit_list
{
public:
lit_list():head(NULL),tail(NULL),iCount(0){};
lit_list(const lit_list<T>& lt):head(NULL),tail(NULL),iCount(0)
{
lit_node<T> * pNode = lt.get_head();
while (pNode)
{
addNode(pNode->get_value());
pNode = pNode->next();
}
}
virtual ~lit_list()
{
clear();
};
bool addNode(T node, int pos = -1){
if (pos >= iCount)
{
return false;
}
lit_node<T> * pCur = new lit_node<T>(node);
if (!pCur)
{
return false;
}
if (tail == head && tail == NULL)
{
tail = head = pCur;
iCount ++;
return true;
}
int i = 0;
lit_node<T> * pTmpNode = head;
if (pos >= 0)
{
while (i < pos)
{
pTmpNode = pTmpNode->next();
++ i;
}
pCur->next(pTmpNode->next()) ;
if (pTmpNode->next())
{
pTmpNode->next()->pre(pCur);
}
else
{
tail = pCur;
}
pCur->pre(pTmpNode);
//////////////////////////////////////////////////////////////////////////
pTmpNode->next(pCur);
++ iCount;
}
else
{
pCur->pre(tail);
tail->next(pCur);
tail = pCur;
++ iCount;
if (iCount == 1)
{
head = tail;
}
}
return true;
}
void clear(){
if (head == NULL || tail == NULL)
{
return;
}
lit_node<T> * pNext = head->next();
lit_node<T> * pCurNode = head;
while (pCurNode)
{
delete pCurNode;
pCurNode = pNext;
if (pCurNode)
{
pNext = pCurNode->next();
}
}
tail = head = NULL;
iCount = 0;
}
int count()const{
return iCount;
}
lit_node<T>* get_head()const{
return head;
}
void set_head(lit_node<T>* node){
head = node;
}
bool isEmpty()const{
if (iCount)
{
return false;
}
return true;
}
bool removeByPos(int pos = -1)
{
if (pos == -1 || pos > iCount)
{
return false;
}
int i = 0;
lit_node<T> * pCurNode = head;
while (i < pos)
{
pCurNode = pCurNode->next();
i++;
}
//设置后继的前驱
if (pCurNode->next())
{
pCurNode->next()->pre(pCurNode->pre());
}
else if (pCurNode->pre())
{
pCurNode->pre()->next(NULL);
}
if (!pCurNode->next())
{
tail = pCurNode->pre();
}
//设置前驱的后继
if (pCurNode->pre())
{
pCurNode->pre()->next(pCurNode->next());
}
else if(pCurNode->next())
{
pCurNode->next()->pre(NULL);
}
if (!pCurNode->pre())
{
head = pCurNode->next();
}
delete pCurNode;
-- iCount;
return true;
}
bool removeByValue(T value){
lit_node<T> * pCurNode = head;
while (pCurNode && pCurNode != tail)
{
if (pCurNode->get_value() == value)
{
//设置后继的前驱
if (pCurNode->next())
{
pCurNode->next()->pre(pCurNode->pre());
}
else if (pCurNode->pre())
{
pCurNode->pre()->next(NULL);
}
//设置前驱的后继
if (pCurNode->pre())
{
pCurNode->pre()->next(pCurNode->next());
}
else if(pCurNode->next())
{
pCurNode->next()->pre(NULL);
}
delete pCurNode;
iCount --;
break;
}
pCurNode = pCurNode->next();
}
return true;
}
void set_tail(lit_node<T>* node){
tail = node;
}
lit_node<T>* get_tail()const{
return tail;
}
lit_node<T> * findValue(T&t)
{
lit_node<T> * pCurNode= head;
while (pCurNode &&pCurNode != tail)
{
if (pCurNode->get_value() == t)
{
return pCurNode;
}
pCurNode = pCurNode->next();
}
return NULL;
}
protected:
lit_node<T>* head;
lit_node<T>* tail;
private:
int iCount;
public:
lit_list<T> & operator = (const lit_list<T> & lt)
{
this->iCount = 0;
this->tail = NULL;
this->head = NULL;
lit_node<T> * pNode = lt.get_head();
while (pNode)
{
addNode(pNode->get_value());
pNode = pNode->next();
}
return * this;
}
};
#endif // !defined(EA_41773B59_650C_4fa0_8A1C_CFE8EC698125__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -