Thread: Adding Node - Double Linked List

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Adding Node - Double Linked List

    I'm trying to write a deck to use in a blackjack game, but am having trouble with the adding cards functions. After I add all of them, I traversed the list to check what's wrong, and for each card I print out the mpPrevious, mValue, mSuit, and mpNext. The Suit, Value, and Next card pointer are all fine, but the Previous member pointer is always 0 (null), instead of being the address of the previous card. Please help me find what is wrong...

    Thank you,
    Sean Michael Simonsen

    Code:
    void Deck::AddCardToTop( Card* pNewCard )
    {
    
    	++mNumberOfCards;
    
    	if( !mpBottomCard )
    	{
    		
    		mpBottomCard = pNewCard;
    
    		mpTopCard = pNewCard;
    	
    	}
    
    	else
    	{
    
    		Card* Temp = mpTopCard;
    
    		mpTopCard = pNewCard;
    
    		mpTopCard->mpNext = Temp;
    
    		Temp->mpPrevious = mpTopCard;
    
    	}
    
    }
    
    void Deck::AddCardToBottom( Card* pNewCard )
    {
    
    	++mNumberOfCards;
    
    	if( !mpTopCard )
    	{
    		
    		mpBottomCard = pNewCard;
    		
    		mpTopCard = pNewCard;
    	
    	}
    	
    	else
    	{
    
    		Card* Temp = mpBottomCard;
    
    		mpBottomCard = pNewCard;
    
    		mpBottomCard->mpPrevious = Temp;
    
    		Temp->mpNext = mpBottomCard;
    
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I recommend STL.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Thank you, I fixed it. The problem was not in those functions, but elsewhere. The reason I thought the previous pointers were null is because I kept printing them with respect to the Top Card, so it doesn't have a preceding one. I'm not using STL because it only does a void return type for removing nodes (as far as I know), and since it's a deck of cards I'll want to shuffle it using a real-life algorithm, plus I need the practice in making my own double linked list.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. You do not need to shuffle the deck. Use a randomized algorithm to access a node. In other words, you shuffle during the deal instead of before it.

    Yes. You will gain experience.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  2. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 05:41 PM
  3. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 08:52 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM