Question about delete

This is a discussion on Question about delete within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to implement a priority queue (minheap) using pointers and a struct for my nodes for an ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Question about delete

    Hi, I am trying to implement a priority queue (minheap) using pointers and a struct for my nodes for an assignment and I was wondering about something. My class supports a function deleteMin() which basically gets the root address which I have stored and deletes the data and key and replaces them with those of the last node on the tree and then rearranges the tree from top to bottom with comparisons. My question is this: As far the last node is concerned from which I took the data I change the pointer of its father to him to NULL so basically that node is disconnected from the tree. However that memory is not freed, right? It's still associated with the program? If it is I was wondering how I can free it so that memory address can be used in the future. I was thinking to use the command delete [pointer to ex-last node] but this will just erase the pointer, right?

    I apologise for being so confusing

    example of code
    Code:
    /*lst is a ponter to struct node and currently points to the last most right node of the tree*/
    
    setLstNode(lst->father->l_ch);  //Function assigns the new last node to be the right child 
                                                      //of the father of the ex-last node 
    lst->father->r_ch=NULL;           //assigning NULL to right child of father
    delete lst;                                 //This probably doesnt do a thing right?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    More like:
    Code:
    node* temp = lst->father->r_ch; // temp points to the last node
    setLstNode(lst->father->l_ch);  // set the new last node
    delete temp;                    // deallocate memory for former last node
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    hmm thanks. After this that address contains random data so it must work, however I still need

    lst->father->r_ch=NULL;

    because I use the fact if it has a null pointer when going through the tree from top to bottom...
    By the way cant I just delete lst then , I mean both lst and temp show to the same address...
    Anyway thanks laserlight.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Sorry, I am more familiar with the array implementation of min/max heaps.

    By the way cant I just delete lst then , I mean both lst and temp show to the same address...
    lst points to your min node, right? If you delete lst, then lst->father is invalid. It may still exist in memory, but you can no longer reach it from lst. As such, you should save a pointer to the min node in a temporary, and then delete it when you are ready.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Aha, okay. Thanks a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  2. Quick question about new and delete
    By megatron09 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2006, 12:20 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Class Bank(i have a question)
    By kuwait in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 07:40 AM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21