Thread: proble with an empty linked list queue

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    proble with an empty linked list queue

    hi iv got a problem every time i try to remove a pice of data from my queue when it is already empty it crashes cmd and i dont understand why i was wondering if anyone could help me

    is is menu driven here are the relevent sections

    Code:
    case 2:
    			strcmp(szReg,leave(&taxiQueue));
    			if(szReg == NULL)
    			{
    				printf("there are no taxis in the queue to leave");
    			}
    			else
    			{
    			 printf("\nTaxi %s was removed from the queue",szReg);
    			}
    			break;
    and the function

    Code:
    char *leave(TaxiQueue *tq)
    {
    	Taxi **front = &tq->pFront;
    	Taxi **back  = &tq->pBack;
    
    	char *reg = malloc(sizeof(char) * 20);
    
    	// Test if the queue is empty
    	if((*front) == NULL && (*back) == NULL)
    	{
    		// Nothing in queue
    		return NULL;
    	}
    	else if((*front) == (*back))
    	{
    		// The front and back are the same
    		strcpy(reg,(*front)->szReg);
    		// Delete the taxi
    		free((*front));
    		// Set the front and back to null
    		(*front) = NULL;
    		(*back) = NULL;
    
    		tq->iLength--;
    
    		return reg;
    	}
    	else
    	{
    		strcpy(reg,(*front)->szReg);
    		// Remove from the front
    		(*front) = (*front)->pNext;
    		free((*front)->pPrev);
    		(*front)->pPrev = NULL;
    
    		tq->iLength--;
    
    		return reg;
    	}
    
    	return NULL;
    }
    i think iv got a case of looking at it so long my brain imagines that theres nothing wrong lol

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you call strcmp on a NULL pointer, bad things will (and did!) happen.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't use then ask if it's OK to use. In C, it's not easier to ask forgiveness than it is to ask permission.


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

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by quzah View Post
    Don't use then ask if it's OK to use. In C, it's not easier to ask forgiveness than it is to ask permission.


    Quzah.
    lol, good one quzah!

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    Quote Originally Posted by quzah View Post
    Don't use then ask if it's OK to use. In C, it's not easier to ask forgiveness than it is to ask permission.


    Quzah.

    what ?

    is that ment to help me ?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( ptr != NULL ) /* something's being pointed at... */
    {
        *ptr = blahblah;
    }
    Do that, instead of:
    Code:
    *ptr = blahblah;
    if( ptr != NULL )
        ...yay?
    Basically, before you ever start fiddling with what is being pointed at, you need to make sure you're actually pointing AT something.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lockless Threaded Queue
    By fguy817817 in forum C Programming
    Replies: 27
    Last Post: 11-18-2009, 03:25 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM