Exemple #1
0
int enqueue(Scheduler *pqueue, Process* process,compFunc* compare){
    Process *previous,*next,*temp;
    temp = pqueue->front;
    if(pqueue->length == 0 || (compare(&process->priority,&temp->priority) < 0))
        return frontInsertion(pqueue, process);
    for(;temp->next != NULL;temp = temp->next){
        previous = temp;
        next = temp->next;
        if(process->priority < next->priority){
            previous->next = process;
            process->next = next;
            return ++pqueue->length;
        }
    };
    temp->next = process;
    process->next = NULL;
    return ++pqueue->length;
}
void main()
{
	Node* list = 0;
	Node* secondList = 0;

	Student* stud = 0;

	FILE *f;
	f = fopen("Students.csv", "r");

	if (f)
	{
		stud = (Student*)malloc(sizeof(Student));

		// buffers
		char name[256];
		char birth[256];
		char faculty[256];

		// pattern: 1,John Smith,10-10-1990,Faculty of Computer Sciene,1,1008
		fscanf(f, "%d, %[^,], %[^,], %[^,], %hd, %hd",
			&(stud->id), name, birth, faculty, &stud->year, &stud->group);

		while (!feof(f))
		{
			stud->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
			strcpy(stud->name, name);

			char* token;

			// solving date field
			char separator[2] = "-";
			token = strtok(birth, separator);
			stud->birthDay.day = atoi(token);

			token = strtok(0, separator);
			stud->birthDay.month = atoi(token);

			token = strtok(0, separator);
			stud->birthDay.year = atoi(token);


			// solving name field
			stud->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
			strcpy(stud->name, name);

			// solving faculty
			stud->faculty = (char*)malloc((strlen(faculty) + 1) * sizeof(char));
			strcpy(stud->faculty, faculty);

			// INSERTION HERE:
			//list = frontInsertion(list, stud);
			list = rearInsertion(list, stud);
			secondList = frontInsertion(secondList, stud);

			// allocate new student && read next line from csv file
			stud = (Student*)malloc(sizeof(Student));
			fscanf(f, "%d, %[^,], %[^,], %[^,], %hd, %hd",
				&(stud->id), &name, &birth, &faculty, &stud->year, &stud->group);
		}
	}
	else
	{
		perror("[FILE ERROR]");
	}

	printList(list);
	searchStudent(list, "fate1");
	searchStudent(list, "fate2");
	searchStudent(list, "fate3");

	list = deleteStudent(list, "Max", 1);
	//list = deleteElement(list, "Philip Rafaello");
	printf("\nList after deletion of Max: ");

	printList(list);

	// search for a student in list
	Student* searched = searchElement(list, "John Smith");
	if (searched)
	{
		printf("\nStudent found: %s", searched->name);
	}
	else
	{
		printf("\nStudent not found!");
	}

	// interchange adjacent nodes in list
	list = interchangeNodesAdj(list, 3, 4);
	printf("\nList after adjacent interchange: ");
	printList(list);

	// interchange nodes in list
	list = interchangeNodes(list, 1, 5);
	printf("\nList after adjacent interchange: ");
	printList(list);

	// sort list by year
	printf("\nSorting the list: ");
	sortList(&list);
	printList(list);

	// list concatenation
	printf("\nFirst list: ");
	printList(list);
	printf("\nSecond list: ");
	printList(secondList);

	//Node* concatenatedList = concatenateList(list, secondList);
	//printf("\nResult: ");
	//printList(concatenatedList);

	deleteList(list);

	// testing some things
	int a = 0x5;

	a = 0xA;
	a = 0xB;
	a = 0xC;

	printf("\nHex: %x", a);


	// finally
	fclose(f);
	printf("\n");
}