Thread: deleting 2D LinkList completely

  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 06:56 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:23 AM
  2. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 11: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