By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

Deleting linked list

P: n/a
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?

Apr 12 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Daniel Vukadinovic wrote:
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.)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #2

P: n/a
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

Apr 12 '06 #3

P: n/a
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?

Apr 12 '06 #4

P: n/a

Daniel Vukadinovic wrote:
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

Apr 12 '06 #5

P: n/a
Daniel Vukadinovic wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #6

P: n/a
Thank you guys, that clarifies everything!

Apr 12 '06 #7

P: n/a

"Abdo Haji-Ali" <ah***@inframez.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
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...


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
Apr 12 '06 #8

P: n/a

Howard wrote:
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

Apr 12 '06 #9

P: n/a

"Abdo Haji-Ali" <ah***@inframez.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

Howard wrote:
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...


That's a requirement of the C++ language standard.

From the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-16.8


Apr 12 '06 #10

P: n/a

Abdo Haji-Ali wrote:
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...

<snip>
Nope no crash. You can call delete on a null pointer. It is just a
no-op (no-operation).

Apr 13 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.