利用单链表实现一个简单的学生信息管理系统

#include<iostream>
using namespace std;
//定义一个学生类结构体
struct Student
{

	int id;
	string name;
	int age;
	char gender;
};
//定义一个结点类结构体
struct Node
{
	Student data;
	Node* next;//指向下一结点的指针
};
//定义一个链表类来管理学生信息,并配置函数实现增删查,浏览
class Linkedlist
{
public:
	Linkedlist();
	~Linkedlist();
	void addStudent(Student student);//增
    bool removeStudent(int id);//删
	bool findStudent(int id, Student& student);//查
	void printStudents();//浏览
private:
	Node* head;//指向链表头部结点的指针,即头指针
};
//实现链表中所定义的函数
Linkedlist::Linkedlist()//该链表没有设置头结点?????到底有没有   确定没有
{
	head = nullptr;//通常把链表末尾的结点指针设置为nullptr,链表为空时,head指针设置为nulltpr
}                      //,表示链表为空,
Linkedlist::~Linkedlist()
{
	Node* current = head;//current是指向当前结点类型的指针,并把指向头结点的指针赋值给current ,此时current指向头结点  
	while (current != nullptr) {
		Node* next = current->next;
		delete current;
		current = next;
	}
}
void Linkedlist::addStudent(Student student)//从左边(头)插入
{
    Node* newNode = new Node;
    newNode->data = student;
    newNode->next = head;
    head = newNode;
}
bool Linkedlist::removeStudent(int id)
{
    Node* current = head;
    Node* prev = nullptr;
    while (current != nullptr && current->data.id != id)//不是所找的结点就执行
    {
        prev = current;
        current = current->next;//指针往后移动
    }
    if (current == nullptr) //如果到表尾了,还没找到目标结点,就执行
       return false;
    if (prev == nullptr)//如果 当前结点是头结点,则将头指针指向当前结点的下一个结点
        head = current->next;
    else prev->next = current->next;//如果要删除的结点不是第一个结点,将前一个结点的指针指
                                    //指向当前结点的下一个结点
    delete current;
    return true;
}
bool Linkedlist::findStudent(int id, Student& student)
{
    Node* current = head;
    while (current != nullptr && current->data.id != id)
    {
        current = current->next;//指针向后移动
    }
        if (current == nullptr)  return false;//表走完也没找到
        student = current->data;
        return true;
}
void Linkedlist::printStudents()
{
    Node* current = head;
    while (current != nullptr)
    {
        cout << "学号为:" << current->data.id << endl;
        cout << "名字为:" << current->data.name << endl;
        cout << "年龄为:" << current->data.age << endl;
        cout << "性别为:" << current->data.gender << endl;
        current = current->next;
    }
}


int main()
{
	Linkedlist list;
	int choice;
	do
	{
		cout << "1.添加学生信息\n";
		cout << "2.删除一个学生的信息\n";
		cout << "3.根据学号查询某个学生的基本信息\n";
		cout << "4.浏览每个学生的信息\n";
		cout << "5.停止\n";
		cout << "请输入你的选择:";
		cin >> choice;
		switch (choice)
		{
		case 1: {
			Student student;
			cout << "输入学号:";cin >> student.id;
			cout << "请输入姓名:";cin >> student.name;
			cout << "请输入年龄:";cin >> student.age;
			cout << "请输入性别(M/F):";cin >> student.gender;
			list.addStudent(student);
			break;
			   }
    case 2: {
        int id;//可是id不是定义过了吗?
        cout << "请输入学号:";cin >> id;
        if (list.removeStudent(id))
            cout << "学生信息已经被删除!\n";
        else cout << "没有找到该学生的信息\n";
        break;
            }

case 3:
	{
		int id;
		cout << "请输入所要查询的学生的id:";cin >> id;
		Student student;
		if (list.findStudent(id, student))
        {
			cout << "该学生信息为:\n";
			cout << "学号:" << student.id << "\n";
			cout << "姓名;" << student.name << "\n";
			cout << "年龄:" << student.age << "\n";
			cout << "性别:" << student.gender << "\n";
		}
		else cout << "该学生的信息没有找到!\n";
		break;
	}
case 4:
	{
		list.printStudents();
		break;
	}
case 5:
	{
		cout << "再见!\n";
		break;
	}
		default:
		{
			cout << "错误的指令\n";
			break;
		}
		}
	} while (choice != 5);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值