Reasons why a for statement using a self-made bidirectional list is not spinning

Asked 2 years ago, Updated 2 years ago, 90 views

Nice to meet you.
This is my first time asking a question.

I am currently working on making my own bi-directional lists using pointers in C++ without using STL.
In the midst of this, I have written to the point where I add elements to the end, but even if I try to put them in a for sentence, they will skip it (not processed).

I tried to find out what caused it using breakpoints, but it didn't become NULL, and I couldn't solve it.

I have little experience with C++ and may have overlooked something about pointers, but could someone please teach me?

BidirectionalList.h

// bidirectional list class
template<typename T>
class BidirectionalList
{
public:
    BidirectionalList();
    T*begin();
    T*end();
    void push_back(T pushData);
private:
    template<typename T>
    // Structure of each element
    structure ListItem
    {
        T*data;
        ListItem*prev;
        ListItem*next;
        ListItem():
            data(nullptr),
            prev(nullptr),
            next(nullptr){}
        ListItem(T*newData):
            data(newData),
            prev(nullptr),
            next(nullptr){}
    };
    ListItem<T>*head;
    ListItem<T>*tail;
    ListItem<T>*nowItem;
};

BidirectionalList.cpp

template<typename T>
BidirectionalList<T>:: BidirectionalList():
    head(new ListItem<T>(),
    tail(new ListItem<T>()) ,
    nowItem(head)
{}

template<typename T>
void BidirectionalList<T>: push_back(T pushData)
{
    ListItem<T>*newItem=newListItem<T>(&pushData);
    nowItem->next=newItem;
    newItem->prev=nowItem;
    newItem->next=tail;
    tail->prev=newItem;
    nowItem = newItem;
}

template<typename T>
T* BidirectionalList <T>::begin()
{
    ListItem<T>*item=nowItem;
    while (item->prev!=head)
    {
        item=item->prev;
    }
    return item ->data;
}
template<typename T>
T* BidirectionalList <T>::end()
{
    ListItem<T>*item=nowItem;
    while (item->next!=tail)
    {
        item=item->next;
    }
    return item ->data;
}

In Use

BidirectionalList<IHoge*>objList;
// New class inherited from IHoge
objList.push_back(new HogeA());
objList.push_back(new HogeB());
objList.push_back(new HogeC());
objList.push_back(new HogeD());
for (IHoge* obj:objList)
{
    // Not called
    obj->HogeFunc();
}

c++ pointer stl

2022-09-30 21:21

1 Answers

 for (IHoge*obj:objList)
{
    // Not called
    obj->HogeFunc();
}

Before you use Range-based for, you may notice the discrepancy if you write it in the traditional for.

for(IHoge*obj=objList.begin();obj!=objList.end();++obj){
    obj->HogeFunc();
}

The problem is that ++obj.Incremented is not ListItem but stored T*.
This is why STL also returns iterators instead of raw pointers.

In order to achieve a practical bidirectional list, you should also consider releasing ListItem or T* that you have created during the operation.


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.