观前提示,没有涉及输入错误这些情况的讨论,也没有涉及名字相同的情况
直接上代码
节点类
public:
int score;
string id;
string name;
string gender;
Node* next;
Node()
{
next = nullptr;
}
void display()
{
cout << id << '\t' << name << '\t' << gender << '\t' << score << endl << endl;
}
void getinfor()
{
cin >> id >> name >> gender >> score;
}
};
链接类
class Link {
public:
Node* head;
Link()
{
head = new Node();
}
~Link()//销毁
{
Node* p = head;
Node* q;
while (p != nullptr)
{
q = p->next;
delete p;
p = q;
}
}
};
Link实现,插入,删除,查找,打印
class Link {
public:
Node* head;
Link()
{
head = new Node();
}
void create()//创建
{
cout << "请输入人数规模: "; cin >> n;
cout << "学号" << '\t' << "姓名" << '\t' << "性别" << '\t' << "分数" << endl;
for (int i = 0; i < n; i++)
{
Node* s = new Node();
s->getinfor();
s->next = head->next;
head->next = s;
}
}
void insert()//插入
{
string id, name, gender;
int score, pos;
Node* p = new Node();
cout << "位置 "; cin >> pos;
cout << "学号 "; cin >> p->id;
cout << "姓名 "; cin >> p->name;
cout << "性别 "; cin >> p->gender;
cout << "分数 "; cin >> p->score;
Node* s = head->next;
for (int i = 0; i < n; i++)
{
if (i == pos)
{
p->next = s->next;
s->next = p;
break;
}
s = s->next;
}
n++;
cout << endl << "插入后的结果:" << endl;
print();
}
void dele()//删除
{
string id;
cout << "请输入你删除的学号: "; cin >> id;
Node* p = head->next;
Node* q = head;
while (p != nullptr)
{
if (p->id == id)
{
q->next = p->next;
delete p;
break;
}
p = p->next;
q = q->next;
}
n--;
cout << endl << "删除后的结果:" << endl;
print();
}
void find()//查找
{
string id;
cout << "按照学号查找,请输入你要查找的学号:";
cin >> id;
Node* p = head->next;
while (p != nullptr)
{
if (p->id == id)
{
cout << endl;
p->display();
break;
}
p = p->next;
}
}
void print()//打印
{
Node* p = head->next;
cout << "学号" << '\t' << "姓名" << '\t' << "性别" << '\t' << "分数" << endl;
while (p != nullptr)
{
p->display();
p = p->next;
}
}
~Link()
{
Node* p = head;
Node* q;
while (p != nullptr)
{
q = p->next;
delete p;
p = q;
}
}
};
创建的关键代码
for (int i = 0; i < n; i++)//循环插入也可以使用不断增加节点插入
{
Node* s = new Node();//每次调用一次构造函数,next指向空
s->getinfor();//输入
s->next = head->next;//输入的数据下一个应该为空,同时此时头节点下一数据域应该为s,这样实现不断往下增加介点
head->next = s;
}
插入关键代码
Node* p = new Node();//输入的节点
cout << "位置 "; cin >> pos;//默认从0开始
cout << "学号 "; cin >> p->id;
cout << "姓名 "; cin >> p->name;
cout << "性别 "; cin >> p->gender;
cout << "分数 "; cin >> p->score;
Node* s = head->next;
for (int i = 0; i < n; i++)
{
if (i == pos)//找
{
p->next = s->next;//
s->next = p;//
break;//假设名字不重复情况
}
s = s->next;
}
删除
Node* p = head->next;
Node* q = head;//定义前驱节点
while (p != nullptr)
{
if (p->id == id)
{
q->next = p->next;
delete p;
break;
}
p = p->next;
q = q->next;
}
运行过程
void work()
{
Link link;
while (1)
{
show();
switch (num)
{
case 1:link.create(); break;
case 2:link.insert(); break;
case 3:link.dele(); break;
case 4:link.find(); break;
case 5:link.print(); break;
case 0:exit(0);
default:exit(0);
}
}
link.~Link();
}
全部代码
#include <iostream>
#include<cmath>
#include<cstring>
#include<stdlib.h>
using namespace std;
int n, num;
class Node {
public:
int score;
string id;
string name;
string gender;
Node* next;
Node()
{
next = nullptr;
}
void display()
{
cout << id << '\t' << name << '\t' << gender << '\t' << score << endl << endl;
}
void getinfor()
{
cin >> id >> name >> gender >> score;
}
};
class Link {
public:
Node* head;
Link()
{
head = new Node();
}
void create()//创建
{
cout << "请输入人数规模: "; cin >> n;
cout << "学号" << '\t' << "姓名" << '\t' << "性别" << '\t' << "分数" << endl;
//head->next = nullptr;
//Node* s = new Node();
//s = nullptr;
for (int i = 0; i < n; i++)
{
Node* s = new Node();
s->getinfor();
/*head = s;
s->next = nullptr;
head = s;*/
s->next = head->next;
head->next = s;
}
}
void insert()//插入
{
string id, name, gender;
int score, pos;
Node* p = new Node();
cout << "位置 "; cin >> pos;
cout << "学号 "; cin >> p->id;
cout << "姓名 "; cin >> p->name;
cout << "性别 "; cin >> p->gender;
cout << "分数 "; cin >> p->score;
Node* s = head->next;
for (int i = 0; i < n; i++)
{
if (i == pos)
{
p->next = s->next;
s->next = p;
/* s->next = p->next;
p->next = s;*/
break;
}
s = s->next;
}
n++;
cout << endl << "插入后的结果:" << endl;
print();
}
void dele()//删除
{
string id;
cout << "请输入你删除的学号: "; cin >> id;
Node* p = head->next;
Node* q = head;
while (p != nullptr)
{
if (p->id == id)
{
q->next = p->next;
delete p;
break;
}
p = p->next;
q = q->next;
}
n--;
cout << endl << "删除后的结果:" << endl;
print();
}
void find()//查找
{
string id;
cout << "按照学号查找,请输入你要查找的学号:";
cin >> id;
Node* p = head->next;
while (p != nullptr)
{
if (p->id == id)
{
cout << endl;
p->display();
break;
}
p = p->next;
}
}
void print()//打印
{
Node* p = head->next;
cout << "学号" << '\t' << "姓名" << '\t' << "性别" << '\t' << "分数" << endl;
while (p != nullptr)
{
p->display();
p = p->next;
}
}
~Link()
{
Node* p = head;
Node* q;
while (p != nullptr)
{
q = p->next;
delete p;
p = q;
}
}
};
void show()
{
//int n;
system("color 0b");
cout << " 学生信息管理系统 " << endl;
cout << "****************************" << endl;
cout << " 1.输入学生信息 " << endl;
cout << " 2.插入学生信息 " << endl;
cout << " 3.删除学生信息 " << endl;
cout << " 4.查找学生信息 " << endl;
cout << " 5.打印学生信息 " << endl;
cout << " 0.退出学生信息 " << endl;
cout << "****************************" << endl;
cout << " 请输入你要操作的序号:";
cin >> num;
}
void work()
{
Link link;
while (1)
{
show();
switch (num)
{
case 1:link.create(); break;
case 2:link.insert(); break;
case 3:link.dele(); break;
case 4:link.find(); break;
case 5:link.print(); break;
case 0:exit(0);
default:exit(0);
}
}
link.~Link();
}
int main()
{
work();
return 0;
}
以上