Calculator + LinkedList

This is a discussion on Calculator + LinkedList within the C++ Programming forums, part of the General Programming Boards category; i'v tryed to creat new node if fFirst is empty but got a problem to test if fFirst is empty ...

  1. #16
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    i'v tryed to creat new node if fFirst is empty but got a problem to test if fFirst is empty

    Code:
    void LinkedList::push_back (CalcCommand* data)
    {
    	
    	if (isEmpty())
    	{
    		fFirst->setNext(data);
    	}
    	else
    	{
    		CalcCommand* temp =fFirst;
    
    		while (temp->getNext() != 0)
    		{
    		    temp = temp -> getNext ();//to let temp point to the last node in the list
    		}
    		
    		temp -> setNext (data);//this will add data to the end of the list
    		data -> setNext (0); //and this will set the one after the end of the list to NULL
    	}
    }

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (isEmpty())
    	{
    		fFirst->setNext(data);
    	}
    If this is in fact empty, which by definition means that 'fFirst' is null, then how can you be calling 'fFirst->setNext(data)'? You're trying to access a null pointer there. That's bad.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Quote Originally Posted by quzah
    Code:
    if (isEmpty())
    	{
    		fFirst->setNext(data);
    	}
    If this is in fact empty, which by definition means that 'fFirst' is null, then how can you be calling 'fFirst->setNext(data)'? You're trying to access a null pointer there. That's bad.

    Quzah.
    hi quzah
    but it's the same when you creat a new object you set the pointers to NULL then you let them point to what ever

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point. If a pointer 'fFirst' is in fact null, then you cannot dereference it to access one of its members. Thus, 'fFirst' has no 'setNext()' function, because you have no instance of 'fFirst'. Create an instance, make 'fFirst' point to it, and then you can access 'fFirst->whatever'. But until you have an instance that you're actually pointing to, you cannot use its members (unless they're static, but that's another issue entirely).

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    Quote Originally Posted by Stoned_Coder
    read xeraths comment. it says it all. You use a list thats not there. fFirts is NULL on first use so temp is also NULL. This is your problem.
    yes Stoned_Coder
    i think that's it
    there is no LinkedList object , so i have to creat it it first , but the my problem now that in the Main function i call Calculator Object ,, but i don't get it to start the program using LinkedList Object
    thanks

  6. #21
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    void LinkedList::push_back (CalcCommand* data)
    {
    	
    	if (isEmpty())
    	{
    		fFirst = new Node(...); //this's what's missing
    		fFirst->setNext(data);
    	}
    	else
    	{
    		CalcCommand* temp =fFirst;
    
    		while (temp->getNext() != 0)
    		{
    		    temp = temp -> getNext ();//to let temp point to the last node in the list
    		}
    		
    		temp -> setNext (data);//this will add data to the end of the list
    		data -> setNext (0); //and this will set the one after the end of the list to NULL
    	}
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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