Deleting linked list

D

Daniel Vukadinovic

OK, here's the deal.
I'd like to delete the list.Here's what I do:

node* p_temp = p_beginning;

while (p_beginning != 0)
{
p_beginning = p_beginning->p_next;
delete p_beginning;
}

p_temp = 0;

What's wrong? I found this scrable in my old notebook and I don't
understand why it has to be this way:

node* p_temp = p_beginning;
while (p_beginning != 0)
{
p_temp = p_beginning;
p_beginning = p_beginning->p_next;
delete p_beginning;
}

p_beginning = 0;

Can someone explain?
 
P

Phlip

Daniel said:
OK, here's the deal.
I'd like to delete the list.Here's what I do:

node* p_temp = p_beginning;

while (p_beginning != 0)
{
p_beginning = p_beginning->p_next;
delete p_beginning;
}

What's wrong is the first delete hits the second item in the list. The first
one never got deleteted.
p_temp = p_beginning;
p_beginning = p_beginning->p_next;

delete p_temp;

Debug the function and watch the values change. (Then learn to use
std::vector<>, and occassionally std::list<>. Part of learning to program
is learning to build raw data structures, and part is learning how to find
and use appropriate libraries.)
 
A

Abdo Haji-Ali

node* p_temp = p_beginning;

while (p_beginning != 0)
{
p_beginning = p_beginning->p_next;
delete p_beginning;
}

p_temp = 0;
First the p_temp is useless, it doesn't do anything. Second the first
item never gets deleted as you start with the second item. Third, this
would simply crash, when the list reaches its end and p_beginning
becomes null you are trying to delete it before ending the loop...
node* p_temp = p_beginning;
while (p_beginning != 0)
{
p_temp = p_beginning;
p_beginning = p_beginning->p_next;
delete p_beginning;
this should be delete p_temp; which would solve the above three
problems
}

p_beginning = 0;
Useless as the above loop wouldn't end if p_beginning wasn't zero

Abdo Haji-Ali
Programmer
In|Framez
 
D

Daniel Vukadinovic

Thanks!

Why is p_temp = p_beginning done twice?

HERE --> node* p_temp = p_beginning;
while (p_beginning != 0)
{
AND HERE --> p_temp = p_beginning;
p_beginning = p_beginning->p_next;
delete p_beginning;
}

And what's the deal of assining the value of p_beginning to p_temp is
it's not used anywhere?
 
A

Abdo Haji-Ali

Daniel said:
Thanks!

Why is p_temp = p_beginning done twice?

HERE --> node* p_temp = p_beginning;
Well, this one is useless..
while (p_beginning != 0)
{
AND HERE --> p_temp = p_beginning;
This one is essential, because every execution of the loop the value of
p_beginning is changed to point to the next item in the list, and here
you assign it to p_temp in order to delete it later
p_beginning = p_beginning->p_next;
delete p_beginning;
Again this should be delete p_temp;
}

And what's the deal of assining the value of p_beginning to p_temp is
it's not used anywhere?
Sorry, I don't quite understand your question...

Abdo Haji-Ali
Programmer
In|Framez
 
P

Phlip

Daniel said:
Thanks!

Why is p_temp = p_beginning done twice?

HERE --> node* p_temp = p_beginning;

That's useless; it's just a mistake in the example.

Try this:

while (p_beginning != 0)
{
Thing * p_temp = p_beginning;
p_beginning = p_beginning->p_next;
delete p_temp;
}
And what's the deal of assining the value of p_beginning to p_temp is
it's not used anywhere?

Did you read the rest of my first post?

Read that block again. p_temp must hold the current item to be deleted,
while p_beginning holds the next item.
 
H

Howard

Abdo Haji-Ali said:
First the p_temp is useless, it doesn't do anything. Second the first
item never gets deleted as you start with the second item. Third, this
would simply crash, when the list reaches its end and p_beginning
becomes null you are trying to delete it before ending the loop...

That wouldn't cause it to crash. There's no problem calling delete on a
NULL pointer. The delete operator doesn't do anything if used on a NULL
pointer.

(Although, I suppose you could implement your own delete operator which DOES
crash!)

-Howard
 
A

Abdo Haji-Ali

Howard said:
That wouldn't cause it to crash. There's no problem calling delete on a
NULL pointer. The delete operator doesn't do anything if used on a NULL
pointer.
Interesting, I just tried an example of deleting a NULL pointer and it
didn't crash. I wonder if this is standard C++ or just another UD...

Abdo Haji-Ali
Programmer
In|Framez
 
J

Jaspreet

Abdo said:
First the p_temp is useless, it doesn't do anything. Second the first
item never gets deleted as you start with the second item. Third, this
would simply crash, when the list reaches its end and p_beginning
becomes null you are trying to delete it before ending the loop...
<snip>
Nope no crash. You can call delete on a null pointer. It is just a
no-op (no-operation).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top