C++链表实现学生成绩管理(增删改查)

简介:并未封装成类,功能也仅实现了增删改查。数据为txt,格式:学号(long) 姓名(string) 总分(int)

学生信息结构体
[cpp]  view plain  copy
  1. struct Student {  
  2.     long m_id;  
  3.     string m_name;  
  4.     int m_score;  
  5. };  

节点结构体
[cpp]  view plain  copy
  1. struct Node {  
  2.     Student data;  
  3.     Node* next;  
  4.   
  5.     Node() { next = nullptr; }  
  6. };  

读取data.txt获取数据
[cpp]  view plain  copy
  1. Node* ReadInformation(Node* root) {  
  2.     if (!root) root = new Node();//防止输入一个未初始化的指针  
  3.   
  4.     Node* p = root;  
  5.     Node* q = nullptr;//去掉最后的空指针  
  6.   
  7.     ifstream in("data.txt");  
  8.   
  9.     long id;  
  10.     string name;  
  11.     int score;  
  12.     while (!in.eof()) {  
  13.         in >> id >> name >> score;  
  14.         p->data.Set(id, name, score);  
  15.         p->next = new Node();  
  16.         q = p;  
  17.         p = p->next;  
  18.     }  
  19.     q->next = nullptr;  
  20.     delete p;  
  21.     p = nullptr;  
  22.     in.close();  
  23.     return root;//返回首指针  
  24. }  

输出全部信息
[cpp]  view plain  copy
  1. void Print(Node* root) {  
  2.     if (!root) return;  
  3.     Node* p = root;  
  4.     cout << setw(6) << "ID" << setw(8) << "Name" << setw(6) << "Score\n";  
  5.     while (p) {  
  6.         cout << setw(6) << p->data.m_id  
  7.             << setw(8) << p->data.m_name  
  8.             << setw(6) << p->data.m_score  
  9.             << endl;  
  10.         p = p->next;  
  11.     }  
  12.     cout << endl;  
  13. }  

四个基本功能:增删改查
增Add:首端插入
[cpp]  view plain  copy
  1. Node* Add(Node* root, const Student& stu) {  
  2.     Node* p = new Node();  
  3.     p->data = stu;  
  4.     p->next = root;  
  5.     return p;  
  6. }  
删Delete:
[cpp]  view plain  copy
  1. //遍历 删除stu对应节点,然后返回头指针  
  2. Node* Delete(Node* root, const Student& stu) {  
  3.     if (!root) {  
  4.         cout << "No information to delete.\n";  
  5.         return root;  
  6.     }  
  7.     Node* q = root;  
  8.     Node* p = q->next;  
  9.     //检查第一个  
  10.     if (q->data == stu) {  
  11.         root = root->next;  
  12.         delete q;  
  13.         q = nullptr;  
  14.         return root;  
  15.     }  
  16.     while (p) {  
  17.         if (p->data == stu) {  
  18.             q->next = p->next;  
  19.             delete p;  
  20.             p = nullptr;  
  21.             return root;  
  22.         }  
  23.     }  
  24.     cout << "No such information to delete.\n";  
  25.     return root;  
  26. }  

改Modify:
[cpp]  view plain  copy
  1. //找到index:从0开始(并非id) 对应的节点,输入修改后的信息,修改,然后返回头指针  
  2. void Modify(Node* root, const int& index) {  
  3.     if (!root) {  
  4.         cout << "There is no information to modify.\n";  
  5.         return;  
  6.     }  
  7.     Node* p = root;  
  8.     int i(0);  
  9.     while (p) {  
  10.         if (i == index) {  
  11.             cout<<"Information;"<< p->data.m_id  
  12.                 << setw(8) << p->data.m_name  
  13.                 << setw(6) << p->data.m_score  
  14.                 << endl;  
  15.             long id;  
  16.             string name;  
  17.             int score;  
  18.             char c;  
  19.             cout << "New:";  
  20.             cin >> id >> name >> score;  
  21.             cout << "Are you sure to modify the information? \nEnter:";  
  22.             cin >> c;  
  23.             if (c == 'Y' || c == 'y') {  
  24.                 p->data.Set(id, name, score);  
  25.                 return;  
  26.             }  
  27.             else return;  
  28.         }  
  29.         p = p->next;  
  30.         ++i;  
  31.     }  
  32.     cout << "The index:" << index << " is too big.\n"  
  33.         << "There are " << i+1 << " students.\n";  
  34. }  

查Find:分为id、name、score range三种查找
[cpp]  view plain  copy
  1.   
Node* Find(Node* root,const int& cho) { if (!root) { cout << "The linklist is empty.\n"; return nullptr; } long id; string name; int sco1, sco2; Node* p = nullptr; switch (cho) { case 1: cout << "Please input Id:"; cin >> id; p=FindById(root,id); break; case 2: cout << "Please input Name:"; cin >> name; p=FindByName(root,name); break;
case 3: cout << "Please input Score's range:"; cin >> sco1 >> sco2; p = FindByScore(root, sco1, sco2);
                break; default: break; }
         if (!p) cout << "Not found.\n";return p; }
Node* FindById(Node* root,const long& id) { Node* p = root; while (p) { if (p->data.m_id == id) { cout << setw(6) << p->data.m_id << setw(8) << p->data.m_name << setw(6) << p->data.m_score << endl; return p; } p = p->next; } return nullptr; } Node* FindByName(Node* root,const string& name) { Node* p = root; Node* res=nullptr; int count(0); while (p) { if (p->data.m_name == name) { cout << setw(6) << p->data.m_id << setw(8) << p->data.m_name << setw(6) << p->data.m_score << endl; ++count; if (count == 1) res = p; } p = p->next; } return res; } Node* FindByScore(Node* root,const int& sco1,const int& sco2) { if (sco1 > sco2) { cout << "Error input.\n"; return nullptr; } Node* p = root; Node* res = nullptr;
        int count(0); while (p) { if (p->data.m_score >= sco1 && p->data.m_score <= sco2) { cout << setw(6) << p->data.m_id << setw(8) << p->data.m_name << setw(6) << p->data.m_score << endl; ++count; if (count == 1) res = p; } p = p->next; } return res; }


测试:main.cpp
[cpp]  view plain  copy
  1.   
bool IsUnique(Node* root, const long& id) {          if (!root) return true;          Node* p = root;          while (p) {               if (p->data.m_id == id) {                return false;           }           p = p->next;          }          return true; }
Node* link=nullptr; int main() { link = ReadInformation(link); { cout << "0.Exit\n"; cout << "1.Add\n"; cout << "2.Delete\n"; cout << "3.Modify\n"; cout << "4.Find\n"; cout << "5.Print\n\n\n"; } int cho; int cho2; do{ cout << "Enter: "; cin >> cho;
Student s; int index;
switch (cho) { case 0:break; case 1: cout << "Information:"; cin >> s.m_id >> s.m_name >> s.m_score; //check id avoid the same id if (!IsUnique(link, s.m_id)) { cout << "Id has existed.\n"; break; } link = Add(link, s); break; case 2: cout << "Information:"; cin >> s.m_id >> s.m_name >> s.m_score; link = Delete(link, s); break; case 3: cout << "Index:"; cin >> index; Modify(link, index); break; case 4: cout << "1.Find by Id\n" << "2.Find by Name\n" << "3.Find by Score's range\n" << "Enter:"; cin >> cho2; Find(link, cho2); break; case 5: Print(link); break; } } while (cho); system("pause"); return 0; }

运行截图:

你可能感兴趣的:(交流)