簡體   English   中英

c ++雙鏈表prev指針

[英]c++ double linked list prev pointer

最近我開始學習c ++,我開始使用簡單的結構。 我已經在雙鏈表上苦苦掙扎,而且我已經卡在“prev”指針上; /“Next”指針運行良好,但“prev”dosen't並且我不知道為什么。

#ifndef _DOUBLELIST_HPP
#define _DOUBLELIST_HPP
#include<iostream>
#include<memory>

template <typename T>
class DoubleList{
    private:
        struct Node{
            T value;
            std::shared_ptr<Node> prev;
            std::shared_ptr<Node> next;

            Node(std::shared_ptr<Node> prevp=nullptr,std::shared_ptr<Node> nextp=nullptr):prev(prevp),next(nextp){};
            Node(T it,std::shared_ptr<Node> prevp,std::shared_ptr<Node> nextp):value(it),prev(prevp),next(nextp){};
        };

        std::shared_ptr<Node> head; //head is also header node as the first node of the list 
        std::shared_ptr<Node> tail; // but it have no element, is not considered to be an element
        std::shared_ptr<Node> curr; // of the list in that i don't chceck when list is empty
        size_t size;

        void clear(){
            while(curr->next)
                curr=std::move(curr->next);
            size=0;
        }

    public:
        DoubleList():size(0),head(std::make_shared<Node>()),curr(tail),tail(head){};


        void insert(const T it);//add node at the begin of a list
        void append(const T it);//add node at the end of a list
        void moveToStart(){curr=head;}
        void goNext();//shift position to a next item
        void goPrev();//shift position to a prev item

        template<typename U> friend std::ostream& operator<<(std::ostream&,const DoubleList<U>&);
}; 


template <typename T>
void DoubleList<T>::insert(const T it){//(item,prev,next)-> these are arguments of Node constructor
    curr->next=std::make_shared<Node>(it,curr,curr->next);// this line of code might be bugged
    if(tail==curr) tail=curr->next;
    ++size;
}

template <typename T>
void DoubleList<T>::append(T it){
   tail=tail->next=std::make_shared<Node>(it,tail,tail->next); //and this line of code might be bugged too
    ++size;
}

template <typename T>
void DoubleList<T>::goNext(){
     curr=curr->next;
     std::cout<<curr->value<<" next\n";
}

// this fun is not working well ;/
template <typename T>
void DoubleList<T>::goPrev(){
     curr=curr->prev;
     std::cout<<curr->value<<" prev\n";
}

template <typename U>
std::ostream& operator<<(std::ostream& os,const DoubleList<U>& d){
    auto temp=d.head.get();
    temp=temp->next.get();
    while(temp){
        os<<temp->value<<std::endl;
        temp=temp->next.get();
    }
    return os;
}

#endif



// and some simple tests

#include<iostream>
#include"doublelist.hpp"

typedef int myCheckType; 

void insertCheck(DoubleList<myCheckType>&);
void appendCheck(DoubleList<myCheckType>&);
void nextCheck(DoubleList<myCheckType>&);
void prevCheck(DoubleList<myCheckType>&);


int main(){
    DoubleList<myCheckType> l;
    insertCheck (l);
    appendCheck(l);
    nextCheck(l);
    prevCheck(l);
    std::cout<<l;
}

void insertCheck (DoubleList<myCheckType>& l){
    for(int i=0;i<4;++i)
        l.insert(i);
}

void appendCheck(DoubleList<myCheckType>& l){
    for(int i=20;i<25;++i)
        l.append(i);
}
void nextCheck(DoubleList<myCheckType>& l){
    for(int i=0;i<4;++i)
        l.goNext();
}

void prevCheck(DoubleList<myCheckType>& l){
    for(int i=0;i<3;++i)
        l.goPrev();
}

我已經把這個錯誤搞砸了。 這個解決方案不是很優雅,但現在效果很好:P

template <typename T>
void DoubleList<T>::insert(const T it){

    if(!size) tail=curr->next=std::make_shared<Node>(it,nullptr,curr->next);
    else curr->next=curr->next->prev=std::make_shared<Node>(it,curr,curr->next);
    ++size;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM