Double Linked List Problem

This is a discussion on Double Linked List Problem within the C++ Programming forums, part of the General Programming Boards category; Hi i have a taxi queue program with a double linked list with a pointer at thre front and end ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Double Linked List Problem

    Hi i have a taxi queue program with a double linked list with a pointer at thre front and end of the queue

    the problem im having is when ever i enter a taxi wether it be the 2nd or 3rd or 10th taxi in the list it will change all the other taxi registration numbers in the list to the same registration

    any help would be greatful

    removing taxis from the queue works perfect and this setback is limiting from building the rest of the program

    Code:
    /* Declare a taxi structure with pointers for suitable for a linear double */
    /* linked list. */
    struct TAXI_STRUCT
    {
    	char* pszRegistrationNo;
    	TAXI_STRUCT* pNext;
    	TAXI_STRUCT* pLast;
    };
    
    /* Declare the pointers to track the start and end of the list. */
    TAXI_STRUCT* pFirstTaxi = NULL;
    TAXI_STRUCT* pLastTaxi = NULL;
    
    
    /* Function to add a taxi to the list. */
    void AddLastTaxi(char* pszRegistrationNo)
    {
    
    	TAXI_STRUCT* pNewTaxi = new TAXI_STRUCT;
    	pNewTaxi->pszRegistrationNo = pszRegistrationNo;
    	if (pFirstTaxi == NULL && pLastTaxi == NULL)  /* If queue has nothing in it */
    	{
    		pNewTaxi->pLast = NULL;
    		pNewTaxi->pNext = NULL;
    		pFirstTaxi = pNewTaxi;
    		pLastTaxi = pNewTaxi;
    	}
    	else
    	{
    		pNewTaxi->pLast = NULL;
    		pNewTaxi->pNext = pLastTaxi;
    		pLastTaxi->pLast = pNewTaxi;
    		pLastTaxi = pNewTaxi;
    	}
    	
    }
    Menu for the program
    Code:
    int main(int argc, char* argv[])
    {
    	int menu = 0;
    	char RegistrationNo[] = "";
    
    
    /* Loop for Menu */
    while ( menu != 8 )
    {
    
    
    	/* Menu for the Queue Displayed to the User */
    	printf("-------------------------------\n");
    	printf("Please Select an option from the menu.\n");
    		printf("1. arrive:	Add a taxi to the end of the queue.\n");
    		printf("2. leave:	Remove the first taxi in the queue.\n");
    		printf("3. search:	Search for a taxi within the queue.\n");
    		printf("4. print:	Print the Taxi queue.\n");
    		printf("5. front:	Display the taxi at the front of the queue.\n");
    		printf("6. back:	Display the taxi at the back of the queue.\n");
    		printf("7. empty:	Display if the queue is Empty or not.\n");
    		printf("8. exit:	Exit the program.\n");
    		printf("-------------------------------\n");
    	scanf("%d",&menu);				/* Read Input for menu from the User */
    	
    
    	switch (menu)                  /* Menu System for the Taxi Rank */
        {
        case 1:
    		/* Enter Taxi Registration Number to the Queue */
    		printf("Please enter the Taxi Registration Number that has joined the queue.\n");
    		scanf("%s", RegistrationNo);		
    		AddLastTaxi(RegistrationNo);             /* Add a Taxi to the End of the Queue*/
    		printf("The Following Taxi has joined the queue: %s \n", RegistrationNo);
            break;
        case 2:							/* Remove the First Taxi from the Queue */
    		printf("Removing %s\n", RemoveFirstTaxi());
            break;
        case 3:							/* Search list for a taxi registration Number */
            break;
        case 4:							/* Print the Taxi Queue in the Current State */
            PrintTaxiQueue();
            break;
    	case 5:							/* Which Taxi is at the Front of the Queue */
    		break;
    	case 6:							/* Which Taxi is at the Back of the Queue */
    		break;
    	case 7:							/* Display if the queue is Empty or Not */
    		break;
    	case 8:							/* Exit the program */
    								
    		break;
    	}
    
    }
    	printf("Thank you for using the program Goodbye....");
    	getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Right, I was going to say that I was sure that your code would not compile, then I noticed that it is actually C++.

    Moved to C++ programming forum.

    EDIT:
    Now, onto the question proper. The first thing I recommend is that you get rid of those global pointers, and instead create a taxi queue struct that has those pointers as members.

    By the way, avoid fully uppercase names for non-macro names. I suggest that you rename TAXI_STRUCT to Taxi or CTaxi (some people like to use a 'C' prefix to indicate that the name is a class name, but I personally don't use such a naming scheme).
    Last edited by laserlight; 03-10-2009 at 07:01 AM.
    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
    Join Date
    Mar 2009
    Posts
    3
    ok. moving the pointers into the strut gave me compile errors saying

    Error 21 error C2227: left of '->pNext' must point to class/struct/union/generic type

    etc.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Shiggins
    ok. moving the pointers into the strut gave me compile errors saying
    You created a new struct type, right? I might name it TaxiQueue, e.g.,
    Code:
    struct TaxiNode
    {
        char* pszRegistrationNo;
        TaxiNode* pNext;
        TaxiNode* pLast;
    };
    
    struct TaxiQueue
    {
        TaxiNode* pFirstTaxi;
        TaxiNode* pLastTaxi;
    };
    Of course, this means that your AddLastTaxi() function should either become a member function (if you have learnt them), or:
    Code:
    void AddLastTaxi(TaxiQueue* taxi_queue, char* pszRegistrationNo);
    It might be better to rename pLast to pPrevious.

    If you are allowed to use the standard library freely, then I suggest that you do not reinvent the wheel but instead use std::list for a doubly linked list, or better yet, use std::queue for your taxi queue.

    Incidentally, although it is debatable, I suggest that you avoid Hungarian notation.
    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

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    ahhh ok i get you now sorry i got confused will try that out now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. swapping nodes in double linked list
    By a0161 in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 06:12 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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