用链表实现学生成绩管理系统的增删改查代码
时间: 2023-07-27 21:30:22 浏览: 97
好的,以下是用链表实现学生成绩管理系统的增删改查代码:
```c++
#include<iostream>
#include<cstring>
using namespace std;
struct student{
int id; //学号
string name; //姓名
float score; //成绩
student *next; //指向下一个节点的指针
};
class StudentList{
public:
StudentList(){
head = NULL;
}
~StudentList(){
student *p = head;
while(p){
student *q = p->next;
delete p;
p = q;
}
head = NULL;
}
void Create(); //创建链表
void Insert(); //插入节点
void Delete(); //删除节点
void Modify(); //修改节点
void Search(); //查找节点
void Display(); //显示链表
private:
student *head; //头节点
};
void StudentList::Create(){
int n;
cout<<"请输入学生人数:"<<endl;
cin>>n;
head = new student; //创建头节点
head->next = NULL;
student *p = head; //p指向尾节点
for(int i=0;i<n;i++){
student *s = new student; //创建新节点
cout<<"请输入第"<<i+1<<"个学生的信息:"<<endl;
cout<<"学号:"<<endl;
cin>>s->id;
cout<<"姓名:"<<endl;
cin>>s->name;
cout<<"成绩:"<<endl;
cin>>s->score;
s->next = NULL;
p->next = s; //尾插法插入新节点
p = s;
}
cout<<"创建链表成功!"<<endl;
}
void StudentList::Insert(){
student *s = new student; //创建新节点
cout<<"请输入要插入的学生信息:"<<endl;
cout<<"学号:"<<endl;
cin>>s->id;
cout<<"姓名:"<<endl;
cin>>s->name;
cout<<"成绩:"<<endl;
cin>>s->score;
s->next = NULL;
student *p = head;
while(p->next){
if(p->next->id > s->id){ //找到插入位置
s->next = p->next;
p->next = s;
cout<<"插入成功!"<<endl;
return;
}
p = p->next;
}
p->next = s; //尾插法插入新节点
cout<<"插入成功!"<<endl;
}
void StudentList::Delete(){
int id;
cout<<"请输入要删除的学生学号:"<<endl;
cin>>id;
student *p = head;
while(p->next){
if(p->next->id == id){ //找到要删除的节点
student *q = p->next;
p->next = q->next;
delete q;
cout<<"删除成功!"<<endl;
return;
}
p = p->next;
}
cout<<"未找到该学生!"<<endl;
}
void StudentList::Modify(){
int id;
cout<<"请输入要修改的学生学号:"<<endl;
cin>>id;
student *p = head;
while(p->next){
if(p->next->id == id){ //找到要修改的节点
cout<<"请输入新的学生信息:"<<endl;
cout<<"学号:"<<endl;
cin>>p->next->id;
cout<<"姓名:"<<endl;
cin>>p->next->name;
cout<<"成绩:"<<endl;
cin>>p->next->score;
cout<<"修改成功!"<<endl;
return;
}
p = p->next;
}
cout<<"未找到该学生!"<<endl;
}
void StudentList::Search(){
int id;
cout<<"请输入要查找的学生学号:"<<endl;
cin>>id;
student *p = head;
while(p->next){
if(p->next->id == id){ //找到要查找的节点
cout<<"学号:"<<p->next->id<<endl;
cout<<"姓名:"<<p->next->name<<endl;
cout<<"成绩:"<<p->next->score<<endl;
return;
}
p = p->next;
}
cout<<"未找到该学生!"<<endl;
}
void StudentList::Display(){
student *p = head->next;
while(p){
cout<<"学号:"<<p->id<<"\t姓名:"<<p->name<<"\t成绩:"<<p->score<<endl;
p = p->next;
}
}
int main(){
StudentList l;
int choice;
while(true){
cout<<"-----------------"<<endl;
cout<<"1.创建链表"<<endl;
cout<<"2.插入节点"<<endl;
cout<<"3.删除节点"<<endl;
cout<<"4.修改节点"<<endl;
cout<<"5.查找节点"<<endl;
cout<<"6.显示链表"<<endl;
cout<<"0.退出程序"<<endl;
cout<<"-----------------"<<endl;
cout<<"请输入您的选择:"<<endl;
cin>>choice;
switch(choice){
case 1:
l.Create();
break;
case 2:
l.Insert();
break;
case 3:
l.Delete();
break;
case 4:
l.Modify();
break;
case 5:
l.Search();
break;
case 6:
l.Display();
break;
case 0:
return 0;
default:
cout<<"输入有误,请重新输入!"<<endl;
break;
}
}
return 0;
}
```
这个程序实现了一个简单的学生成绩管理系统,可以通过命令行菜单进行增删改查等操作。其中,链表的每个节点包含了学生的学号、姓名和成绩等信息,并且可以进行动态的插入、删除和修改操作。
阅读全文