I've commented out nonrelevant code statements from your post which would probably send you off on a tangent. Also, I've added comments indicating what coding is necessary to complete your assignment. A total of nine code statements are needed in the if/else statement to complete the task. Pay close attention to the comments in the if/else statement. And, yes, I've completed this assignment to verify my posts.
Code:
#include <stdio.h>
#include <stdlib.h>
struct stud_info
{
char fname[20] ;
char lname[20] ;
int rank ;
float score ;
struct stud_info* prev,* next ;
} ;
int main(void)
{
FILE* infile = NULL;
struct stud_info* pnew_rec = NULL;
struct stud_info* link2 = NULL;
struct stud_info* top = NULL;
struct stud_info* bottom = NULL;
struct stud_info* move_pntr = NULL;
struct stud_info* move_pntr2 = NULL;
infile = fopen("p2data.txt", "r") ;
// pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
// fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) ;
// pnew_rec->next = NULL ;
// top = pnew_rec ;
// bottom = pnew_rec ;
while( !feof(infile) )
{
pnew_rec = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ;
if(fscanf( infile, "%s%s%d%f", pnew_rec->fname , pnew_rec->lname, &pnew_rec->rank, &pnew_rec->score ) != 4)
break;
// pnew_rec->next = top ;
// top is NULL, so this is first record read
if(top == NULL)
{
// You'll need four code statements here to accomplish the following:
// We'll assign the first record read to the top pointer.
// Since this is the first record read, the bottom pointer will also point to this record
// The new rcord prev and next point should be pointing to NULL since there are no
// previous and next records
}
else{
// You'll need five code statements here to accomplish the following:
// We'll assign the bottom pointer to the temporary pointer called link2
// Next we'll assign this new record read to the next pointer of link2
// Since there are no records following the new record, we'll assign NULL to the
// next pointer of the new record.
// The prev pointer of the new record should be be set to link2 which was the previous
// record at the end of the list
// Finally, the bottom pointer should now be set to the new record read
}
//top = pnew_rec ;
// link2 = (struct stud_info*) malloc( sizeof( struct stud_info ) ) ; //get block of mem for link 2
// fscanf( infile, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
// link2->prev = bottom ;
// bottom = link2 ;
}
move_pntr = top ;
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 ;
}
move_pntr2 = bottom ;
while(move_pntr2 != NULL)
{
printf( "%15s %15s %4d %5.2f \n", move_pntr2->fname , move_pntr2->lname, move_pntr2->rank, move_pntr2->score ) ;
move_pntr2 = move_pntr2->prev ;
}
fclose(infile);
return (0) ; //end of func main
}