/ / Link list, the addition of the list, the deletion of the list, read a friend from the file call me to write a list of operations, I haven't touched the C data structure for a long time, I still can't think of it. Ha. Flower I have been going out a day, I have a thing of this, and I put it on the friends who need it. Write is not very good.:) # Include
Struct add_book {char name [15]; CHAR SEX [4]; int Age; char add [100]; float score; struct add_book * next; / * point to next list * /} list_entry; int menu_select (); void print_to_screen (); int 4; int swapy (inters); void sort (struct add_book **); void save (); int Load (); static struct add_book * Last = NULL; STRUCT ADD_BOOK * START; main () {Int del_num, modify_num, com_num; for (;;) switch (menu_select ()) {case 1: printf ("[1] loading form files./n); Load (); break; case 2: printf ("[2] Save to Files./N"); Save (); Break; Case 3: Printf ("[3] add a new record./nplease input the Contant of The record! / n "); add_record (); breaf; case 4: printf (" [4] DELETE RECORD! / NPLESE INPUT NUBER of The Record That You Want To Delete! "); Printf (" / NNumber: "); scanf ("% D ", & del_num); del_record (del_num); Break; Case 5: Printf "[5] modify record./nplease input number of the record that you want to modify!"); Printf ("/ nnumber:"); scanf ("% d", & modify_num); modify (modify_num); Break; Case 6: Printf ("[5] Compositor./N"); Printf ("Compositor by Score)
SMALL) ./ n ... / n "); sort (& start); break; case 7: print_to_screen (); break; cas 8: exit (0);}}
Void print_to_screen () {struct add_book * sign;
INT i; if (start == null) {Printf ("Error: this is a space list! / n"); return;} else Sign = start; for (i = 1 ;; i ) IF (Sign! = Null ) {Printf ("NO .-% D- / N", I); Printf ("Name:% S / N", SIGN-> Name); Printf ("SEX:% S / N", SIGN-> SEX ); Printf ("Age:% D / N", SIGN-> AGE); Printf ("Address:% S / N", SIGN-> ADD); Printf ("score:% f / n", siGN-> Sign = Sign-> Next;} else {printf ("===== Print over! ===== / n"); Break;}}
INT add_record () {struct add_book * info;
For (;;) {info = (Struct add_book *) malloc (SIZEOF (LIST_ENTRY)); if (! info) {printf ("/ n out of memory!"); return;} printf ("name:"); Scanf ("% s", info-> name); Printf ("SEX:"); scanf ("% s", info -> sex); Printf ("Age:"); scanf ("% d", & info -> AGE); Printf ("Address:"); scanf ("% s", info -> add); Printf ("score:"); scanf ("% f", & info -> score;
If (! last) {Last = info; las-> next = null; start = info; / * save start node * /} else {las-> next = info; info-> next = null; las = info;} Return;}} int menu_select () {int C; // char s [80]; printf ("/ n [1] load form files./n"); Printf ("[2] save to file./n" ); Printf ("[3] add record./N"); Printf ("[4] delete record./N"); Printf ("[5] modify./N"); Printf ("[6] Compositor./N "); Printf (" [7] Print to screen./N "); Printf (" [8] quit./N "); do {printf (" / NENTER Your kice: "); //// Gets (s); // c = atoi (s); scanf ("% d", & c);} while (c <1 || c> 8); return (c);}
int del_record (int del_num) {int i; struct add_book * sign; struct add_book * ptr_prev = NULL; struct add_book * ptr = NULL; if (start == NULL) {printf ( "ERROR: This is a space list / n! ");} Else sign = start;
For (i = 1; i <= del_num; i ) {if (== (DEL_NUM-1)) PTR_PREV = SIGN; if (i == del_num) PTR = Sign; SIGN = SIGN-> NEXT;
} Else {printf ("THE RECORD YOU SELECT IS NOTF (" THE RECID YOU S);}}}}} (PTR_PREV! = NULL && PTR-> Next! = NULL) PTR_PREV-> Next = Ptr-> Next; IF (PTR_PREV == NULL) START = PTR-> Next; if (Ptr-> Next == Null && Ptr_prev! = null) {Last = PTR_PREV; PTR_PREV-> Next = NULL;} if (Ptr-> Next == Null && ptr_prev == NULL) Last = PTR_PREV; Free (PTR); Printf ("/ n =====); ===== / n"); return 0;}
INT MODIFY (INT MODIFY_NUM) {INT I; STRUCT ADD_BOOK * SIGN; STRUCT Add_book * Ptr = NULL; if (start == null) {Printf ("Error: this is a space List! / n"); return 0;} Else Sign = Start;
For (i = 1; i <= modify_num; i ) {if (Sign! = NULL) {if (i == modify_num) PTR = Sign; SIGN = Sign-> Next;} else {printf ("The Record you SELECT IS not exist! / n "); return 0;}} printf (" ===== begin modify! ===== / n "); Printf (" Name: [% s] / n -> ", PTR-> name); Scanf ("% s", ptr -> name); Printf ("SEX: [% s] / n ->", ptr-> sex); scanf ("% s", ptr -> SEX); Printf ("Age: [% D] / N ->", PTR-> AGE); Scanf ("% D", & PTR -> AGE); Printf ("Address: [% s] / n -> ", PTR-> add); scanf ("% s ", ptr -> add); Printf (" score: [% f] / n -> ", ptr-> score; scanf ("% f ", & Ptr -> score); Printf ("======== / n"); return 0;} void sort (struct add_book ** h) // hóãóö ¸ï ö¸ïòá' ¸ í · Μä õõõ μä õõõ {struct add_book * h1, * p, * q, * r, * s; h1 = p = (struct add_book *) malloc (sizeof (list_entry)); p-> next = * h; While (p-> next) {q = p-> next; r = p; while (q-> next) {if (q-> next-> score> r-> next-> score) r = q; Q = Q-> NE Xt;} if (r! = p) {s = r-> Next; r-> next = S-> Next; S-> Next = P-> next; p-> next = s;} P = P- > next;} * h = h1-> next; free (h1);}
Void save () {file * ptr; struct add_book * sign; sign = start; ptr = fopen ("c: //rikxu.txt", "w"); while (Sign! = null) / * get data from stdin * / {
FPRINTF (PTR, "% S / N", SIGN-> Name); / * send data to file. * / fprintf (PTR, "% S / N", SIGN-> SEX); FPRINTF (PTR, "% D / N ", SIGN-> AGE); FPRINTF (PTR,"% S / N ", SIGN-> add); FPRINTF (PTR,"% f / n ", siGN-> score); Sign = Sign-> NEXT Fclose (PTR);} int Load () {file * ptr; struct add_book * sign, * new_start, * temp, * start_bak; char name_temp [15]; int i = 1; ptr = fopen ("C: / / eri = (ptr == null) return 0; while (ptr, "% s", name_temp)! = EOF) / * GET DATA from stdin * / {temp = Struct add_book *) malloc (list_entry)); if (! Temp) {Printf ("Error: Out of Memory!"); Return 0;} / * send data to file. * / strcpy (TEMP-> Name, Name_temp); FSCANF (PTR, "% S", TEMP-> SEX); FSCANF (PTR, "% D", & TEMP-> AGE); FSCANF (PTR, "% s", temp-> add); fscanf ( PTR, "% f", & temp-> score; if (i == 1) {new_start = Temp; Sign = TEMP;} else {siGN-> next = Temp; Sign = Sign-> Next;} i ;} Fclose (PTR); TEMP-> Next = NULL; Start_bak = start; while (start_bak! = null) {sign = start_bak; start_bak = start_bak-> next; free (sign);} start = new_start; las = temp;}