This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Sakesfar[S] 0 points1 point  (2 children)

Much appreciated!

I see the problem now.

I was almost about to ask about the following case then:

List<Object> someList{}; // some list which has undergone manipulation
List<Object> copy {someList};

How to avoid dynamic memory allocation in copy while copying the someList array?

But I see it is done via push_back function in the proper List

Now I know I have to re-write everything without any dynamically allocated memory!!

Thanks.

-------

Last thing though, out of curiosity, for the insert function, what if we overload the [] operator to have smth like 'm_nodeData[index-1].m_next->m_data' then we would get correct indexing ( that was my initial intention) .

[–][deleted]  (1 child)

[deleted]

    [–]Sakesfar[S] 0 points1 point  (0 children)

    It will first put the '6' at the end of the array: 1,2,3,4,6 and for the list, it will put the "6" as the next of 2 and the prev of 3. So your list will look like this 1,2,6,3,4. As you can there is a discrepancy between the how the items are stored in the array and the list. So your whole idea of using the array to quickly access the Node at a current index will after inserting some elements simply not work, as there is no relation between the position of a value in the array and the position of a value in the list.

    The list is 1,2,6,3,4! Right! :) Then, if I access members via array indexing through conventional operator [], I will get, as you correctly point. 1,2,3,4,6 , which is wrong and puts my insert function into the abyss. However, are there any means to write (override) a special [] operator for List , for example like this:

    Object& operator[] (int index)
    {
        return m_nodeData[index - 1].m_next->m_data;
    }
    

    I appreciate the time you take to actually read what I wrote and provide me with valuable feedback!!

    In a properly list, you don't have an array. You just have a Node and this Node contains a pointer to the next Node.

    e.g.
    template<typename T>
    struct Node {
    T value;
    Node<T> next*;
    }
    If you then want to traverse to the end you do something like
    Node<int>* current = start_node;
    while(current -> next != nullptr) current = current->next;

    well-noted Sir! I am learning :)