-
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
}
}
}
-
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.
-
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");
}