/******************************************************************************
|
* FILE: MyList.h
|
* Description:
|
* Trace & Debug Tools Class.
|
*
|
* Modified Code History
|
* Mark Date By Modification Reason
|
*******************************************************************************
|
* 01 2007-1-31 songxw Initial creation.
|
******************************************************************************/
|
|
#ifndef __MYLIST_INCLUDE_H__
|
#define __MYLIST_INCLUDE_H__
|
|
using namespace std;
|
|
|
#define LIST_MAX_SIZE 20480
|
|
///////////////////////////////////////////////////////////////////////////////////
|
// Attention!
|
// The following list template only stores the class T pointers to dynamic storage.
|
// So the memory that the class T pointers to is not managed by this list,
|
// you must be careful about setup and cleanup of dynamic storage.
|
template <class T>
|
class MyList
|
{
|
public:
|
class MyListNode
|
{
|
public:
|
MyListNode(T * p)
|
{
|
data = p;
|
next =0;
|
}
|
|
~MyListNode()
|
{
|
if (data)
|
delete data; // Attention! when destroy the list, it will release the related dynamic memory
|
if (next)
|
delete next; // There is nested possibility. So delete data should be ahead of delete next.
|
}
|
//add end
|
|
MyListNode& operator=(const MyListNode& s)
|
{
|
if( &s != this)
|
{
|
if (data)
|
{
|
delete data;
|
data = 0;
|
}
|
if (next)
|
{
|
delete next;
|
next =0;
|
}
|
if (s.data)
|
data = new T(*s.data);
|
if(s.next)
|
next = new MyListNode(*s.next);
|
}
|
return *(this);
|
}
|
|
MyListNode(const MyListNode & s)
|
{
|
data = 0;
|
next = 0;
|
(*this) = s;
|
}
|
|
public:
|
T *data; // you should notice here!
|
MyListNode *next;
|
};
|
|
class iterator
|
{
|
public:
|
iterator() {value = 0;}
|
~iterator() {}
|
|
MyListNode *getValue() {return value;}
|
void setValue(MyListNode * pNode) {value = pNode;}
|
|
iterator & operator++(int)
|
{
|
if(value)
|
value = value->next;
|
return (*this);
|
}
|
|
T *operator *() {return value->data;}
|
const iterator & operator =(MyListNode * pNode)
|
{
|
value = pNode;
|
return (*this);
|
}
|
iterator & operator=(iterator & itr)
|
{
|
value = itr.getValue();
|
return (*this);
|
}
|
int operator!=(MyListNode * pNode) {return ( value != pNode);}
|
|
private:
|
MyListNode *value;
|
};
|
|
public:
|
MyList<T> ()
|
{
|
header = 0;
|
count = 0;
|
maxsize = LIST_MAX_SIZE;
|
}
|
~MyList/*<T>*/ ()
|
{
|
if (header)
|
delete header;
|
}
|
MyList<T> & operator =(const MyList<T> &s);
|
MyList<T> (const MyList <T> &s)
|
{
|
header = 0;
|
count = 0;
|
maxsize = LIST_MAX_SIZE;
|
(*this) = s;
|
}
|
|
bool empty() const {return count ? false : true;}
|
int size() const {return count;}
|
int max_size() {return maxsize;}
|
int insert(T *p, int index = -1);
|
T *getElement(int index) const;
|
int remove(T *p); // only remove the pointer from the list , not release the dynamic memory
|
int erase(int index); // not only remove the pointer from the list , but alse release the dynamic memory
|
int erase(iterator &loc); // not only remove the pointer from the list , but alse release the dynamic the memory
|
int clear();
|
|
MyListNode *begin() const {return header;}
|
MyListNode *end() const {return 0;}
|
|
private:
|
MyListNode *header;
|
int count;
|
int maxsize;
|
};
|
|
template <class T>
|
MyList<T> &MyList<T>::operator =(const MyList<T> &s)
|
{
|
if (&s != this)
|
{
|
this->clear();
|
maxsize = s.maxsize;
|
if (s.header)
|
header = new MyListNode(*s.header);
|
count = s.count;
|
}
|
|
return *(this);
|
}
|
|
template <class T>
|
int MyList<T>::clear()
|
{
|
if (header)
|
delete header;
|
header=0;
|
count =0;
|
maxsize = LIST_MAX_SIZE;
|
return 0;
|
}
|
|
template <class T>
|
T *MyList<T>::getElement(int index) const
|
{
|
if (index<0 || index >(count-1) )
|
return 0;
|
MyListNode *pCurr = header;
|
int pos_index = 0;
|
while (pCurr && pos_index != index)
|
{
|
pos_index++;
|
pCurr = pCurr->next;
|
}
|
return pCurr->data;
|
}
|
|
// Function Desciption
|
// index =0, insert at the header of the list;
|
// index = -1, insert at the bottom of the list, this is the default value.
|
// return value: -1 imply the failed, or return the current total count of nodes of the list.
|
template <class T>
|
int MyList<T>::insert(T *p, int index)
|
{
|
if (count > maxsize-1 || (index && index > (count-1) ) )
|
return (-1);
|
MyListNode *pNewNode = new MyListNode(p);
|
if (!pNewNode)
|
return (-1);
|
MyListNode *pCurr = header;
|
MyListNode *pPrev = 0;
|
int pos_index = 0;
|
while (pCurr && pos_index != index)
|
{
|
pos_index++;
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if (pCurr)
|
{
|
if (pPrev)
|
{
|
pPrev->next = pNewNode;
|
}
|
else
|
{
|
header = pNewNode;
|
}
|
pNewNode->next = pCurr;
|
}
|
else
|
{
|
if (index == (-1) || index ==0)
|
{
|
if (pPrev)
|
pPrev->next = pNewNode;
|
else
|
header = pNewNode;
|
}
|
}
|
count++;
|
|
return count;
|
}
|
|
// Attention! this function only remove the pointer from the list, not release the dynamic moemory
|
template <class T>
|
int MyList<T>::remove(T *p)
|
{
|
MyListNode *pCurr = header;
|
MyListNode *pPrev = 0;
|
while (pCurr && pCurr->data != p)
|
{
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if (pCurr)
|
{
|
if (pPrev)
|
pPrev->next = pCurr->next;
|
else
|
header = pCurr->next;
|
pCurr->next =0 ;
|
pCurr->data =0 ; // important! make sure the list didn't care of the dynamic memory of the data
|
delete pCurr;
|
count--;
|
|
return count;
|
}
|
else
|
return (-1);
|
}
|
|
// Attention! this function will release the related dynamic memory
|
template <class T>
|
int MyList<T>::erase(int index)
|
{
|
if (index<0 || index > count-1)
|
return (-1);
|
MyListNode *pCurr = header;
|
MyListNode *pPrev = 0;
|
int pos_index = 0;
|
while(pCurr && pos_index != index)
|
{
|
pos_index++;
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if(pCurr)
|
{
|
if(pPrev)
|
pPrev->next = pCurr->next;
|
else
|
header = pCurr->next;
|
pCurr->next =0;
|
delete pCurr;
|
count--;
|
|
return count;
|
}
|
else
|
return (-1);
|
}
|
|
// Attention! this function will release the related dynamic memory
|
template <class T>
|
int MyList<T>::erase(iterator & loc)
|
{
|
MyListNode *pCurr = header;
|
MyListNode *pPrev = 0;
|
while (pCurr && pCurr != loc.getValue() )
|
{
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if (pCurr)
|
{
|
if (pPrev)
|
pPrev->next = pCurr->next ;
|
else
|
header = pCurr->next;
|
loc.setValue(pPrev);
|
pCurr->next =0 ;
|
delete pCurr; // import! it will release the related dynamic memory
|
count--;
|
|
return count;
|
}
|
else
|
return (-1);
|
}
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//Attention!
|
//The following list template stores the copy of the value, it difference from the above MyList<T>
|
template <class T>
|
class MyVList
|
{
|
public:
|
class MyVListNode
|
{
|
public:
|
MyVListNode(T &d)
|
{
|
data = d;
|
next = 0;
|
}
|
~MyVListNode()
|
{
|
if (next)
|
delete next;
|
}
|
MyVListNode &operator =(const MyVListNode & s)
|
{
|
if (&s != this)
|
{
|
if(next)
|
{
|
delete next;
|
next = 0;
|
}
|
data = s.data;
|
if (s.next)
|
next = new MyVListNode(*s.next);
|
}
|
return *(this);
|
}
|
MyVListNode(const MyVListNode & s)
|
{
|
next = 0;
|
(*this) = s;
|
}
|
|
public:
|
T data;
|
MyVListNode *next;
|
};
|
|
class iterator
|
{
|
public:
|
iterator() {value = 0;}
|
~iterator() {}
|
|
MyVListNode *getValue() {return value;}
|
void setValue(MyVListNode * pNode) {value = pNode;}
|
|
iterator & operator++(int)
|
{
|
if (value)
|
value = value->next;
|
return (*this);
|
}
|
T operator *() {return value->data;}
|
const iterator &operator =(MyVListNode * pNode)
|
{
|
value = pNode;
|
return (*this);
|
}
|
iterator &operator =(iterator &itr)
|
{
|
value = itr.getValue();
|
return (*this);
|
}
|
int operator !=(MyVListNode *pNode) {return (value != pNode);}
|
|
private:
|
MyVListNode * value;
|
};
|
|
public:
|
MyVList<T> ()
|
{
|
header = 0;
|
count = 0;
|
maxsize = LIST_MAX_SIZE;
|
}
|
~MyVList/*<T>*/ ()
|
{
|
if (header)
|
delete header;
|
}
|
MyVList<T> &operator =(const MyVList<T> &s);
|
MyVList<T> (const MyVList<T> &s)
|
{
|
header = 0;
|
count =0;
|
(*this) = s;
|
}
|
|
bool empty() const {return count ? false : true;}
|
int size() const {return count;}
|
int max_size() {return maxsize;}
|
|
int insert(T data, int index = -1);
|
const T &getElement(int index);
|
int erase(int index);
|
int clear();
|
|
MyVListNode *begin() const {return header;}
|
MyVListNode *end() const {return 0;}
|
|
private:
|
MyVListNode *header;
|
int count;
|
int maxsize;
|
};
|
|
template<class T>
|
MyVList<T> &MyVList<T>::operator =(const MyVList<T> &s)
|
{
|
if (&s != this)
|
{
|
if (header)
|
this->clear();
|
maxsize = s.maxsize;
|
if (s.header)
|
header = new MyVListNode(*s.header);
|
count = s.count;
|
}
|
|
return (*this);
|
}
|
|
template<class T>
|
int MyVList<T>::clear()
|
{
|
if (header)
|
delete header;
|
header=0;
|
count =0;
|
|
return 0;
|
}
|
|
template<class T>
|
const T &MyVList<T>::getElement(int index)
|
{
|
MyVListNode * pCurr = header;
|
int pos_index =0;
|
while (pCurr && pos_index!=index)
|
{
|
pos_index++;
|
pCurr = pCurr->next;
|
}
|
|
return pCurr->data;
|
}
|
|
// Function Desciption
|
// index =0, insert at the header of the list;
|
// index = -1, insert at the bottom of the list, this is the default value.
|
// return value: -1 imply the failed, or return the current total count of nodes of the list.
|
template<class T>
|
int MyVList<T>::insert(T data, int index)
|
{
|
if (count > maxsize-1 || index<(-1) || index > (count-1) )
|
return (-1);
|
MyVListNode *pNewNode = new MyVListNode(data);
|
if (!pNewNode)
|
return (-1);
|
MyVListNode *pCurr = header;
|
MyVListNode *pPrev = 0;
|
int pos_index =0;
|
while (pCurr && pos_index != index)
|
{
|
pos_index++;
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if(pCurr)
|
{
|
if (pPrev)
|
{
|
pPrev->next = pNewNode;
|
}
|
else
|
{
|
header = pNewNode;
|
}
|
pNewNode->next = pCurr;
|
}
|
else
|
{
|
if (index == (-1) || index ==0)
|
{
|
if(pPrev)
|
pPrev->next = pNewNode;
|
else
|
header = pNewNode;
|
}
|
}
|
count++;
|
|
return count;
|
}
|
|
template<class T>
|
int MyVList<T>::erase(int index)
|
{
|
if (index < 0 || index > count-1)
|
return (-1);
|
MyVListNode * pCurr = header;
|
MyVListNode * pPrev = 0;
|
int pos_index = 0;
|
while (pCurr && pos_index != index)
|
{
|
pos_index++;
|
pPrev = pCurr;
|
pCurr = pCurr->next;
|
}
|
if (pCurr)
|
{
|
if (pPrev)
|
pPrev->next = pCurr->next;
|
else
|
header = pCurr->next;
|
pCurr->next =0 ;
|
delete pCurr;
|
count--;
|
|
return count;
|
}
|
else
|
return (-1);
|
}
|
///////////////////////////////////////////////////////////////////////////////
|
|
#endif //__MYLIST_INCLUDE_H__
|