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

Checking code to delete linked list

28 views
Skip to first unread message

Paul

unread,
May 9, 2015, 7:42:49 AM5/9/15
to
(No, this is not homework.) I'm attempting to write code to deallocate the memory of a linked list and set its head pointer to zero. My code compiles. However, I don't know of an easy way to test it.

I have two solutions, one recursive and one non-recursive. I'd be very grateful if someone could tell me if/how either have problems, or whether both solutions are ok.

Thank you!

struct List{
List* next;
int data;
};

// Deallocate the memory of a list and set the head to 0
void DeleteList(List*& head)
{
if(head)
DeleteList(head->next);

delete head;

head = 0;

}

// A non-recursive version
void deleteList(List*& head)
{
List* pDel = head;

while(pDel)
{
head = head->next;
delete pDel;
pDel = head;
}

head = 0;
}

Öö Tiib

unread,
May 9, 2015, 8:27:23 AM5/9/15
to
On Saturday, 9 May 2015 14:42:49 UTC+3, Paul wrote:
> (No, this is not homework.)

If it is not homework then better learn to use 'std::forward_list<int>'.
Standard library containers are more efficient and well tested than
self-made. Better learn to use other standard library containers
first since linked list is among least efficient container for majority
of container usages.

> I'm attempting to write code to deallocate
> the memory of a linked list and set its head pointer to zero. My
> code compiles. However, I don't know of an easy way to test it.

Tests are usually little command-line programs that only deal with
one or few closely related classes. Basically you can test that
"delete list" with 3 test calls: call the function with empty list,
call it with list that contains one element and call with list of
million of elements.

You can use a class with user-written constructors and destructor
as 'data' instead of 'int' so you can count in those that all of the
elements are added and removed like you did plan. Also it might be
worth to time that last call to find out its performance.

> I have two solutions, one recursive and one non-recursive. I'd be
> very grateful if someone could tell me if/how either have problems,
> or whether both solutions are ok.

The functions you posted seem OK to me.

It might be worth to note that abilities to test and to debug your
work yourself are very important skills to have. When you constantly
commit defective code then it will be noticed by your team-mates since
it is negative work. ;) Team would overall gain if you did look
YouTube instead of writing bad code and that is rather bad news to you.


Jorgen Grahn

unread,
May 9, 2015, 2:54:35 PM5/9/15
to
On Sat, 2015-05-09, 嘱 Tiib wrote:
> On Saturday, 9 May 2015 14:42:49 UTC+3, Paul wrote:
>> (No, this is not homework.)
>
> If it is not homework then better learn to use 'std::forward_list<int>'.

Or std::vector<int>, if he doesn't need the rarely used special
features linked lists give.

> Standard library containers are more efficient and well tested than
> self-made. Better learn to use other standard library containers
> first since linked list is among least efficient container for majority
> of container usages.

Oh, you covered that already ... of course. Good.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages