hello all, im having some trouble trying to figure out the problem and why this program is not working correctly. I have two linklists built from two infiles. Some of the names in my first list are contained in my second and i want to delete them using my second function. Any feedback would be much appreciated. Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct info_node
{
char fname[20] ;
char lname[20] ;
int rank ;
float score ;
struct info_node * next ;
} ;
struct info_node * build_list(FILE *) ;
void data_clean(struct info_node *, struct info_node *) ;
int main()
{
struct info_node* link1 = NULL, * link2 = NULL ;
struct info_node* top = NULL ;
struct info_node* move_pntr = NULL, * move_pntr2 = NULL ;
FILE * infile ;
FILE * infile2 ;
infile2 = fopen("p3purge_data.txt", "r") ;
infile = fopen("p3data.txt", "r") ;
link1 = build_list(infile) ;
link2 = build_list(infile2) ;
data_clean(link1, link2) ;
move_pntr = link1 ;
while(move_pntr != NULL)
{
printf( "%15s %15s %4d %5.2f \n", move_pntr->fname , move_pntr->lname, move_pntr->rank, move_pntr->score ) ;
move_pntr = move_pntr->next ;
}
return (0) ;
} //end of func main()
struct info_node * build_list(FILE * infile)
{
struct info_node* top = NULL ;
struct info_node* pnode = NULL ;
struct info_node* pre_pntr ;
pnode = (info_node *)malloc(sizeof(info_node) ) ;
fscanf( infile, "%s%s%d%f", pnode->fname , pnode->lname, &pnode->rank, &pnode->score ) ;
pnode->next = NULL ;
top = pnode ;
while( !feof(infile) )
{
pnode = (info_node *)malloc(sizeof(info_node) ) ;
fscanf( infile, "%s%s%d%f", pnode->fname , pnode->lname, &pnode->rank, &pnode->score ) ;
pnode->next = top ;
top = pnode ;
}//end of !feof
fclose(infile);
return(top);
}
void data_clean(struct info_node * link1, struct info_node * link2)
{
struct info_node* pre_pntr ;
struct info_node* current_pntr ;
struct info_node* shortlist ;
struct info_node* longlist ;
struct info_node* clean_link ;
longlist = link2 ;
shortlist = link1 ;
while(shortlist != NULL)
{
pre_pntr = longlist ;
current_pntr = shortlist ;
while(current_pntr != NULL)
{
if(strcmp(current_pntr->fname, shortlist->fname) && (current_pntr->lname, shortlist->lname) == 0)
{
if (current_pntr->next == NULL)
{
pre_pntr->next = current_pntr->next ;
current_pntr = pre_pntr ;
//break;
}
else if(current_pntr == pre_pntr)
{
current_pntr = current_pntr->next ;
}
else
{
pre_pntr->next = current_pntr->next ;
current_pntr = current_pntr->next ;
}
}// end of if strcmp
else
{
pre_pntr = current_pntr ;
current_pntr = current_pntr->next ;
}
}//end of while loop
shortlist = shortlist->next ;
}
return ;
} //end of func data_clean()