- #ifndef __NODE_H__
- #define __NODE_H__
- template <typename T>
- class Node
- {
- private:
- Node *b2fcur;
- Node *f2bcur;
- protected:
- Node *parent_;
- Node *prev_;
- Node *next_;
- Node *firstchild;
- Node *lastchild;
- T data_;
- public:
- Node():parent_(0),firstchild(0),prev_(0),next_(0),lastchild(0),f2bcur(0),b2fcur(0){}
- Node(T t):data_(t),parent_(0),firstchild(0),prev_(0),next_(0),lastchild(0),f2bcur(0),b2fcur(0){}
- ~Node()
- {
- next_ = 0;
- prev_ = 0;
- parent_ = 0;
- firstchild = 0;
- lastchild = 0;
- f2bcur = 0;
- b2fcur = 0;
- }
- T data()
- {
- return (data_);
- }
- Node* parent()
- {
- return (parent_);
- }
- void SetParent(Node* parent)
- {
- parent_ = parent;
- }
- void AttachBack(Node* node)
- {
- if(firstchild)
- {
- node->next_ = firstchild->next_;
- firstchild->next_->prev_ = node;
- node->prev_ = firstchild;
- firstchild->next_ = node;
- }
- else
- {
- firstchild = node;
- lastchild = node;
- }
- node->parent_ = this;
- }
- void AttachFront(Node* node)
- {
- if(firstchild)
- {
- firstchild->prev_ = node;
- node->next_ = firstchild;
- firstchild = node;
- }
- else
- {
- firstchild = node;
- lastchild = node;
- }
- node->parent_ = this;
- }
- void RemoveFromParent()
- {
- if(parent_ == 0)
- return;
- if(next_)
- {
- next_->prev_ = prev_;
- if(prev_)
- prev_->next_ = next_;
- }
- if(parent_->firstchild == this)
- parent_->firstchild = next_;
- if(parent_->lastchild == this)
- parent_->lastchild = prev_;
- parent_ = 0;
- next_ = 0;
- prev_ = 0;
- }
- T F2BNext()
- {
- if(f2bcur)
- {
- f2bcur = f2bcur->next_;
- return f2bcur->data();
- }
- else
- {
- f2bcur = firstchild;
- }
- return f2bcur->data();
- }
- T B2FNext()
- {
- if(b2fcur)
- {
- b2fcur = b2fcur->prev_;
- return b2fcur->data();
- }
- else
- {
- b2fcur = lastchild;
- }
- return b2fcur->data();
- }
- };
- #endif
- //该片段来自于http://www.codesnippet.cn/detail/291120137618.html
来源: http://www.codesnippet.cn/detail/291120137618.html