I got the search problem figured out, and now am applying the same idea to a delete node function
but the function fails to return to the main menu after it is done
Code:
//Node structure
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;
//Header structure
typedef struct
{
struct node_d *psHead;//might change this to node_d
struct node_d *psTail;
struct node_d *psCurrent;
int node_count;
}Header;
Code:
int Delete_Data(Header *psList,Node *list,Node *node, int z)
{
int x, n = 1, indicator = 1;
char answer[40], delete_[1], confirmation[1];
strcpy(confirmation,"y");
node = psList->psHead;
printf("\n\nWhat City or Zipcode would you like to delete?");
scanf("%s",&answer);
if (isdigit(answer[0]) != 0)
{
//search for zip
for(x=0;x<z;x++)
{
if( strncmp( answer,(node->zipcode), 6) == 0 )
{
system("cls");
printf("\nZipcode = %s City = %s State = %s",node->zipcode, node->city,node->state);
printf("\nPopulation = %d Longitude = %-.2f latitude = %-.2f\n",node->population,node->longitude, node->latitude);
indicator = 0;
printf("\nAre you sure you want to delete this record?");
printf("\npress 'y' for yes and 'n' for no\n");
scanf("%s",&delete_);
if(strcmp(delete_, confirmation) == 0)
{
node->next->previous = node->previous;
node->previous->next = node->next;
free(node);
printf("node deleted");
n = 0;
}
}
node = node->next;
}
if(indicator !=0)
{
printf("\nRecord not found\n\n");
}
}
else
{
//search for city
for(x=0;x<z;x++)
{
if( strncmp( answer,(node->city), 6) == 0)
{
system("cls");
printf("\nZipcode = %s City = %s State = %s",node->zipcode, node->city,node->state);
printf("\nPopulation = %d Longitude = %-.2f latitude = %-.2f\n",node->population,node->longitude, node->latitude);
indicator = 0;
printf("\nAre you sure you want to delete this record?");
printf("\npress 'y' for yes and 'n' for no\n");
scanf("%s",&delete_);
if(strcmp(delete_, confirmation) == 0)
{
node->next->previous = node->previous;
node->previous->next = node->next;
//free(node);
printf("node deleted");
}
}
node = node->next;
}
if(indicator !=0)
{
printf("\nRecord not found\n\n");
}
}//end else
return 0;
}
You guys have any ideas on why it does this?
The last thing I get is the printed message "node deleted"
and then crash