473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deleting linked list

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
10 10210
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
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
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

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
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
Thank you guys, that clarifies everything!

Apr 12 '06 #7

"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

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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
1
by: Kevin | last post by:
Hi, How can I enumerate a linked list while being hable to delete any number of elements while enumerating ? I was using: struct st { ... struct st *prev;
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
25
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
3
by: Daz01 | last post by:
How do I delete records? Ive written a program that can add new records and I can display what I've entered, but how do I select certain records to delete? Code below #include <cstdlib>...
7
nabh4u
by: nabh4u | last post by:
hi, i have a double linked list containing some elements and i have a vector which stores the address of the elements in the list (pointer to the list). i want to delete a value from the list,like...
4
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have...
42
by: Avon | last post by:
Hello there. I have a problem regarding node deletion in singly-linked lists in C. What happens is that I can't hold the previous node efficiently. Here is the code I use. struct lista *search...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.