Deleting linked list

Discussion in 'C++' started by Daniel Vukadinovic, Apr 12, 2006.

  1. 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?
    Daniel Vukadinovic, Apr 12, 2006
    #1
    1. Advertising

  2. Daniel Vukadinovic

    Phlip Guest

    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!!!
    Phlip, Apr 12, 2006
    #2
    1. Advertising


  3. > 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
    Abdo Haji-Ali, Apr 12, 2006
    #3
  4. 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?
    Daniel Vukadinovic, Apr 12, 2006
    #4
  5. 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
    Abdo Haji-Ali, Apr 12, 2006
    #5
  6. Daniel Vukadinovic

    Phlip Guest

    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!!!
    Phlip, Apr 12, 2006
    #6
  7. Thank you guys, that clarifies everything!
    Daniel Vukadinovic, Apr 12, 2006
    #7
  8. Daniel Vukadinovic

    Howard Guest

    "Abdo Haji-Ali" <> wrote in message
    news:...
    >
    >> 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
    Howard, Apr 12, 2006
    #8
  9. 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
    Abdo Haji-Ali, Apr 12, 2006
    #9
  10. Daniel Vukadinovic

    Howard Guest

    "Abdo Haji-Ali" <> wrote in message
    news:...
    >
    > 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-lite/freestore-mgmt.html#faq-16.8
    Howard, Apr 12, 2006
    #10
  11. Daniel Vukadinovic

    Jaspreet Guest

    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).
    Jaspreet, Apr 13, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    463
    emerth
    Jul 10, 2003
  2. Kevin
    Replies:
    1
    Views:
    369
    Richard Bos
    Feb 24, 2004
  3. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  5. sara
    Replies:
    4
    Views:
    1,252
    Michael Doubez
    Feb 15, 2011
Loading...

Share This Page