jazm_blk
jazm_blk
2017-12-05 12:35

关于c++中的双向循环链表

  • c++
  • 数据
  • 链表
 #pragma once
#define V_TPLS template<class T>\
                void MyList<T>::

template<class T>
class MyList
{
    T data;
    MyList* next, *back;
public:
    MyList()
    {
        next = this;
        back = this;
        data = 0;
    }
    //在链表后插入数据
    void push_back(T data);
    //在链表前插入数据
    void push_front(T data);//前继
    //清空整个链表
    void _clear()
    {
        while (this->next!=this)
        {
            MyList *temp = this->next;
            this->next = this->next->next;
            delete temp;
        }
    }

    void put()
    {
        MyList *p = this->next;
        while (p!=this)
        {
            cout << p->data << ends;
            p = p->next;
        }
    }

    ~MyList()
    { _clear(); }
};

//在链表后插入数据
V_TPLS push_back(T data)
{
    MyList *temp = new MyList;
    temp->data = data;
    static MyList*p_n = this;
    temp->next = p_n->next;
    temp->back = p_n;
    p_n->next = temp;
    p_n = p_n->next;
    this->back = temp;
}
//在链表前插入数据
V_TPLS push_front(T data)
{
    MyList *temp = new MyList;
    temp->data = data;
    temp->next = this->next;
    temp->back = this;
    this->next->back = temp;
    this->next = temp;
}


为什么我的_clear()运行的时候this指针的地址为什么会改变呢?
每次运行到_clear()函数时就会报错

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答