Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code for destructor of linked list

32 views
Skip to first unread message

Paul

unread,
May 3, 2015, 5:52:02 AM5/3/15
to
No, this is not for a homework or class assignment. I am trying to learn this stuff independently.

The below is my attempt to write a destructor for a linked list. I'm suspicious of it because it seems different and somewhat simpler than the answers I've seen online. I'd be grateful if someone could tell me if there's a problem with it. Thank you.

Paul

struct LinkedList
{
int data;
LinkedList* next;
~LinkedList();
};

LinkedList::~LinkedList()
{
while(next)
{

LinkedList* head = next;
next = head->next;
delete head;

}

}

Victor Bazarov

unread,
May 3, 2015, 11:35:17 AM5/3/15
to
There is no "problem" with it, if it works (theoretically) and
accomplishes what you mean it to accomplish. However, as you can
probably judge, it's recursive, and that could mean a problem if your
linked list grows to be too long for the execution environment to
handle. You're going to learn about it eventually, so you might start now.

Recursion is usually organized using the process' stack (automatic
storage), which is often limited. In order to avoid putting too much
stress on the stack, it is advisable to replace recursion with a loop
whenever possible. I'll leave it to you to convert this recursive
function into a proper loop.

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Paavo Helde

unread,
May 3, 2015, 12:05:52 PM5/3/15
to
Paul <peps...@gmail.com> wrote in
news:58beac82-633c-4f2d...@googlegroups.com:
It seems you are trying to delete everyhing twice ('delete head' already
deletes head->next recursively so there is no need for the while loop).
Double-deleting a pointer is illegal, so the program is invalid. On the
other hand, deleting a NULL pointer is a well-defined non-op, so in
principle you could have even simpler constructor:

LinkedList::~LinkedList()
{
delete next;
}

However, this is a recursive call, which may cause troubles (stack
overflow) with huge lists, so a while loop would be in principle better.
Alas, your variant of it was invalid.

Cheers
Paavo

Melzzzzz

unread,
May 3, 2015, 12:06:40 PM5/3/15
to
On Sun, 03 May 2015 11:35:03 -0400
Victor Bazarov <v.ba...@comcast.invalid> wrote:

> On 5/3/2015 5:51 AM, Paul wrote:
> > No, this is not for a homework or class assignment. I am trying to
> learn this stuff independently.
> >
> > The below is my attempt to write a destructor for a linked list. I'm
> suspicious of it because it seems different and somewhat simpler than
> the answers I've seen online. I'd be grateful if someone could tell me
> if there's a problem with it. Thank you.
> >
> > Paul
> >
> > struct LinkedList
> > {
> > int data;
> > LinkedList* next;
> > ~LinkedList();
> > };
> >
> > LinkedList::~LinkedList()
> > {
> > while(next)
> > {
> >
> > LinkedList* head = next;
> > next = head->next;
> > delete head;
> >
> > }
> >
> > }
>
> There is no "problem" with it, if it works (theoretically)

It does not works...
0 new messages