Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

C++ double linked list z c++11

$
0
0

Cześć ! Uczę się c++ od jakiegoś czasu i postanowiłem zaimplementować podstawowe struktury.
I tu mam problem z double linked list. Dodawanie działam wskaźnik next też działa  itp. ale nie wiem dlaczego mi nie działa wskaźnik prev. Wydaje mi się że to wszystko jakoś logicznie zaimplementowałem ale widocznie gdzieś jest błąd i nie mogę go znaleść od dłuższego czasu. ;/
Bardzo proszę o pomoc.

 

#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=nullptr,std::shared_ptr<Node> nextp=nullptr):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
        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){};

        //add at the begin of a list
        void insert(const T it);

        //add at the end of a list
        void append(const T it);
        void moveToStart(){curr=head;}

        //shift position to the next item
        //if there is no item , it will stay on the last item
        void goNext();

        //shift position to the prev item
        //if there id no item , it will stay on the last item
        void goPrev();
        T remove();

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


template <typename T>
void DoubleList<T>::insert(const T it){
    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




// i tutaj main z prostymi testami 
#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;
    //l.getCurr();
    //.print2();

  //  std::cout<<l;
  //  l.print2();

}

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

 


Viewing all articles
Browse latest Browse all 21942