新建student.cpp,实现student.h声明的所有函数
1 #include<iostream> 2 #include "student.h" 3 4 using namespace std; 5 6 void displayStudent(Student *stu_head) { 7 Student *curNode = stu_head; 8 cout << "学号 姓名 性别 年龄 成绩" << endl; 9 while(curNode != NULL) { 10 cout << curNode->no << " " << curNode->name << " " << curNode->sex << " " << curNode->age << " " << curNode->score << endl; 11 curNode = curNode->next; 12 } 13 } 14 15 Student* searchStudent(Student *stu_head) { 16 string sno; 17 cout << "please input student no:" << endl; 18 cin >> sno; 19 Student *curNode = stu_head; 20 cout << "学号 姓名 性别 年龄 成绩" << endl; 21 while(curNode != NULL) { 22 if(curNode->no == sno) { 23 cout << curNode->no << " " << curNode->name << " " << curNode->sex << " " << curNode->age << " " << curNode->score << endl; 24 break; 25 } else { 26 if(curNode->next != NULL){ 27 curNode = curNode->next; 28 } else { 29 break; 30 } 31 } 32 } 33 return curNode; 34 } 35 36 Student* addStudent(Student *stu_head) { 37 Student *student = new Student; 38 cout << "input no:" << endl; 39 cin >> student->no; 40 cout << "input name:" << endl; 41 cin >> student->name; 42 cout << "input age:" << endl; 43 cin >> student->age; 44 cout << "input sex:" << endl; 45 cin >> student->sex; 46 cout << "input score:" << endl; 47 cin >> student->score; 48 49 student->next = NULL; 50 51 if (stu_head == NULL){ 52 stu_head = student; 53 } else { 54 student->next = stu_head->next; 55 stu_head->next = student; 56 } 57 return stu_head; 58 } 59 60 void modStudent(Student *stu_head) { 61 Student *curNode = searchStudent(stu_head); 62 if(curNode != NULL) { 63 cout << "input your modify select:" << endl; 64 cout << "1)name" << endl; 65 cout << "2)sex" << endl; 66 cout << "3)age" << endl; 67 cout << "4)score" << endl; 68 int key; 69 cin >> key; 70 switch (key) 71 { 72 case 1: 73 cout << "input name:" << endl; 74 cin >> curNode->name; 75 break; 76 case 2: 77 cout << "input sex:" << endl; 78 cin >> curNode->sex; 79 break; 80 case 3: 81 cout << "input age:" << endl; 82 cin >> curNode->age; 83 break; 84 case 4: 85 cout << "input score:" << endl; 86 cin >> curNode->score; 87 break; 88 default: 89 break; 90 } 91 cout << "学号 姓名 性别 年龄 成绩" << endl; 92 cout << curNode->no << " " << curNode->name << " " << curNode->sex << " " << curNode->age << " " << curNode->score << endl; 93 } 94 95 } 96 97 Student* delStudentByNo(Student *stu_head, string sno) { 98 Student *curNode = stu_head; 99 if(stu_head != NULL && stu_head->no == sno) { 100 if(stu_head->next != NULL) { 101 Student* delNode = stu_head; 102 stu_head = stu_head->next; 103 delNode = NULL; 104 } else { 105 stu_head = NULL; 106 } 107 108 } 109 110 while(curNode != NULL && curNode->next != NULL){ 111 if(curNode->next->no == sno){ 112 Student *delNode = NULL; 113 delNode = curNode->next; 114 curNode->next = curNode->next->next; 115 delNode = NULL; 116 break; 117 } else { 118 curNode = curNode->next; 119 } 120 // displayStudent(stu_head); 121 } 122 return stu_head; 123 }