deleting 2D LinkList completely

This is a discussion on deleting 2D LinkList completely within the C++ Programming forums, part of the General Programming Boards category; Isthere any problem int the code (for deleting 2D LinkList completely) given below? Code: void CDataStructure::Destroy(void) { Node *traverseN = ...

  1. #1
    Registered User DeadlyWarrior's Avatar
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    5

    deleting 2D LinkList completely

    Isthere any problem int the code (for deleting 2D LinkList completely) given below?


    Code:
    void CDataStructure::Destroy(void)
    {
    	Node *traverseN = new Node();
    	Node *tempN     = new Node();
    	Edge *tempE     = new Edge();
    	Edge *traverseE = new Edge();
    
    	if(Node_Head != NULL)
    	{
    	traverseN = Node_Head;
    
    	while(traverseN != NULL)
    	{
    		if(traverseN->Link != NULL)
    		{
    			traverseE = traverseN->Link;
    
    			while(traverseE != NULL)
    			{
    				tempE = traverseE;
    				traverseE = traverseE->Next;
    				delete tempE;
    			}
    		}
    
    		tempN = traverseN;
    		traverseN = traverseN->Next;
    		delete tempN;
    	}
    	}
    
    }
    Expecting right answer specially from (Code Master )Elysia
    Last edited by DeadlyWarrior; 12-04-2009 at 05:56 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    *Moved to new thread*

    Quote Originally Posted by DeadlyWarrior
    Isthere any problem int the code (for deleting 2D LinkList completely) given below?
    You have memory leaks. As for correctness of the deletion itself: some may be able to guess what exactly you are trying to do, but for the less enlightened among us, it would help if you provided some background of what you are doing, perhaps also providing the relevant class definitions.
    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 C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A minor point is this:
    Code:
    	
    if(Node_Head != NULL)
    {
       traverseN = Node_Head;
       while(traverseN != NULL)
       {
    Why check Node_head of being NULL? You do that since you assign Node_head to traverseN and then check the later for being NULL. There is no harm done assigning Node_Head to traverseN if it is NULL, so this is pointless. Just delete the if statement completely.

  4. #4
    Registered User DeadlyWarrior's Avatar
    Join Date
    Dec 2009
    Location
    Pakistan
    Posts
    5
    [QUOTE=laserlight;912473]*Moved to new thread*


    I Have these two structs in one class may be this would help you in understanding the problem

    Code:
    struct Edge
    	{
    	CString Linked_MAC;
    	int     Weight;
    
                    Edge   *Next;  
    
    	}*Edge_Head;
    
              struct Node
    	{
    	CString MAC_Address;
    	Edge   *Link;
    
    	Node   *Next;
            
    	}*Node_Head;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D in directX
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 01-25-2009, 10:23 AM
  2. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 10:51 AM
  3. Confused Deleting 2D array
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2006, 11:36 AM
  4. Replies: 16
    Last Post: 09-22-2006, 03:39 PM
  5. Deleting characters from 2d arrays
    By `firefox in forum C Programming
    Replies: 4
    Last Post: 05-21-2005, 05:18 PM

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