简介:并未封装成类,功能也仅实现了增量改查。数据为txt,格式:学号(long)姓名(string)总分(int)
学生信息结构体
struct Student {
long m_id;
string m_name;
int m_score;
};
节点结构体
struct Node {
Student data;
Node* next;
Node() { next = nullptr; }
};
读取data.txt中获取数据
Node* ReadInformation(Node* root) {
if (!root) root = new Node();//防止输入一个未初始化的指针
Node* p = root;
Node* q = nullptr;//去掉最后的空指针
ifstream in("data.txt");
long id;
string name;
int score;
while (!in.eof()) {
in >> id >> name >> score;
p->data.Set(id, name, score);
p->next = new Node();
q = p;
p = p->next;
}
q->next = nullptr;
delete p;
p = nullptr;
in.close();
return root;//返回首指针
}
输出全部信息
void Print(Node* root) {
if (!root) return;
Node* p = root;
cout << setw(6) << "ID" << setw(8) << "Name" << setw(6) << "Score\n";
while (p) {
cout << setw(6) << p->data.m_id
<< setw(8) << p->data.m_name
<< setw(6) << p->data.m_score
<< endl;
p = p->next;
}
cout << endl;
}
四个基本功能:增删改查
增地址:首端插入
Node* Add(Node* root, const Student& stu) {
Node* p = new Node();
p->data = stu;
p->next = root;
return p;
}
删删除:
//遍历 删除stu对应节点,然后返回头指针
Node* Delete(Node* root, const Student& stu) {
if (!root) {
cout << "No information to delete.\n";
return root;
}
Node* q = root;
Node* p = q->next;
//检查第一个
if (q->data == stu) {
root = root->next;
delete q;
q = nullptr;
return root;
}
while (p) {
if (p->data == stu) {
q->next = p->next;
delete p;
p = nullptr;
return root;
}
}
cout << "No such information to delete.\n";
return root;
}
改修改:
//找到index:从0开始(并非id) 对应的节点,输入修改后的信息,修改,然后返回头指针
void Modify(Node* root, const int& index) {
if (!root) {
cout << "There is no information to modify.\n";
return;
}
Node* p = root;
int i(0);
while (p) {
if (i == index) {
cout<<"Information;"<< p->data.m_id
<< setw(8) << p->data.m_name
<< setw(6) << p->data.m_score
<< endl;
long id;
string name;
int score;
char c;
cout << "New:";
cin >> id >> name >> score;
cout << "Are you sure to modify the information?<y/n> \nEnter:";
cin >> c;
if (c == 'Y' || c == 'y') {
p->data.Set(id, name, score);
return;
}
else return;
}
p = p->next;
++i;
}
cout << "The index:" << index << " is too big.\n"
<< "There are " << i+1 << " students.\n";
}
找:
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);
if (!p) cout << "Not found.\n";
return p;
case 2:
cout << "Please input Name:";
cin >> name;
p=FindByName(root,name);
if (!p) cout << "Not found.\n";
return p;
case 3:
cout << "Please input Score's range:";
cin >> sco1 >> sco2;
p = FindByScore(root, sco1, sco2);
if (!p) cout << "Not found.\n";
return p;
default:
break;
}
return nullptr;
}
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;
int count(0);
Node* res = nullptr;
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中:
#include "node.h"
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);
cout << endl;
break;
case 5:
Print(link);
break;
}
} while (cho);
system("pause");
return 0;
}
独特之处:检测的ID是否已存在,存在输入信息无效,必须保证学号唯一
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;
}
运行截图: