双向链表的C++实现

/*
 * 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_ */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值