Thread: Searching Double Linked lists

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    Searching Double Linked lists

    Assuming I have these two structures for a double linked list
    Code:
    typedef struct node_d
    {
    char city[40];
    char state[3];
    char zipcode[6];
    double longitude;//double
    double latitude;//double
    long population;//long
    struct node_d *previous;
    struct node_d *next;
    }Node;
    
    typedef struct
    {
    struct node_d *psHead;
    struct node_d *psTail;
    struct node_d *current_node;
    int node_count;
    }Header;

    I am trying to implement a search.
    I figured this would work, but doesn't. Can anyone see any flaws in it ? I am confused on how to fix this problem

    Code:
    int Search_List(Header *psList, int z)
    {  
    	int x; 
    	char answer[40], compare[40];
    	
    	printf("\n\nWhat City or Zipcode would you like to search for?");
    	scanf("%s",&answer);
    	if (isdigit(answer[0]) != 0)
    	{
    		//search for zip
    		psList->current_node = psList->psHead;
    		for(x=0;x<psList->node_count;x++)
    		{
    			strcpy(compare, psList->current_node->zipcode);/////////it seems to crash right here
    			if(strcmp(answer, compare)== 0)
    			{
    				system("cls");
    				printf("\nThe Zipcode was found with %d comparisons",x+1);
    				printf("\nZipcode = %s   City = %s   State = %s",psList->psHead->zipcode, psList->psHead->city, psList->psHead->state);
    				printf("\nPopulation = %d  Longitude = %-.2f  latitude = %-.2f\n",psList->psHead->population, psList->psHead->longitude, psList->psHead->latitude);
    				x=z;
    			}
    			else
    			{
    				psList->current_node = psList->current_node->next;
    				
    			}
    		}
    		printf("\nRecord was not found\n");
    	}
    it seems to crash right at this command
    strcpy(compare, psList->current_node->zipcode);
    but I don't know why. Is there a problem with the way strcpy is called?
    Any help would be appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd redo the loop personally. Just use a pointer and increment it. It's much easier to read.
    Code:
    Node *ptr;
    for( ptr = psList->head; ptr; ptr = ptr->next )
    {
        if( strcmp( answer, ptr->zipcode ) == 0 )
        {
            ...match...
        }
        ...else continue so no need for else...
    }
    Make sure your zipcode string is null terminated if you're treating it as a string. Make sure the buffer you copy it into also is null terminated. (Either memset it, or do it when you declare it). Also, you may want to use 'strncpy' instead, and specify to copy 5 bytes instead of just a standard strncpy which doesn't care if it runs off the end of your buffer.

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

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    Thanks again quzah, but I changed it to this
    and it is still crashing right at the if statement
    It does not even go through the for loop once.

    Code:
    int Search_List(Header *psList,Node *ptr, int z)
    {  
    	int x; 
    	char answer[40], compare[40];
    	
    	printf("\n\nWhat City or Zipcode would you like to search for?");
    	scanf("%s",&answer);
    	if (isdigit(answer[0]) != 0)
    	{
    		//search for zip
    		psList->current_node = psList->psHead;
    		
    		for( ptr = psList->psHead; ptr; ptr = ptr->next )
    		{
    			
    			
    		if( strcmp( answer,ptr->zipcode) == 0 )///////crashes here
    			{
    				
    				system("cls");
    				printf("\nThe Zipcode was found with %d comparisons",x+1);
    				printf("\nZipcode = %s   City = %s   State = %s",psList->current_node->zipcode, psList->current_node->city, psList->current_node->state);
    				printf("\nPopulation = %d  Longitude = %-.2f  latitude = %-.2f\n",psList->current_node->population, psList->current_node->longitude, psList->current_node->latitude);
            
    			}
       
    
    		}
    return 0;
    }
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd debug your list first if I was you. Make a function that just goes through the list and prints the entire thing to the screen. If that works, you know the list is fine. If it doesn't, you'll know where you problem is.

    From there, it's only a minor modification to get the same function to now search for something in the list.

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

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    Am I mistaken or should this print?

    printf("Zipcode is %s",ptr->zipcode);

    the statement comes in main before my search function. But it crashes at this statment

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by AmazingRando
    Am I mistaken or should this print?

    printf("Zipcode is %s",ptr->zipcode);

    the statement comes in main before my search function. But it crashes at this statment
    As long as ptr is pointing to a valid struct, it should print. As it's crashing, I expect it isn't pointing to the right place. Did you assign ptr a value?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. Double Linked lists
    By Jamsan in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 09:46 AM
  5. double linked lists
    By susyb in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2001, 06:09 AM