Thread: searching a linked list

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

    searching a linked list

    I am having a problem with this search function
    It compares the first node fine. But I am not sure what kind of statement to use to compare the target with the second node and so on
    I realize current = current->next; won't work because the error I recieve is "left of 'next' specifies undefined struct/union 'node'"
    Does anyone know what I can do to fix this problem?
    Code:
    int Search_List(Header *psList, int z)
    {
    	int x; 
    	char answer[40];
    	struct node* current = psList->psHead;
    	printf("\n\nWhat City or Zipcode would you like to search for?");
    	scanf("%s",&answer);
    	if (isalpha(answer[0]) == 0)
    	{
    		//search for zip
    		current = psList->psHead;
    	
    		for(x=0;x<z;x++)
    		{
    			
    			if(strcmp(answer, current,5)== 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
    			{
    		current = current->next;//problem is here
    		
    			}
    		}
    	}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct node {
        struct node *next;
        int data;
        char dataasstring[BUFSIZ];
    } *list;
    
    ...stuff...
    
    struct node *n;
    
    for( n = list; n; n = n->next )
    {
        if( n->data == testvalue )
            ...found it...
        if( !strcmp( n->dataasstring, teststring ) )
            ...found it...
    }
    Two simple comparisons for you to take a look at. One for strings, one for numbers. Take your prick.

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

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    thanks quzah. I changed it a little but I am still having trouble with it.
    Assuming Header struct looks like this
    Code:
    typedef struct
    {
    struct node_d *psHead;
    struct node_d *psTail;
    struct node_d *current_node;
    int node_count;
    }Header;
    I figured this would work, but doesn't. Can you see any flaws in it ?
    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);
    			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");
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM