C++ 将链接列表更改为双链接列表

C++ 将链接列表更改为双链接列表,c++,templates,doubly-linked-list,C++,Templates,Doubly Linked List,嗨,你能帮我把这个链表改成双链表吗? 我将非常感谢您的帮助:) #包括 使用名称空间std; 模板 结构节点 { T数据; 节点*下一步; 节点*prev; }; 模板 类容器 { 公众: //构造一个新的空Kontener 容器() { 头=新节点; 头部->下一步=头部; 头部->上一个=头部; }; //构造一个新的jp_列表,该列表是现有列表的副本 Container(const Container<T>& rt_side) { head

嗨,你能帮我把这个链表改成双链表吗? 我将非常感谢您的帮助:)

#包括
使用名称空间std;
模板
结构节点
{
T数据;
节点*下一步;
节点*prev;
};
模板
类容器
{
公众:
//构造一个新的空Kontener
容器()
{
头=新节点;
头部->下一步=头部;
头部->上一个=头部;
};
//构造一个新的jp_列表,该列表是现有列表的副本

 Container(const Container<T>& rt_side)
    {
        head = new node<T>;
        head->next = head;
        head->prev = head;

        node<T> *crt_ptr = rt_side.head->next;
        while(crt_ptr != rt_side.head)
        {
            push_back(crt_ptr->data);
            crt_ptr = crt_ptr->next;
        }
    };

    //adds a data node to the front of the list
    void push_front(T nw_data)
    {
        node<T> *temp = new node<T>;
        temp->data = nw_data;

        temp->next = head->next;
        head->next->prev = temp;

        temp->prev = head;
        head->next = temp;
    };

    //adds a data node to the end of the list
    void push_back(T nw_data)
    {
        node<T> *temp = new node<T>;
        temp->data = nw_data;

        head->prev->next = temp;
        temp->prev = head->prev;

        temp->next = head;
        head->prev = temp;
    };

    //removes the first node and returns the data
    T pop_front()
    {
        node<T> *temp = head->next;
        T temp_data = head->next->data;

        head->next = temp->next;
        temp->next->prev = head;

        delete temp;

        return temp_data;
    };

    //removes the last node and returns the data
    T pop_back()
    {
        node<T> *temp = head->prev;
        T temp_data = head->prev->data;

        head->prev = temp->prev;
        temp->prev->next = head;

        delete temp;

        return temp_data;
    };

    //resturns the size of the list
    int size()
    {
        int size = 0;
        node<T> *crt_ptr; //pointer to current node

        crt_ptr = head->next;
        while(crt_ptr != head)
        {
            size += 1;
            crt_ptr = crt_ptr->next; //advance to the next node then loop
        }

        return size;
    };

    //prints out all the data in the list
    void display_all()
    {
        node<T> *crt_ptr = head->next;

        for(int i = 0; crt_ptr != head; i++)
        {
            cout << "Node " << (i+1) << ": " << crt_ptr->data << endl;

            crt_ptr = crt_ptr->next;
        }
    };

    Container& operator= (const Container& rt_side)
    {
        if(this == &rt_side)
            return *this;
        node<T> *crt_ptr = head->next;

        //empty this list so the rt_side can be coppied in
        while(crt_ptr != head)
        {
            crt_ptr = crt_ptr->next;
            pop_front();
        }

        crt_ptr = rt_side.head->next;

        while(crt_ptr != rt_side.head)
        {
            push_back(crt_ptr->data);
            crt_ptr = crt_ptr->next;
        }

        return *this;
    };

    virtual ~Container()
    {
        int list_size = size();

        for(int i = 0; i < list_size; i++)
        {
            pop_front();
        }

        delete head;
    };

private:
    node<T> *head;

};

