【问题标题】:c++ double linked list prev pointerc++ 双链表 prev 指针
【发布时间】:2016-08-17 20:19:53
【问题描述】:

最近我开始学习 c++,并且开始玩简单的结构。我在双链表上苦苦挣扎,我被困在“上一个”指针上;/“下一个”指针工作得很好,但“上一个”却没有,我不知道为什么。

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

【问题讨论】:

  • 你的 cmets // this line of code might be bugged 是正确的。尝试使用调试器一次修复一件事。您的 prevCheck 部分由于所有其他函数的编写方式而损坏。
  • 我正在用 vim 写东西,但我还没有和 gdb 成为朋友 :P 但是我当然会尝试。
  • 用vim学习c++???为自己准备一个带有调试器的 IDE!
  • 使用vim 代替IDE 并非闻所未闻。我用vimc,但又一次cc++简单得多。
  • 我喜欢 vim。但这并不能阻止您使用 gdb 的图形前端(例如 ddd)

标签: c++ linked-list


【解决方案1】:

我已经修复了那个错误。这个解决方案不是很优雅,但现在效果很好: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;
}

【讨论】:

    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多