From 4e0d6240040e4982bd31b564da747f0e22df9077 Mon Sep 17 00:00:00 2001 From: kr820js Date: Sat, 20 Apr 2024 18:45:06 +0200 Subject: [PATCH] submition1 --- cv10/program.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 cv10/program.c diff --git a/cv10/program.c b/cv10/program.c new file mode 100644 index 0000000..93afdab --- /dev/null +++ b/cv10/program.c @@ -0,0 +1,125 @@ +#include +#include +#include + +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 )){ + 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; +} + +void printer(struct students* head) +{ + puts("==========================="); + struct students* next_element=head; + while(next_element!=NULL){ + printf("%s",next_element->name); + next_element=next_element->next_student; + } + puts("========== END ============"); +} +