120 lines
3.0 KiB
C
120 lines
3.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct students* reading();
|
|
struct students* sort_students(struct students* );
|
|
|
|
struct students{
|
|
char* name;
|
|
struct students* next_student;
|
|
};
|
|
int main()
|
|
{
|
|
int quantity;
|
|
char qty_buffer[10];
|
|
fgets(qty_buffer, sizeof(qty_buffer), stdin);
|
|
quantity = atoi(qty_buffer);
|
|
if(quantity<=0){
|
|
puts("Nespravny vstup");
|
|
return 0;
|
|
}
|
|
struct students* readed = reading();
|
|
if(readed==NULL){
|
|
puts("Ziadne prihlasky");
|
|
return 0;
|
|
}
|
|
struct students* sorted = sort_students(readed);
|
|
struct students* next_element=sorted;
|
|
int counter=0;
|
|
puts("Prijati studenti:");
|
|
while(next_element!=NULL){
|
|
if(counter==quantity){
|
|
puts("Neprijati studenti:");
|
|
}
|
|
counter++;
|
|
printf("%s",next_element->name);
|
|
next_element=next_element->next_student;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct students* reading(){
|
|
char buffer[100];
|
|
struct students* first_student =malloc(sizeof(struct students));
|
|
struct students* current_student=first_student;
|
|
struct students* last=NULL;
|
|
while(fgets(buffer, sizeof(buffer), stdin )){
|
|
if(strlen(buffer)==1){
|
|
break;
|
|
}
|
|
current_student->name=malloc(strlen(buffer) + 1);
|
|
strcpy(current_student->name, buffer);
|
|
current_student->next_student=malloc(sizeof(struct students));
|
|
last=current_student;
|
|
current_student=current_student->next_student;
|
|
}
|
|
free(current_student);
|
|
if(last==NULL){
|
|
return NULL;
|
|
}
|
|
last->next_student=NULL;
|
|
|
|
return first_student;
|
|
}
|
|
|
|
|
|
//A - B - C
|
|
//A - C
|
|
//A - C - B
|
|
|
|
//A - C - B - D - F
|
|
//B->next_student C
|
|
//B
|
|
struct students* sort_students(struct students* list_of_students_to_sort){
|
|
struct students* current_link=list_of_students_to_sort;
|
|
struct students* previous_element=NULL;
|
|
struct students* first_element=list_of_students_to_sort;
|
|
struct students* A;
|
|
struct students* B;
|
|
struct students* C;
|
|
struct students* D;
|
|
int counter=1;
|
|
while (counter!=0){
|
|
counter=0;
|
|
current_link=first_element;
|
|
previous_element=NULL;
|
|
while (current_link->next_student!=NULL){
|
|
A=previous_element;
|
|
B=current_link;
|
|
C=current_link->next_student;
|
|
D=current_link->next_student->next_student;
|
|
if(strcmp(B->name, C->name)==0){
|
|
counter++;
|
|
B->next_student=D;
|
|
free(C->name);
|
|
free(C);
|
|
break;
|
|
}
|
|
if(strcmp(B->name, C->name)>0){
|
|
counter++;
|
|
if(A!=NULL){
|
|
A->next_student=C;
|
|
}else{
|
|
first_element=C;
|
|
}
|
|
B->next_student=D;
|
|
C->next_student=B;
|
|
break;
|
|
}
|
|
previous_element = current_link;
|
|
current_link=current_link->next_student;
|
|
}
|
|
}
|
|
|
|
return first_element;
|
|
}
|
|
|
|
|
|
|