1. Create a Dynamic Linked List. each node in the linked list includes the student ID, name, gender, age, and score.ProgramThe following functions can be implemented:
Create a linked list
Display linked list
Check whether an element exists in the linked list and display all the information of this element. If this element is not found, the system displays "None of this record !" .
Delete the node of the specified student ID in the linked list.
Insert a new node at the specified position in the linked list (the student ID cannot be the same as other nodes ).
Requirement: when the program is running, display the menu consisting of the above functions, call the corresponding program and display its corresponding results according to the options, and then display the menu program, the program execution ends until you press the exit option.
CompleteCodeAs follows:
# Include "stdio. H "# include" stdlib. H "typedef struct student {int ID; // student ID char name [20]; // name char sex; // gender (f or m) int age; // age int score; // score struct student * Next;} student; Student * head = NULL; int length; // The length of the linked list void create () {student * P1, * P2; length = 0; P1 = (student *) malloc (sizeof (student); P1-> id =-1; if (Head = NULL) Head = p1; printf ("Enter the student's student ID, name, gender, age, score: \ n"); While (1) // exit {P2 = (student *) malloc (sizeof (s) when the student ID is 0 Tudent); scanf ("% d % S % C % d", & p2-> ID, P2-> name, & p2-> sex, & p2-> Age, & p2-> score); // enter the student information if (P2-> id = 0) {printf ("linked list created! \ N "); break;} length ++; // chain table length P1-> next = P2; P2-> next = NULL; P1 = p1-> next ;} return;} void display () {student * P = head-> next; printf ("all student information in the linked list is as follows: \ n"); While (P! = NULL) {printf ("% d % S % C % d \ n", p-> ID, p-> name, p-> sex, p-> Age, p-> score); P = p-> next;} return;} void search () {int num; Student * P = head-> next; printf ("the student ID to be searched is:"); scanf ("% d", & num); While (P! = NULL) {If (p-> id = num) {printf ("student information with student ID % d: \ n", num ); printf ("% d % S % C % d \ n", p-> ID, p-> name, p-> sex, p-> Age, p-> score); return;} p = p-> next;} If (P = NULL) printf ("No such record! \ N "); return;} void insert () {int num, I; Student * P, * q; P = head; printf (" Enter the position you want to insert: "); scanf (" % d ", & num); If (Num> length) {printf (" the location to insert cannot be found \ n "); return ;} else {printf ("Enter the student ID, name, gender, age, and score of the student you want to insert: \ n"); q = (student *) malloc (sizeof (student); scanf ("% d % S % C % d", & Q-> ID, Q-> name, & Q-> sex, & Q-> Age, & Q-> score); While (P! = NULL) {If (p-> id = Q-> ID) {printf ("this student ID already exists and cannot be inserted! \ N "); return;} p = p-> next;} p = head; for (I = 0; I <num; I ++) P = p-> next; q-> next = p-> next; P-> next = Q; length ++; printf ("inserted successfully! \ N "); Return ;}} void Delete () {int num; Student * P, * q; q = head, P = head-> next; printf ("Enter the student ID to delete: \ n"); scanf ("% d", & num); While (P! = NULL) {If (p-> id = num) {q-> next = p-> next; free (p); Length --; printf ("deleted successfully! \ N "); return;} p = p-> next; q = Q-> next;} If (P = NULL) {printf ("the ID to be deleted cannot be found! \ N "); Return ;}} void menu () {printf (" ____________________________________________________________________ \ n "); printf (" | student information management system | \ n "); printf ("| 0, exit the system | \ n"); printf ("| 1. Create a linked list | \ n "); printf ("| 2. Display linked list | \ n"); printf ("| 3. Find an element in the linked list | \ n "); printf ("| 4. Delete the node with the specified student ID in the linked list | \ n"); printf ("| 5. Insert a new node at the specified position | \ n "); printf ("____________________________________________________________________\ N"); return;} int main (void) {int A; menu (); While (1) {printf ("select the appropriate function:"); scanf ("% d", & A); Switch () {Case 0: Return 0; Case 1: Create (); menu (); break; Case 2: If (head) {display (); menu ();} else {printf ("the linked list is empty. Please create a linked list first! \ N "); menu ();} break; Case 3: If (head) {search (); menu () ;}else {printf (" the linked list is empty, create a linked list first! \ N "); menu ();} break; Case 4: If (head) {Delete (); menu () ;}else {printf (" the linked list is empty, create a linked list first! \ N "); menu ();} break; Case 5: If (head) {insert (); menu () ;}else {printf (" the linked list is empty, create a linked list first! \ N "); menu ();} break; default: break;} system (" pause "); Return 0 ;}
program description: four student information headers are added. Choose Liuwei> Zhanghua> Lina> Liuxiang. The length of the linked list is 4. Enter 4 when inserting the table, A student information will be inserted behind Liuxiang; input 1 will insert a student information behind Liuwei;