int main (void) { FILE *test; char name[20]; int grade; Student S; Tree T; Initialize(&T,copyStudent,destroyStudent,compareStudents); printf("Initialize()\n"); printf("Size=%d, Height=%d, ",Size(&T),Height(&T)); if(Balanced(&T)) printf("Balanced=YES\n\n"); else printf("Balanced=NO\n\n"); test=fopen("test.txt","r"); while(fscanf(test,"%s %d",name,&grade)==2) { InitializeStudent(name,grade,&S); Insert(&T,&S); printf("Insert(%s,%d)\n",NameOfStudent(S),GradeOfStudent(S)); printf("Size=%d, Height=%d, ",Size(&T),Height(&T)); if(Balanced(&T)) printf("Balanced=YES\n\n"); else printf("Balanced=NO\n\n"); FreeStudent(&S); } fclose(test); Minimum(&T,&S); do { printf("%s\t%d%%\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); } while(Successor(&T,&S)); Destroy(&T); return EXIT_SUCCESS; }
static int compareStudents (void *X, void *Y) { int i, j; i=GradeOfStudent(*(Student*)X); j=GradeOfStudent(*(Student*)Y); if(i>j) return -1; if(i==j) return 0; return 1; }
int compareStudents(void * S1, void * S2) { Student * student1 = (Student *)S1; Student * student2 = (Student *)S2; if (GradeOfStudent(*student1) > GradeOfStudent(*student2)) return 1; else if (GradeOfStudent(*student1) == GradeOfStudent(*student2)) return 0; else return -1; }
static int equalItems (Item X, Item Y) { /* Why couldn't we write the following? if(strcmp(NameOfStudent(X),NameOfStudent(Y))!=0 || GradeOfStudent(X)!=GradeOfStudent(Y)) return 0; else return 1; */ int i; char *s; s=(char *)malloc(strlen(NameOfStudent(X))+1); strcpy(s,NameOfStudent(X)); i=strcmp(s,NameOfStudent(Y)); free(s); if(i!=0 || GradeOfStudent(X)!=GradeOfStudent(Y)) return 0; else return 1; }
static void * copyStudent (void *Y, void *X) { if(Y==NULL) Y=malloc(sizeof(Student)); if(Y!=NULL) InitializeStudent(NameOfStudent(*(Student*)X), GradeOfStudent(*(Student*)X), Y); return Y; }
void * copyStudent(void * S1, void * S2) { Student * student1 = (Student *)S1; Student * student2 = (Student *)S2; if (student1 == NULL) { student1 = malloc(sizeof(Student)); } InitializeStudent(NameOfStudent(*(Student *)student2),GradeOfStudent(*(Student *)student2),student1); return (void *)student1; }
static void showListContent (List *L) { int i; Student S; for(i=0;i<Size(L);i++) { Peek(i,L,&S); printf("\t%s %d%%\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); } }
void InitializeStudent (char *name, int grade, Student *S) { strncpy(S->name,name,MAXNAMESIZE-1); S->name[MAXNAMESIZE-1]='\0'; S->grade=grade; #ifdef DEBUG if(strcmp(name,NameOfStudent(*S))!=0 || GradeOfStudent(*S)!=grade) { printf("Violated postcondition for InitializeStudent!\n"); exit(EXIT_FAILURE); } #endif }
static void showQueueContent (Queue *Q) { int i; Student S; for(i=0;i<Size(Q);i++) { Head(Q, &S); printf("\t%s %d%%\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); } for(i = 0;i<Size(Q);i++) { Tail(Q, &S); FreeStudent(&S); } }
int main (int argc, char* argv[]) { FILE *test; char s[20]; int i; Student S; Heap H; i=strtol(argv[1],NULL,10); Initialize(&H,i,copyStudent,destroyStudent,compareStudents); test=fopen("test.txt","r"); while(fscanf(test,"%s %d",s,&i)==2) if(!Full(&H)) { InitializeStudent(s,i,&S); Insert(&H,&S); FreeStudent(&S); } else { Top(&H,&S); if(i>GradeOfStudent(S)) { Remove(&H); FreeStudent(&S); InitializeStudent(s,i,&S); Insert(&H,&S); FreeStudent(&S); } } fclose(test); while(!Empty(&H)) { Top(&H,&S); Remove(&H); printf("%s %d\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); } Destroy(&H); return EXIT_SUCCESS; }
int main() { char studentName[40]; int studentGrade; Student * curStudent = NULL; FILE * studentFile = fopen("test.txt","r"); Tree theTree; int first = 0; int shouldContinue = 1; /* initialize tree */ Initialize (&theTree, (©Student), (&destroyStudent), (&compareStudents)); printf("Initialize()\n"); getTreeInfo(&theTree); while (fscanf(studentFile,"%s %d",studentName,&studentGrade)==2) { curStudent = malloc(sizeof(Student)); InitializeStudent(studentName,studentGrade,curStudent); printf("Insert(%s,%d)\n", studentName,studentGrade); Insert(&theTree,curStudent); getTreeInfo(&theTree); } fclose(studentFile); while (shouldContinue == 1) { void * theStudent = malloc(sizeof(Student)); Student * printStudent; if (first == 0) { shouldContinue = Minimum(&theTree,theStudent); if (shouldContinue == 0) break; first++; } else { shouldContinue = Successor(&theTree,theStudent); if (shouldContinue == 0) break; } printStudent = (Student *)theStudent; printf("%s \t%d%%\n", NameOfStudent(*printStudent),GradeOfStudent(*printStudent)); free(theStudent); } Destroy(&theTree); return 0; }
static void showQueueContent (Queue *L) { int i; Student S; int realHead = L->head; //loop transverses through queue via head for(i=0;i<Length(L);i++) { //circulates array if maxsize is reached if (L->head == MAXLISTSIZE) { L->head = 0; } Head(L,&S); //gets head of queue printf("\t%s %d%%\n",NameOfStudent(S),GradeOfStudent(S)); FreeStudent(&S); L->head++; } L->head = realHead; //fixes the head }
static void copyItem (Item *Y, Item X) { InitializeStudent(NameOfStudent(X),GradeOfStudent(X),Y); }