1
最近、私はC++を学び始めました。シンプルな構造で遊び始めました。私は二重リンクリストで苦労していると私は "前の"ポインタに固執している/ "次の"ポインタはうまく動作しますが、 "前の" dosen'tと私は理由を知らない。C++ダブルリンクリスト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();
}
あなたのコメント '//このコード行は盗聴されるかもしれません。デバッガを使用して、一度に1つだけ修正してください。あなたの 'prevCheck'は、他のすべての関数がどのように記述されているかによって部分的に壊れています。 – AndyG
私はvimで書いていますが、まだgdbと友好関係にありません:Pしかし、私はそれを試みます。 – Klemens
vimでC++を学ぶ?あなた自身がデバッガを使ってIDEを手に入れよう! – user2647513