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

简介:并未封装成类,功能也仅实现了增量改查。数据为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;
}

运行截图:

  • 8
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值