【问题标题】:Iterating through a custom double linked list遍历自定义双链表
【发布时间】:2020-09-08 19:57:36
【问题描述】:

我正在实现一个双链表。我已经实现了添加到列表头部和尾部的功能。我一直在尝试制作一个可以打印给定对象的所有元素的函数。 尽管该函数会打印第一个值,但它不会打印列表中的任何其他项目。

d_list.h

 #include<iostream>
 #include "node.h"

 #ifndef NODE_H
 #define NODE_H


template<class T>
class d_list{
   private:
       int l_size;
       Node<T>* head;
       Node<T>* tail;

   public:
       d_list():l_size(0){
          head=nullptr;
          tail=nullptr;
       }
       Node<T>* gethead(){return this->head;}
       Node<T>* gettail(){return this->tail;}

         int getlength(){
           return this->l_size;
         }

       void addashead(T data){
          Node<T>* curr_node=new Node<T>(data);
          if(this->head==nullptr){
            this->head=curr_node;
            this->tail=curr_node;
            this->l_size=l_size+1;
          }else {
             curr_node->next=this->head->previous;
              this->head->previous=curr_node;
             this->head=curr_node;

              this->l_size=l_size+1;
          }
       }

       void addastail(T data ){
          Node<T>* curr_node=new Node<T>(data);
          if(this->tail==this->head){
            this->head->next=curr_node->previous;
            curr_node->previous=this->head;
            this->tail=curr_node;
            this->tail->next=nullptr;
            this->l_size=l_size+1;
          }
          else{
            this->tail->next=curr_node;
            curr_node->previous=this->tail;
            this->tail=curr_node;
            this->l_size=l_size+1;
          }
       }

       Node<T>* begin(){
         return this->head;
       }

       Node<T>* end(){
         return this->tail;
       }

       bool isempty(){
         return this->head==nullptr;
       }

       T& back(){
         return this->tail->m_data;
       }

       T& front(){
         return this->head->m_data;
       }

       void printlist(){
           Node<T>* temp= this->head;
        while(temp) {
        std::cout << temp->m_data<< " -> ";
        temp = temp->next;
    }


       }

};

#endif

节点.h

template <class T>
class Node{
    private:


    public:
       T m_data;
       Node<T>* next;
       Node<T>* previous;
        Node()=default;
        Node(T data){
           m_data=data;
           previous=nullptr;
           next=nullptr;
        }
       ~Node();


};

main.cpp

#include "d_list.h"

#include<iostream>
using namespace std;



int main(){

    d_list<int> d1;


    d1.addashead(50);



    d1.addastail(20);


    d1.addastail(60);



    d1.addastail(80);



    d1.printlist();
    return 0;
}

【问题讨论】:

    标签: c++ list data-structures


    【解决方案1】:

    你的 addastail() 和 addashead() 方法有一些错误。

    我删除了这些错误,检查一下:

     void addashead(T data){
              Node<T>* curr_node=new Node<T>(data);
              if(this->head==nullptr){
                this->head=curr_node;
                this->tail=curr_node;
                this->l_size=l_size+1;
              }else {
                 curr_node->next=this->head;
                  this->head->previous=curr_node;
                 this->head=curr_node;
                 this->l_size=l_size+1;
               }
    
       }
    

    在else部分的第一行, 你写道:

    curr_node->next=this->head->previous;
    

    但应该是:curr_node-&gt;next=this-&gt;head;

    因为 curr_node 的下一个应该指向头部,而不是头部的上一个。

    void addastail(T data ){
              Node<T>* curr_node=new Node<T>(data);
              if(this->tail==this->head){
                this->head->next=curr_node;
                curr_node->previous=this->head;
                this->tail=curr_node;
                this->tail->next=nullptr;
                this->l_size=l_size+1;
              }
              else{
                this->tail->next=curr_node;
                curr_node->previous=this->tail;
                this->tail=curr_node;
                this->l_size=l_size+1;
              }
           }
    

    在if部分的第一行,你写了:

    this->head->next=curr_node->previous;
    

    但应该是:this-&gt;head-&gt;next=curr_node;

    【讨论】:

    • 是的,它有效!谢谢你。我们如何实现双向连接?
    • 是的,我现在进去了!谢谢你解释清楚?
    • 假设有两个节点 A 和 B,对于双向连接 b/w A 和 B,A 的下一个应该指向节点 B,B 的上一个应该指向 A。因此,现在你可以通过 A=A->next 从节点 A 遍历到节点 B,通过 B=B->previous 从 B 遍历到 A。
    猜你喜欢
    • 2011-09-24
    • 2015-09-24
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2014-08-27
    相关资源
    最近更新 更多