Results 1 to 6 of 6

Thread: Problem in LinkedList destructor.

  1. #1
    Join Date
    Aug 2009
    Posts
    58

    Problem in LinkedList destructor.

    Hello to all,
    I have write following LinkedList destructor, but there is one problem in it. I want to know, is this a valid LinkedList destructor?
    Code:
    LinkedList::~LinkedList()
     {
       ListNode *ptrs;
    
       for (ptrs = heads; heads; ptrs = heads)
       {
         heads = heads->next
         delete ptrs;
       }
    }
    I don't know whether it is right or not. Can anyone tell me what is problem in LinkedList destructor?
    Please help me.
    Last edited by KAIRU26; 15-02-2010 at 06:51 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in LinkedList destructor.

    You have to use while-loop instead of for loop to fix this problem. It is very simple to use. I have written following code to you. Just try to understand it.
    Code:
    ListNodes* currents = heads;
    while( currents != 0 ) {
        ListNodes* nexts = currents->next;
        delete currents;
        currents = next;
    }
    head = 0;
    In the above code I have use one variable to linked list value.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problem in LinkedList destructor.

    Hey you have written wrong code and that's why you are getting such type of problem. I have written following code for you. It is working program. In the following code I have use ~CLists() to define destructor. I also have use pPointers = pRoots; to sets the pointer to start at the beginning of the list.
    Code:
    CLists::~CLists() // destructor
    {
    	CNode *pPointers;
    
    	pPointers = pRoots; 
    	do
    	{
    		CNodes *pPointersDels; 
    		
    		pPointersDels = pPointers;
    		delete pPointerDels;
    
    		if(pPointers->pNexts != 0)
    		{
    			pPointers = pPointers->pNexts; 
    		}
    	} while(pPointers != pHead);
    
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem in LinkedList destructor.

    As per my information you have to use if-else statement instead of for statement to fix this problem. I have written following destructor program using if-else statement. Just try to understand it.
    Code:
    CLists::~CLists() // destructor
    {
    	do
    	{
    	CNodes *pPointers;
    
    	pPointers = pRoots; 
    
    	if(pRoots->pNexts == 0) 
    	{
    		delete pRoots->pNexts;
    		delete pRoots;
    		delete pPointers;
    	}
    	else
    	{
    		pRoots = pRoots->pNext;
    		delete pPointers; 
    	}
    	} while(pRoots);

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Problem in LinkedList destructor.

    You have to use condition "head;" to check that it is not null. As per my information "head" is same as "head != null". You have write wrong code and that's why you are getting such type of problem. You have unnecessarily used for statement and that's why you are getting such type of problem. You have to use standard library to fix this problem.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Problem in LinkedList destructor.

    You have to first use following code in your program to fix this problem. It is very simple one.
    Code:
    heads(32) -> [54]singles_nodes -> [0]ends-ofs-lists.
    After this try to run following code

    for (ptrs = heads; headss != 0; ptr = head)

Similar Threads

  1. sorting linkedlist
    By duper in forum Software Development
    Replies: 1
    Last Post: 05-06-2011, 03:30 AM
  2. What is the difference between a destructor and a finalizer?
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 10:47 AM
  3. Basic use of destructor
    By Sacchidananda in forum Software Development
    Replies: 3
    Last Post: 19-11-2009, 10:20 AM
  4. Destructor in PHP5
    By Duck in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 12:01 AM
  5. Destructor in Java
    By Anti_Trend in forum Software Development
    Replies: 7
    Last Post: 25-10-2008, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,999,764.40247 seconds with 17 queries