gpt4 book ai didi

C++ 双链表 prev 指针

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:41 26 4
gpt4 key购买 nike

最近我开始学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com