#endif
集装箱(集装箱和码头侧)
{
头=新节点;
头部->下一步=头部;
头部->上一个=头部;
节点*crt\U ptr=rt\U侧。头部->下一步;
while(crt\U ptr!=rt\U侧头)
{
向后推(crt\U ptr->数据);
crt_ptr=crt_ptr->next;
}
};
//将数据节点添加到列表的前面
无效推力前(T nw_数据)
{
node*temp=新节点;
温度->数据=nw_数据;
温度->下一步=头部->下一步;
头部->下一步->上一步=温度;
温度->上一个=压头;
头部->下一步=温度;
};
//将数据节点添加到列表的末尾
无效推回(T nw_数据)
{
node*temp=新节点;
温度->数据=nw_数据;
头部->上一个->下一个=温度;
温度->上一个=压头->上一个;
温度->下一步=头部;
压头->上一个=温度;
};
//删除第一个节点并返回数据
T pop_front()
{
节点*temp=head->next;
T临时数据=头部->下一步->数据;
头部->下一步=临时->下一步;
临时->下一个->上一个=头部;
删除临时文件;
返回温度数据;
};
//删除最后一个节点并返回数据
T pop_back()
{
节点*temp=head->prev;
温度数据=头部->上一个->数据;
压头->上一个=温度->上一个;
临时->上一个->下一个=头部;
删除临时文件;
返回温度数据;
};
//重新恢复列表的大小
int size()
{
int size=0;
node*crt_ptr;//指向当前节点的指针
crt_ptr=头部->下一步;
while(crt_ptr!=头部)
{
大小+=1;
crt_ptr=crt_ptr->next;//前进到下一个节点,然后循环
}
返回大小;
};
//打印出列表中的所有数据
无效显示所有内容()
{
节点*crt\U ptr=head->next;
for(int i=0;crt_ptr!=head;i++)
{
下一步;
while(crt\U ptr!=rt\U侧头)
{
向后推(crt\U ptr->数据);
crt_ptr=crt_ptr->next;
}
归还*这个;
};
虚拟容器()
{
int list_size=size();
对于(int i=0;i

我只是一个初学者,所以请帮助我:)

尾部总是指向插入列表的最后一项

然而,我不认为有一个尾部指针就一定是一个双链接列表。一个单链接列表也可以有一个尾部指针(不管它有多无用)。我相信你是在要求创建一个双端双链接列表

您已经有了启用双链接的下一个和上一个指针。您所要做的就是,当您将某个内容推送到列表中时,您需要使尾部指针指向正在添加的节点。同样,在删除节点时,您需要尾部指针指向尾部的上一个,然后再删除最后一个节点

*更新* 下面是一些代码。我假设一个两端都是双端的双链表

void push_front(T nw_data)
{
    node<T> *temp = new node<T>;
    temp->data = nw_data;

    if(head == nullptr)
    {
        head = temp;
        tail = temp;
    }
    else if(head == tail)
    {
        head->next = temp;
        temp->prev = head;
        tail = temp;
    }
    else
    {
        temp->next = head->next;
        head->next->prev = temp;

        temp->prev = head;
        head->next = temp;        
    }        
};

//adds a data node to the end of the list
void push_back(T nw_data)
{
    node<T> *temp = new node<T>;
    temp->data = nw_data;

    if(head == nullptr)
    {
        head = temp;
        tail = temp;
    }
    else if(head == tail)
    {
        head->next = temp;
        temp->prev = head;
        tail = temp;
    }
    else
    {
        temp->prev = tail;
        tail->next = temp;
        tail = temp;      
    }  
};

T pop_back()
{
    node<T> *temp = tail;
    T temp_data = tail->data;

    tail = tail->prev;
    tail->next = null;

    delete temp;

    return temp_data;
};
void push_front(T nw_数据)
{
node*temp=新节点;
温度->数据=nw_数据;
if(head==nullptr)
{
压头=温度;
尾=温度;
}
else if(head==tail)
{
头部->下一步=温度;
温度->上一个=压头;
尾=温度;
}
其他的
{
温度->下一步=头部->下一步;
头部->下一步->上一步=温度;
温度->上一个=压头;
头部->下一步=温度;
}        
};
//将数据节点添加到列表的末尾
无效推回(T nw_数据)
{
node*temp=新节点;
温度->数据=nw_数据;
if(head==nullptr)
{
压头=温度;
尾=温度;
}
else if(head==tail)
{
头部->下一步=温度;
温度->上一个=压头;
尾=温度;
}
其他的
{
温度->上一个=尾部;
尾部->下一步=温度;
尾=温度;
}  
};
T pop_back()
{
节点*温度=尾部;
T温度数据=尾部->数据;
tail=tail->prev;
tail->next=null;
删除临时文件;
返回温度数据;
};
*更新*复制构造函数 在复制构造函数中,如果push_back设置了尾部,那么您需要做的就是像现在一样将节点推回。head->next=head和head->prev=head使链接列表循环

Container(const Container<T>& rt_side)
{
    this->head = rt_side.head;

    node<T> * crt_ptr = rt_side.head->next;

    while (crt_ptr != null)
    {
        push_back(crt_ptr->data);

        crt_ptr = crt_ptr->next;
    }
};
集装箱(集装箱和码头侧)
{
此->头部=右侧头部;
节点*crt\U ptr=rt\U侧。头部->下一步;
while(crt_ptr!=null)
{
向后推(crt\U ptr->数据);
crt_ptr=crt_ptr->next;
}
};

您好,您能帮我将此链表更改为双链表吗?
若要更改某些内容,这些内容必须已经存在。您现有的非双链表类在哪里?我知道我必须添加节点*tail;但我不知道如何实现它。@MIchaelDonson
我知道我必须添加节点*tail节点*tail
双链接列表不需要这个指针,除非你想快速找到最后一个元素。你只需要
next
prev
顺便说一句,你的
操作符=
不需要这么复杂:
{Container temp=rt_side;std::swap(temp.head,head);return*this;}
只有当复制构造函数和析构函数正常工作时,此操作才有效。在推回过程中,存在许多问题。head->prev是解引用
Container(const Container<T>& rt_side)
{
    this->head = rt_side.head;

    node<T> * crt_ptr = rt_side.head->next;

    while (crt_ptr != null)
    {
        push_back(crt_ptr->data);

        crt_ptr = crt_ptr->next;
    }
};