/*
* List.h
*
* Created on: 2016年11月10日
* Author: root
*/
#ifndef LIST_H_
#define LIST_H_
typedef int posit_t;
template<typename _TP>
class ListNode {
public:
_TP data;
ListNode* _M_Pre;
ListNode* _M_Next;
public:
ListNode() :
data(0), _M_Pre(NULL), _M_Next(NULL) {
}
ListNode(_TP d) :
data(d), _M_Pre(NULL), _M_Next(NULL) {
}
~ListNode() {
}
};
#define stl_aloc_rename(_p) \
typedef _p value_type ;\
typedef _p& reference ;\
typedef _p* pointer ; \
typedef const _p& const_reference ;\
typedef const _p* const_pointer ;
template<typename _TP>
class iterator_list {
public:
stl_aloc_rename( _TP )
typedef ListNode<_TP>* PNODE;
typedef iterator_list<_TP> self;
public:
iterator_list(PNODE _p) :
_M_PNode(_p) {
}
self& operator =(PNODE _p) {
_M_PNode = _p;
return *this;
}
reference operator *() const {
return _M_PNode->data;
}
pointer operator ->() const {
return &(_M_PNode->data);
}
self operator ++(int) { //i++
PNODE _p = _M_PNode;
_M_PNode = _M_PNode->_M_Next;
return _p;
}
self& operator++() {
_M_PNode = _M_PNode->_M_Next;
return _M_PNode;
}
self operator --(int) { //i--
PNODE _p = _M_PNode;
_M_PNode = _M_PNode->_M_Pre;
return *_p;
}
self& operator --() {
_M_PNode = _M_PNode->_M_Pre;
return _M_PNode;
}
const bool operator ==(self & _self) const {
return (_self._M_PNode == _M_PNode) ? (true) : (false);
}
bool operator !=(const self& _self) const {
return (_self._M_PNode == _M_PNode) ? (false) : (true);
}
public:
PNODE _M_PNode;
};
template<typename _TP>
class List {
public:
stl_aloc_rename( _TP )
typedef iterator_list<_TP> iterator;
protected:
typedef ListNode<_TP>* node_pointer;
public:
List() :
_M_PHead(NULL), _M_Tail(NULL), len(0) {
}
public:
int insert(const_reference _p, posit_t pos = 0) {
if (_M_PHead) {
node_pointer d = new ListNode<_TP>(_p);
node_pointer walk = _M_PHead;
for (int index = 0; index < pos; index++) {
walk = walk->_M_Next;
} //
d->_M_Next = walk->_M_Next;
walk->_M_Next->_M_Pre = d;
walk->_M_Next = d;
d->_M_Pre = walk;
} else {
_M_PHead = new ListNode<_TP>(_p);
_M_Tail = new ListNode<_TP>(NULL);
_M_PHead->_M_Next = _M_Tail;
_M_Tail->_M_Pre = _M_PHead;
}
++len;
return 0;
}
void clear() {
node_pointer p = _M_PHead;
while (p != _M_Tail) {
node_pointer _tmp = p;
p = p->_M_Next;
delete _tmp;
len--;
}
}
const bool find(const_reference d, int & pos) {
node_pointer pstart = _M_PHead;
node_pointer pend = _M_Tail;
int index = 0 ;
bool bfound = false ;
while(pstart != pend ){
if(pstart->data == d ){
bfound = true ;
goto end;
}
pstart=pstart->_M_Next ;
++index ;
}
end:
pos=index ;
return bfound;
}
const bool rfind(const_reference d, int & pos) {
node_pointer pstart = _M_PHead;
node_pointer pend = _M_Tail;
int index = 0 ;
bool bfound = false ;
while(pstart != pend ){
if(pend->data == d ){
bfound = true ;
goto end;
}
pend=pend->_M_Pre ;
++index ;
}
end:
pos=index ;
return bfound;
}
iterator begin() {
iterator tmp(_M_PHead);
return tmp;
}
iterator end() {
iterator tmp(_M_Tail);
return tmp;
}
#define __test__
#ifdef __test__
#include <iostream>
void print() {
node_pointer walk = _M_PHead;
while (walk) {
std::cout << walk->data << std::endl;
walk = walk->_M_Next;
}
}
#endif
private:
node_pointer _M_PHead;
node_pointer _M_Tail;
int len;
};
#endif /* LIST_H_ */
双向链表的C++实现
最新推荐文章于 2025-04-11 09:04:43 发布