Exemplo n.º 1
0
int main(void) {
	FILE *test;
	char s[20];
	int grade;
	
	Student S;
	Queue Q; 
	
	Initialize(&Q);
	showQueueSize(&Q);
	showQueueContent(&Q);
	
	test=fopen("test.txt","r");
	while(fscanf(test,"%s",s)==1) {
		if(strcmp(s,"Enqueue")==0) {
			fscanf(test,"%s %d",s,&grade);
			InitializeStudent(s,grade,&S);
			Enqueue(S, &Q);
			FreeStudent(&S);
		}
		if(strcmp(s,"Dequeue")==0)
			Dequeue(&Q);
		showQueueSize(&Q);
		showQueueContent(&Q);
	}
	
	fclose(test);
	Destroy(&Q);
	return EXIT_SUCCESS;
}
Exemplo n.º 2
0
int main(void) 
{
	FILE *test;
	char s[20];
	int grade;
	
	Student S;
	Queue Q; 
	
	Initialize(&Q);
	showQueueSize(&Q);
	showQueueContent(&Q);
	
	test=fopen("test.txt","r");
    //loop checks for a command in txt file either "Insert" or "Delete"
	while(fscanf(test,"%s",s)==1) 
    {
		if(strcmp(s,"Insert")==0) 
        {
			fscanf(test,"%s %d",s,&grade);
			InitializeStudent(s,grade,&S);
			Enqueue(S,&Q); //adds student from the text file
			FreeStudent(&S);
		}
		if(strcmp(s,"Delete")==0)
			Dequeue(&Q); //deletes student from queue
		showQueueSize(&Q);
		showQueueContent(&Q);
	}
	
	fclose(test);
	Destroy(&Q);
	return EXIT_SUCCESS;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
int main(void) {
	FILE *test;
	char s[20];
	int position, grade;
	
	Student S;
	List L; 
	
	Initialize(&L);
	showListSize(&L);
	showListContent(&L);
	
	test=fopen("test.txt","r");
	while(fscanf(test,"%s %d",s,&position)==2) {
		if(strcmp(s,"Insert")==0) {
			fscanf(test,"%s %d",s,&grade);
			InitializeStudent(s,grade,&S);
			Insert(S,position,&L);
			FreeStudent(&S);
		}
		if(strcmp(s,"Remove")==0)
			Remove(position,&L);
		showListSize(&L);
		showListContent(&L);
	}
	
	fclose(test);
	Destroy(&L);
	return EXIT_SUCCESS;
}
Exemplo n.º 6
0
/* Main Function */
int main (void) 
{
    FILE *fp;
    char name[20]; //name buffer
    int grade;      
    Student boyGirl;    //Student to be inserted
    Tree class;         //Binary tree
    
    Initialize(&class, *(&copyItem), (&destroyItem), (&compareItems));
    fp = fopen("test.txt", "r");
    if (fp == NULL) 
    {
        printf("Error opening file!!\n");
        exit(0);
    }
    
    /* While loop that reads data from test.txt and inserts it 
     * into the binary tree.
     */ 
    while (fscanf(fp, "%s %d",name ,&grade) == 2) 
    {
        InitializeStudent(name, grade, &boyGirl);
        Insert(&class, &boyGirl);
    }

    displayTree(&class);    //Displays tree contents in terminal
    Destroy(&class);        //Deletes the binary tree

    fclose(fp);

    return 0;
    
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
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, (&copyStudent), (&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;
}
Exemplo n.º 10
0
static void copyItem (Item *Y, Item X) {
	InitializeStudent(NameOfStudent(X),GradeOfStudent(X),Y);
}