Student management System--based on two-way circular link list

Source: Internet
Author: User

The student management system based on two-way circular link list, including initialization, inserting, deleting, checking, saving, automatically sorting by name, exiting and saving function.

The idea of realization is to divide the various parts of the program into three levels. The main function is the interface layer, that is, the client layer, where the suffix is student is a function of the general scheduling function, the content of the logical layer, under the dispatch function has a corresponding scheduled function, that is, the corresponding function of the implementation function, the general suffix named node, meaning that this function directly manipulate the nodes in the list, Can be easily divided into the implementation layer;

This hierarchical implementation facilitates the direct invocation of code maintenance and functionality to include or overlap functions, thus improving code reuse, and reducing code redundancy, and the lowest possible implementation function can also be used in other projects for the operation of a bidirectional circular list.

The implementation of the student management system is to first use an initialization file function to put some student information into the disk, and then use the initialization function to read the list of various operations, saving function and Exit function can be linked to the list of the contents of the operation, that is, the contents of the memory is written to disk, Where the operation of the file using binary read and write files, reading and writing objects for the student structure.

Here is the code implementation:

#include <stdio.h> #include <stdlib.h> #include <string.h> #pragma  warning  (disable:4996 )//Define Student information node struct snode{char name[50];char sex;int math;int chinese;int history;};. typedef struct snode student;//defining a linked table node Struct node{student student;struct node  *left;struct Node *right;}; Typedef struct node node;void init (void);//Initialize a student information file node * createlist ();// Create a bidirectional loop linked list void initializationlist (node *head);//Initialize the list int insertstudent (node *head);// The menu inserts the function dispatch function int insertlist (node *head, student newstudent);//Insert Function implementation function, head interpolation method insert node void  Searchstudent (Node *head);//Find the function of the scheduling function Node *searchnode (node *head, char *name);// Find the implementation function of the function int  deletestudent (node *head);//menu to remove the function of the dispatch function Int deletenode (Node *head,  char *name);//delete function int lenlist (node *head),//To find the length of the two-way loop linked list Void sortlist (node&Nbsp;*head, int len);//Int savetofile (Node *head) based on the sorting function of the name string;//writes the student data stored in memory back to the disk file void  printlist (Node * head);//Print List Contents Int main (void) {//init ();//If there is no student information file, you can open this function, you can close this function after initialization,// The initial student information can be modified in this function printf ("Student management system \ n");p rintf ("All students currently: \ n"); Node *head = createlist ();//Create a two-way circular list management student initializationlist (head);//Read the initial Student information initialization list from the file while  (1) {int len = lenlist (head); Sortlist (Head, len);//output is sorted by first name System ("CLS"); Student management system \ n ");p rintf (" Currently all students: \ n ");p rintlist (head);//print the information in the list printf (" 1-> add \t2-> Delete \t3-> find 4-> save 5-> exit \ n "); INT&NBSP;SELECT&NBSP;=&NBSP;0;SCANF ("%d ", &select);switch  (select) {case 1:insertstudent (head) System ("CLS");p rintf ("Student management system \ n");p rintf ("Currently all students: \ n");p rintlist (head); Break;case 2:deletestudent (head); System ("CLS");p rintf ("Student management system \ n");p rintf ("Currently all students: \ n");p rintlist (head); Break;case 3:searchstudent (head); printf ("Please press any key to return to the main menu \ n"); GetChar (); Break;case 4:savetofile (head);p RinTF ("Save succeeded \ n"), System ("CLS");p rintf ("Student management system \ n");p rintf ("All students currently: \ n"), Initializationlist (head);p rintlist (head); BREAK;CASE&NBSP;5:PRINTF ("program is about to exit \ n"), SaveToFile (head);p rintf ("Save succeeded \ n"); exit (1); break;}} return 0;} Creation of initialization file Void init (void) {File *fop = fopen ("D:/test/classtest/student.txt",  "WB"); student students[5] = { {  "Zhao",  ' m ',  90, 100, 100 },{   "Qian",  ' W ', 60, 60, 60 },{  "Sun",  ' m ',  85, 85, 90  },{  "Li",  ' m ', 60, 100, 100 },{  "Zhou",  ' W ',  100, 100,  100 } };fwrite (Students, sizeof (Student), &NBSP;5,&NBSP;FOP); fclose (FOP);} Create a bidirectional loop linked list node * createlist () {node *head =  (node *) malloc (sizeof (Node));if  (head) {head->left = head;head->right = head;return head;} Elsereturn null;} Initialize linked list void initializationlist (node *heAD) {File *fip = fopen ("D:/test/classtest/student.txt",  "RB"); student newstudent;while  (Fread (&newstudent, sizeof (Student), &NBSP;1,&NBSP;FIP)  !=  0) {insertlist (head, newstudent);}} Menu Insert Function int insertstudent (node *head) {student newstudent;printf ("Please enter student information: \ n");p rintf ("Name:" ); scanf ("%s", &newstudent.name), GetChar ();p rintf ("Gender:"); Newstudent.sex=getchar ();p rintf ("math:"); scanf ("%d", &newstudent.math);p rintf ("Language:"), scanf ("%d", &newstudent.chinese);p rintf ("History:"); scanf ("%d",& Newstudent.history); insertlist (head, newstudent);} Junction insertion//Insertion function implementation function, head interpolation insert Int insertlist (node *head, student data) {node *newnode =   (node *) malloc (sizeof (Node));if  (NewNode) {newnode->student = data;newnode-> left = head;newnode->right = head->right;head->right = newnode;newnode- >right->left = newnode;return 1;} Elsereturn&nbSp;-1;} The menu removes the function of the dispatch function Int  deletestudent (node *head) {printf ("Please enter the name of the student who needs to be removed:"); CHAR&NBSP;NAME[50];SCANF ( "%s", name); GetChar (); Int flag=deletenode (head, name);if  (flag) printf ("Delete succeeded \ n"); elseprintf ("Delete failed \ n "); return flag;} The implementation function of the delete function Int deletenode (node *head, char *name) {node *searchstudent =  Searchnode (Head, name);if  (searchstudent) {searchstudent->left->right =  Searchstudent->right;searchstudent->right->left = searchstudent->left;free (searchStudent ); return 1;} return 0;} Find the function of the dispatch function Void searchstudent (node *head) {printf ("Please enter the name of the student who needs to be removed:"); CHAR&NBSP;NAME[50];SCANF ("%s",  name); GetChar (); Node *result = searchnode (head, name);if  (Result) {printf ("name \ t gender \ t math \ t language \ \ History \ n"); printf ("%s\t%c\t%d\t%d\t%d\n", result->student.name, result->student.sex,result -> Student.math, result->student.chinese, result->Student.history);} Elseprintf ("Did not find the student \ n");} Find the implementation function of the function Node *searchnode (node *head, char *name) {node *left = head; node *right = head;do{left = left->left;right = right->right;if  (strcmp (left->student.name, name) ==0) return left;if  (strcmp (right->student.name,  Name) ==0) Return right;}  while  (left != right && left->left != right);return  NULL;} Sort function based on name string Void sortlist (Node *head, int len) {node *p, *q, *max, * Temp student t;p = head->right;q = p->right;int i = 0, j =  0;for  (i = 0; i < len - 1; i++) {if  (p ==  Head) break;max = p;q = p;for  (j = i; j < len; j++) { if  (Q == head) break;if  (strcmp(max->student.name, q->student.name) >0) max = q;q = q->right;} if  (max != p) {t = max->student;max->student = p->student;p-> Student = t;} P = p->right;}} Int lenlist (node *head) {node *p = head;int len = 0;while) for two-way circular linked list   (p->right != head) {len++;p  = p->right;} Return len;} Print List Contents void printlist (node * head) {node *p = head->right;while  (p  != head) {printf ("%s\t%c\t%d\t%d\t%d\n", p->student.name, p->student.sex,p-> student.math, p->student.chinese, p->student.history);p  = p->right;}} Writes the student data stored in memory back to the disk file Int savetofile (node *head) {File *fop = fopen ("d:/test/classtest/ Student.txt ", " WB "); node *p = head->right;while  (p != head) {fwrite (&p->student,sizeof (Student), &NBSP;1,&NBSP;FOP);p  = p->right;} return 1;}


Student management System--based on two-way circular link list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.