473,699 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10231
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******** *************@u 72g2000cwu.goog legroups.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******** *************@u 72g2000cwu.goog legroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2732
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 in el:
5
859
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
1
1585
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
4598
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 potential pitfalls I'd be extremely grateful. Cheers Steve
25
3866
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 is important that I iterate through the list backwards, because the items in it have to be processed in reverse order before erasing. However, there does not appear to be an std::list::erase() method defined for reverse iterators.
3
3011
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> #include <iostream> using namespace std;
7
2240
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 the nth value enetered in the list, in constant time. how do i do that? i got the address of the element to be deleted from the vector but how do i relate that address with the element in the list and then delete it and rearrange the pointers for the...
4
2844
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 written a program in c++ in linked list but am facing two problems. (1) when i delete an item that is not stored the program hangs and (2) I cannot modify the program to insert the node at the back i can only do it to insert at the front. here is the...
42
4546
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 (struct lista *list, int m, char *s, struct lista *previous) ......... Not problem-related code goes here....
7
5769
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8885
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.