/* 3. Vježba Napraviti funkcije za rad s uređenom jednostruko povezanom listom struktura u radnoj memoriji (dodavanje, traženje, brisanje, ispis). Strukturu zapisa i dio zapisa koji predstavlja ključ definirati po volji. Napisati potprograme za izmjenu ključa elementa liste uz očuvanje uređenja: a) zamjenom sadržaja elemenata b) uporabom funkcija za brisanje i dodavanje elementa U glavnom programu treba kreirati uređenu listu podataka prethodno pohranjenih u slijednoj formatiziranoj datoteci, a zatim interaktivno tražiti elemente i mijenjati vrijednost ključa uz provjeru sadržaja liste. Dodatak: Prilagoditi podatkovne strukture i napisati potprogram koji postojeću listu, uz osnovni ključ, uređuje po nekom drugom ključu (lista s više ključeva). */ //(c) kreator '97 //travelling through the time, moving slowly in the sand //knowledge is the weapon against the hunger in the land. #include #include #include #include #include struct item_type { //structure type.. char name[20+1]; int score; }; struct list_item { //structure of node struct item_type item; struct list_item *next; }; typedef struct list_item node; int create(node *record, FILE *input) //create linked list without key { struct item_type item; int flag=0; while ((fscanf(input, "%s %d", &item.name, &item.score)!=EOF)) { flag=1; record->item=item; //copy item to current node record->next=(node *)malloc(sizeof(node)); //allocate space for next node if (!record->next) break; //stop adding.. it's end if (!create(record->next, input)) record->next=0; //create next node } return flag; } void display(node *record) //display linked list { printf("%s %d\n", record->item.name, record->item.score); if (record->next) display(record->next); return; } node *locate(node *record, char *target) { if (!strcmpi(record->next->item.name,target)) return(record); //found else if (!record->next->next) return 0; //end of list else locate(record->next, target); //try next node } node *delet(node *first) //add 1 record, ret ptr to start of altered list { node *tag; node *tmp; char target[20+1]; printf("Delete item [s]: "); scanf("%s", &target); if (!(strcmpi(first->item.name,target))) { tmp=first->next; free(first); first=tmp; } else //insert node after existing { tag=locate(first, target); if (!tag) printf("Not found!\n"); else { tmp=tag->next->next; free(tag->next); tag->next=tmp; } } return(first); } node *insert(node *first) //add 1 record, ret ptr to start of altered list { node *newrecord; //new node node *tag; //node before target node struct item_type newitem; //new data item char target[20+1]; //data item following the new entry printf("New data item [s] [d]: "); scanf("%s %d", &newitem.name, &newitem.score); printf("Place it before [s]: "); scanf("%s", &target); if (!(strcmpi(first->item.name,target))) //new node is first in the list { newrecord=(node *)malloc(sizeof(node)); newrecord->item=newitem; newrecord->next=first; first=newrecord; } else //insert node after existing { tag=locate(first, target); if (!tag) printf("Not found!\n"); else { newrecord=(node *)malloc(sizeof(node)); newrecord->item=newitem; newrecord->next=tag->next; tag->next=newrecord; } } return(first); } node *create2(node *start, FILE *input) { node *tmp=start, *newitem, *tmp2; struct item_type item; fscanf(input, "%s %d", &item.name, &item.score); start->item=item; start->next=0; while (fscanf(input, "%s %d", &item.name, &item.score)!=EOF) { newitem=(node *)malloc(sizeof(node)); newitem->item=item; newitem->next=0; if (tmp->item.score>=item.score) { //to the beginning newitem->next=start; start=newitem; } else { //find where to put it tmp2=start; //init values.. in fact start searching from head->next tmp=start->next; while (tmp&&tmp->item.scorenext; } tmp2->next=newitem; //link prev with new newitem->next=tmp; //link new with fol } tmp=start; } return start; } void change (node *first) { node *tag; struct item_type item; char target[20+1]; printf("change item [s]: "); scanf("%s", &target); if (!strcmpi(first->item.name,target)) { //it's head.. printf("Enter new values [s] [d]: "); scanf("%s %d", &item.name, &item.score); first->item=item; } else { //seek and destroy ;) tag=locate(first, target); if (!tag) printf("Not found!\n"); else { printf("Enter new values [s] [d]: "); scanf("%s %d", &item.name, &item.score); tag->next->item=item; } } } void main(void) { FILE *input; node *start=(node *)malloc(sizeof(node)); char choice; clrscr(); if (!(input=fopen("INPUT.TXT", "rt"))) { printf("Input error!\n"); exit(1); } while (1) { clrscr(); printf("1) sort_liste\n2) unsort_list\n3) add\n4) delete\n5) change\n6) exit\n"); choice=getch(); switch(choice) { case '1': start=create2(start, input); display(start); getch(); break; case '2': create(start, input); display(start); getch(); break; case '3': display(start); start=insert(start); display(start); getch(); break; case '4': display(start); start=delet(start); display(start); getch(); break; case '5': display(start); change(start); display(start); getch(); break; case '6': fclose(input); exit(0); default: printf("Wrong choice!\n"); getch(); } rewind(input); } }