#define _CRT_SECURE_NO_WARNINGS #include #include #include #include typedef struct Student ST; struct Student { char ime[15]; char prezime[15]; int indeks; int orderNum; struct Student *next; }; ST *Alloc(ST *newitem); int Insert(ST *head, ST *newitem); int ReadFIle(ST *head); int Print(ST *head); int Kopiraj(ST *head, ST* sort); int Copy(ST *sort, ST* head); int SortInput(ST *sort, ST *newitem); int Brisi_iste(ST *sort); int Delete(ST *sort, ST *head); int DeleteList(ST *sort); int main(void) { ST head = { 0 }; ST sort_head = { 0 }; srand((unsigned)time); head.next = NULL; sort_head.next = NULL; //procitaj indeks,ime i prezime iz dtk ReadFIle(&head); // a. napravi novu listu i spremi studente + orderNum Kopiraj(&head, &sort_head); Print(&sort_head); // b. izbrisi studente s istim orderNum Brisi_iste(&sort_head); Print(&sort_head); //Izbrisi liste ( oslobodi mem ) DeleteList(&head); DeleteList(&sort_head); Print(&sort_head); return 0; } ST *Alloc(ST *newitem) { newitem = (ST*)malloc(sizeof(ST)); if (newitem == NULL) { printf("GRESKA1 !"); return NULL; } return newitem; } int Insert(ST *head, ST *newitem) { newitem->next = head->next; head->next = newitem; return 0; } int Print(ST *head) { head = head->next; if (head == NULL) { printf("Empty list\n"); return 0; } printf("Ispis liste:\n"); while (head != NULL) { printf("%d %s %s %d\n", head->indeks, head->ime, head->prezime, head->orderNum); head = head->next; } return 0; } int ReadFIle(ST *head) { FILE *fp = NULL; ST *newitem = NULL; fp = fopen("Studenti2.txt", "r"); if (fp == NULL) { printf("GRESKA2!"); return 0; } while (!(feof(fp))) { newitem = Alloc(newitem); fscanf(fp, "%d %s %s", &newitem->indeks, newitem->ime, newitem->prezime); newitem->orderNum = rand() % 5 + 1; Insert(head, newitem); } fclose(fp); return 0; } int Kopiraj(ST *head, ST* sort) { head = head->next; while (head != NULL) { Copy(sort, head); head = head->next; } return 0; } int Copy(ST *sort, ST* head) { ST *newitem = NULL; newitem = Alloc(newitem); newitem->indeks = head->indeks; newitem->orderNum = head->orderNum; strcpy(newitem->ime, head->ime); strcpy(newitem->prezime, head->prezime); SortInput(sort, newitem); return 0; } int SortInput(ST *sort, ST *newitem) { ST *tmp = NULL; tmp = sort; sort = sort->next; while (sort != NULL && strcmp(sort->prezime, newitem->prezime) < 0) { tmp = sort; sort = sort->next; } Insert(tmp, newitem); return 0; } int Brisi_iste(ST *sort) { ST *prev_sort = NULL; int i = 0; prev_sort = sort; sort = sort->next; while (sort != NULL) { i = Delete(sort, sort); if (i != 0) { prev_sort->next = sort->next; free(sort); sort = prev_sort->next; continue; } else { prev_sort = sort; sort = sort->next; } } return 0; } int Delete(ST *sort, ST *head) { ST *prev_head = NULL; int i = 0; ST *tmp = NULL; tmp = head; prev_head = head; head = head->next; while (head != NULL) { if (sort->orderNum == head->orderNum) { prev_head->next = head->next; free(head); head = prev_head->next; i = 1; } else { prev_head = head; head = head->next; } } //Print(tmp); return i; } int DeleteList(ST *sort) { ST *head = NULL; head = sort; sort = sort->next; while (head->next != NULL) { head->next = sort->next; free(sort); sort = head->next; } return 0; }