C++ 双链表 prev 指针

标签 c++ linked-list

最近我开始学习 C++,并且开始玩简单的结构。我在双链表上苦苦挣扎,我被困在“prev”指针上;/“Next”指针工作正常但“prev”不工作,我不知道为什么。

#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;
}

关于C++ 双链表 prev 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39005581/

相关文章:

C++删除没有括号的数组内存仍然有效吗?

c++ - 将数据保存和加载到文件 C++(初学者)

html - 为什么有两个事件链接?

c++ - 使用模板将堆栈实现为链表时出现 "symbols not found"错误

c++ - 为什么 std::map 在按值传递给 lambda 时表现异常?

c++ - 在 ')' token 之前应为 '*'

c++访问单链表中的特定值

c++ - 段错误 : Bubble sort on link list

c - 为什么释放内存会导致段错误?

c++ - 通过它自己的条件变量c++控制每个线程