C Language Student Management system:
This program synthesizes the list, sorting and other knowledge, can be said to be a comprehensive exercise.
# include <stdio.h># include <malloc.h># include <stdlib.h>typedef struct Student{char Name[20];char Sex;int age;int Score;char sid[20];struct Student * PNEXT;} ST, *pst;void print (void); Welcome interface void ShowMenu (void); Show menu void Help (void); Show help PST create_list (void); Create a linked list void traverse_list (const PST); Iterate through the contents of the output list, modify the pointer variable with const constants to prevent the variable from being modified by bool Is_empty (PST); Determines whether the linked list is an empty int length_list (PST); Calculate the chain list length bool Append_list (PST); Append data bool Insert_list (PST); Insert data bool Delete_list (PST); Delete Data St Get_list (PST); Get data void sort_list (PST); Sorts the linked list data in descending order of grade int main (void) {int val; PST Phead = (PST) malloc (sizeof (ST));p rint ();p rintf ("\n\n\n");p head = Create_list (); traverse_list (Phead); while (true) {ShowMenu ();p rintf ("Choose you want to do:\n"); scanf ("%d", &val); switch (val) {case 1:append_list (phead); Break;case 2:insert_list (phead); Break;case 3:delete_list (Phead); Break;case 4:traverse_list (PHead); Break;case 5: Sort_list (phead); break;case 6:help (); Break;case 7:printf ("the Program would exit soon!\n "); break;default:printf (" Input error! Please input again! ");} if (7 = = val) break;} return 0;} void print (void) {printf ("\t*************************************************\n");p rintf ("\t******************** \ n ");p rintf (" \t****** ******\n ");p rintf (" \t***** Welcome To use Student Manage system! \ n ");p rintf (" \t****** ******\n ");p rintf (" \t************************************** \ n ");p rintf (" \t******************************-by lifeng*********\n "); return;} void ShowMenu (void) {printf ("\t*************************************************\n");p rintf ("\t************** 1. Add ************************\n ");p rintf (" \t************** 2. Insert ************************\n ");p rintf (" \t************** 3. Delete ************************\n ");p rintf (" \t************** 4. Show ************************\n ");p rintf (" \t************** 5. Sort ************************\n ");p rintf("\t************** 6. Help ************************\n ");p rintf (" \t************** 7. Exit ************************\n ");p rintf (" \t*************************************************\n "); return;} PST create_list (void) {int I;int j = 0;int Len; PST Student; PST Phead = (PST) malloc (sizeof (ST)), if (NULL = = Phead) {printf ("The Memory is error!\n"); exit (-1);} PST Ptail = Phead;ptail->pnext = null;printf ("Please input the number of students:"); scanf ("%d", &len);p rintf ("Ple ASE input the no.%d Student information! "); for (i=0; i<len; ++i)//for loop create linked list {printf ("Please input the no.%d Student information!"); printf ("Name:"); scanf ("%s", student->name[20]);p rintf ("Sex:"), scanf ("%c", &student->sex);p rintf ("Age:" ); scanf ("%d", &student->age);p rintf ("Score:"), scanf ("%d", &student->score);p rintf ("Sid:"); scanf ("% S ", student->sid[20]); PST pnew = (PST) malloc (sizeof (ST)), if (NULL = = pnew) {printf ("The Memory is error!\n"); exit (-1);} Pnew = Student;ptail->pnext = PNEW;PNEW->pnext = Null;ptail = pnew;} return phead;} void Treverse_list (const PST phead) {int i; PST p = phead->pnext;if (Is_empty (phead)) {printf ("The list is empty!\n"); exit (1);} while (NULL = = P->pnext) {printf ("The information of no.%d are\n", i);p rintf ("The name is:%s", p->name[20]);p rintf ("The Sex is:%c", P->sex);p rintf ("The Age was:%d", p->age);p rintf ("The score is:%d", P->score);p rintf ("th E SID is:%s ", P->sid);p rintf (" \ n ");p = P->pnext;++i;} return;} BOOL Is_empty (PST phead) {PST p = phead->pnext;if (NULL = = p) return True;elsereturn false;} int length_list (PST phead) {int len; PST p = phead->pnext;while (NULL! = p) {++len;p = P->pnext;} return Len;} BOOL Append_list (PST phead) {int i;int len;int pos; PST Ptail = (PST) malloc (sizeof (ST)); PST p = phead;printf ("Please input the information-want to append:\n");p rintf ("Name:"); scanf ("%s", &ptail->nam E[20]);p rintf ("Sex:"), scanf ("%c", &ptail->sex);p rintf ("Age:"); scanf ("%d", &ptail->age);printf ("Score:"); scanf ("%d", &ptail->score);p rintf ("Sid:"); scanf ("%s", &ptail->sid[20]); len = Length_ List (phead); for (i=0; i<len; ++i) {p=p->pnext;} P->pnext = Ptail;while (p!=null && i<pos) {p = P->pnext;} return true;} BOOL Insert_list (PST phead) {int i = 0;int pos; ST temp; PST p = phead;printf ("Please input the position of the List:pos ="); scanf ("%d", &pos);p rintf ("Please input the infor Mation of the student:\n ");p rintf (" Name: "), scanf ("%s ", &temp.name[20]);p rintf (" Sex: "); scanf ("%c ", &temp.sex );p rintf ("Age:"), scanf ("%d", &temp.age);p rintf ("Score:"), scanf ("%d", &temp.score);p rintf ("Sid:"); scanf (" %s ", &temp.sid[20]); while (P!=null && i<pos) {p = p->pnext;++i;} if (null==p | | i>pos) {printf ("error!\n"); exit (-1);} PST pnew = (PST) malloc (sizeof (ST)); *pnew = Temp;pnew->pnext = P->pnext;p->pnext = Pnew;return true;} BOOL Delete_list (PST phead) {int i = 0;int pos; PST p = phead; PST Q; PST PVal = (PST) malloc (SizeoF (ST));p rintf ("Please input the position of you want to Delete:pos ="); scanf ("%d", &pos); while (P!=null && i <pos-1) {p = p->pnext;++i;} if (null==p | | i>pos-1) {printf ("error!\n"); exit (-1);} *pval = *p;q = P->pnext;p->pnext = P->pnext->pnext;free (q);p rintf ("Delete successfully! The information of delete:\n ");p rintf (" Name:%s ", pval->name[20]);p rintf (" Sex:%c ", Pval->sex);p rintf (" Age:% D ", Pval->age);p rintf (" Score:%d ", Pval->score);p rintf (" Sid:%s\n ", pval->sid[20]); return true;} ST get_list (PST phead) {int i = 0;int pos; ST Stu; PST p = phead->pnext;printf ("Please input the position of you need:pos ="); scanf ("%d", &pos); if (Is_empty (Phead) {printf ("The list is empty!\n"); return Stu;} while (null!=p && i<pos) {p = p->pnext;++i;} if (null==p | | i>pos) {printf ("error!\n"); exit (-1);} Stu = *p;return Stu;} void Sort_list (PST phead)//bubble sorting sorted by grade descending {int i, j;int len; ST temp; PST p, Q;len = Length_list (Phead); for (I=0, P=phead; i<len; ++i, P=p->pnext) {for (j=0, q=phead->pnext; j<len-i-1; ++j, Q=q->pnext) {if (P->score < Q->score) { temp = *p;*p = *q;*q = temp;}}} return;} void Help (void) {printf ("It's So easy! Do you need help?\n\n ");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to C language Programming--comprehensive Exercises (iii) Student management system