1

I am not too versed in C++, but I had the task of designing and implementing a linked list. This is what I handed in:

template <typename T>
struct Node
{
    Node() : next(nullptr) {}
    Node(T const & val) : next(nullptr), value(val) {}

    Node * next;
    T value;
};

template <typename T>
class LinkedList
{
    public:
        LinkedList()
        {
            begin_ = new Node<T>;
            current_ = begin_;
        }
        ~LinkedList()
        {
            delete begin_;
        }

        void insert(T const & value)
        {
            Node<T> * node = new Node<T>(value);
            node->next = current_->next;
            current_->next = node;
            current_ = current_->next;
        }

        void remove()
        {
            Node<T> * tmp = current_->next;
            if(!end())
            {
                current_->next = current_->next->next;
            }
            delete tmp;
        }

        bool end() const
        {
            return current_->next == nullptr;
        }

        void reset()
        {
            current_ = begin_;
        }

        void advance()
        {
            if(!end())
            {
                current_ = current_->next;
            }
        }

        T get() const
        {
            return current_->next->value;
        }

    private:
        Node<T> * begin_;
        Node<T> * current_;
};

I passed the assignment, but my teacher underlined the delete begin_ in the destructor ~LinkedList() and wrote "This leaks!" next to it. I have been thinking about how delete begin_ could possibly leak but I still do not understand. Can someone please help me with this?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
mel-
  • 533
  • 4
  • 11

3 Answers3

2

You have to go through every node in the list and delete. Hold a separate pointer to next, delete current, then move forward and continue deleting until next pointer is null.

~LinkedList()
{
    Node * current = begin_;
    Node * aNext = begin_->next;

    while (null != aNext){
        delete(current);
        current = aNext;
        aNext = current->next;
    }
}

Something like this. Don't know where you're getting begin_ from, but........

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • Oh, I've been completely stuck on the line he underlined, that I didn't even think of the possibility he could be talking about the other nodes. Thanks a lot. – mel- Jun 17 '12 at 19:13
  • NO problem whatsoever :-) glad to help. Also, might think to "accept" your best answer, this will help you later also when you have more questions. – trumpetlicks Jun 17 '12 at 19:14
  • Yes, of course; it would not let me accept an answer for a certain amount of minutes though! – mel- Jun 17 '12 at 19:23
0

In the destructor of Node it should send the delete command to next so that it frees up all nodes in the list.

Luke
  • 560
  • 6
  • 26
0

I think you will understand it better this way:

~LinkedList()
{
  reset();
  while(!end()) remove();
}
Alexander
  • 23,432
  • 11
  • 63
  • 73