I have a linked list inside an array of structs:
My question is, when I go to print out the contents of each array member, what is the syntax of printing out the linked list contents with them?Code:typedef struct Course { char Number [NumSize + 1]; char Section [SecSize + 1]; int Units; struct Course *Next; } Course_T; typedef struct Stud { char SSN[SSNsize+1]; char Class; float GPA; int Units; int NumCourses; Course_T* CourseList; } Student_T; Student_T StudentList[ListMax];
Heres the whole mess so far, no error correction etc.
Any help would be appreciated. Thank You.Code:#include <ctype.h> #include <stdlib.h> #include <stdio.h> #include <string.h> /* for string comparisons */ #define False 0 /*False indicator*/ #define NotFound -1 /* indicator for student not found in list */ #define SSNsize 11 /* Size of SSN string */ #define ListMax 100 /* student list max length */ #define LineLength 80 /* Max length of input line */ #define NumSize 5 /*Max Length of Course Number*/ #define SecSize 3 /*Max Length of Section Number*/ typedef struct Course { char Number [NumSize + 1]; /*Course Number*/ char Section [SecSize + 1]; /*Course Section*/ int Units; /*Course Credits*/ struct Course *Next; /*Next Course pointer*/ } Course_T; typedef struct Stud { /* Student Records */ char SSN[SSNsize+1]; /* SS# array of char */ char Class; /* Student Classification */ float GPA; /* Grade Point Average */ int Units; /* # of units */ int NumCourses; /* Number of classes being taken */ Course_T* CourseList; /* Ptr to list of classes */ } Student_T; /* Struct Type of Student*/ Student_T StudentList[ListMax]; /* list of student data */ /********************************/ main() { /* Start of Function prototypes */ int ReadList (Student_T*, char*); void DisplayStudent (int StudentNum, Student_T* StudentList); /* End of Function prototypes */ char File[LineLength]; int StudentNum; /* Number of students in array at start */ char response; /* Menu Choice response */ int Index = 0; /* position of student file found in array */ /** Beginning of Program **/ StudentNum = ReadList (StudentList,File); /* Pass address of 1st element of array */ /* Return # of records read into array */ DisplayStudent(StudentNum,StudentList); system("PAUSE"); return 0; } /*************************************************************/ int ReadList (Student_T *StudentList, char *name) { char line[LineLength+1]; /* Line read from file */ int CurrNum = 0; /* Index into array for current student */ char filename[LineLength]; /* Data file name */ int i; FILE *data; /* Data file pointer */ Student_T *Curr; /* Pointer to current student (student being read) */ Course_T *New, /* Used to allocate memory for new nodes */ *First = NULL, /* Ptr to First node in list */ *Last = NULL; /* Ptr to Last node in list */ /* First and Last set to NULL to indicate empty list */ do { printf ("\nEnter data filename: "); scanf ("%s", filename); data = fopen (filename, "r"); strcpy (name,filename); } while (data == NULL); while ( fgets (line, LineLength+1, data) != NULL ) /* read each student's data */ { Curr = &StudentList[CurrNum]; sscanf (line, "%s %c %f %d %d",Curr->SSN,&(Curr->Class),&(Curr->GPA),&(Curr->Units),&(Curr->NumCourses)); if ((Curr->NumCourses) == 0) (Curr->CourseList = NULL); else { for(i=0;i<(Curr->NumCourses);i++) { printf ("In For loop i is:%d Curr->NumCourses is %d\n", i,Curr->NumCourses ); New = (Course_T*) malloc ( sizeof (Course_T) ); /* allocate new node space */ if (New == NULL) printf ("Heap error - could not allocate memory\n"); else { /* Memory allocated successfully */ fgets (line, LineLength+1, data); sscanf (line, "%s %s %d", &New->Number, &New->Section, &(New->Units)); /* Put data into new node */ printf ("Number is %s \n Section is %s\n Units are %d\n\n", &New->Number, &New->Section, New->Units); New->Next = NULL; /* Insert new node into the list */ if (First == NULL) { /* If list is empty... */ Last = New; /* point both pointers to new node */ First = New; } else { /* If list is not empty... */ Last->Next = New; /* Point Next ptr in last node to new node */ Last = New; /* and also point Last ptr to new node */ } } /* end else where New != NULL */ } /* end for */ } /* End else *First not null */ CurrNum++; } fclose (data); return CurrNum; } /*************************/ int DisplayStudent (int StudentNum, Student_T* StudentList) /* Display with Num Courses */ { int Num,i; /* Loop counter */ char Class1[14] = "Undergraduate"; char Class2[9] = "Graduate"; Student_T* Stud1_ptr; /* Ptr to student record being displayed */ Stud1_ptr = StudentList; /* Ptr to first record in array */ Course_T* Course_ptr; Course_ptr = First; printf ("\nSocial Security Number Classification GPA Units Number Courses\n"); //Define this as Header printf ("---------------------- -------------- --- ----- --------------\n"); // use dash = '-' & formatting for (Num = 0; Num < StudentNum; Num++) /* Print list of SSNs */ { if (Stud1_ptr->Class == 'U') // this is ugly try to fix logic printf ("%15s %20s %5.2f %3d %9d\n",Stud1_ptr->SSN,Class1,Stud1_ptr->GPA,Stud1_ptr->Units,Stud1_ptr->NumCourses); if (Stud1_ptr->NumCourses > 0) for(i=0;i<(Stud1_ptr->NumCourses);i++) { //This needs to point to CourseList Walk the list I guess // but how? printf ("%s %s \n", Stud1_ptr->(Course_ptr->Number),Stud1_ptr->(Course_ptr->Section)); //WRONG else printf ("%15s %15s %10.2f %3d %9d\n",Stud1_ptr->SSN,Class2,Stud1_ptr->GPA,Stud1_ptr->Units,Stud1_ptr->NumCourses); Stud1_ptr++; /* point to next record */ } printf ("\n"); }



LinkBack URL
About LinkBacks


