这是我当时c++课上的一个作业,完成的不错就分享一下
下面是老师的要求:
要求实现四个函数,分别用于增删改查。
增 默认添加 可增加添加位置
删 删除单个 可批量删除
改 a餐变c餐 ,第5个变C餐(单个指定修改和批量修改)
查 查询A餐顾客有多少个,第5个顾客是什么餐 完成1个18分 两个25分
理论上所有类似的东西都可以套用
下面是代码:
#include<iostream>
#include<cstring>
using namespace std;
struct student {
string name;
int number;
int score;
struct student* next;
};
//打印菜单
void menu() {
cout << "1.增加节点" << endl
<< "2.删除节点" << endl
<< "3.修改节点" << endl
<< "4.查找节点" << endl
<< "5.输出所有节点" << endl
<< "输入0以退出程序" << endl;
}
//增
void addData(student* head, int i) {
student* p = head->next;
//找到要插入的节点
int j = 1;
while (j < i) {
p = p->next;
j++;
}
if (i == j) {
student* q = new student;
cout << "请输入所添加学生的名字,学号以及分数" << endl;
cin >> q->name >> q->number >> q->score;
q->next = p->next;
p->next = q;
}
system("cls");
menu();
}
//删
//删除特定名字的节点
void delData_1(student* head, string* name) {
student* p = head;
student* q = head;
int i = 0;
//找到并删除节点
while (q->next != NULL) {
p = q;
q = q->next;
if (q->name == *(name + i)) {
p->next = q->next;
delete q;
q = p;
i++;
}
}
system("cls");
menu();
}
//删除特定学号的节点
void delData_2(student* head, int* number) {
student* p = head;
student* q = head;
int i = 0;
//找到并删除节点
while (q->next != NULL) {
p = q;
q = q->next;
if (q->number == *(number + i)) {
p->next = q->next;
delete q;
q = p;
i++;
}
}
system("cls");
menu();
}
//删除特定分数的(所有)节点
void delData_3(student* head, int i) {
student* p = head;
student* q = head;
//找到并删除节点
while (q->next != NULL) {
p = q;
q = q->next;
if (q->score == i) {
p->next = q->next;
delete q;
q = p;
}
}
system("cls");
menu();
}
//改
//常规修改,对特定节点进行修改
void changeData_1(student* head, int i) {
student* p = head->next;
//找到要修改的节点
int j = 1;
while (j < i) {
p = p->next;
j++;
}
if (i == j) {
cout << "请输入修改后学生的名字,学号以及分数" << endl;
cin >> p->name >> p->number >> p->score;
}
system("cls");
menu();
}
//对节点中某一属性修改(仅修改分数)(将score_1修改为score_2)
void changeData_2(student* head, int score_1, int score_2) {
student* f = head;
student* s = head;
//找到并修改节点
while (s->next != NULL) {
f = s;
s = s->next;
if (s->score == score_1) {
s->score = score_2;
s = f;
}
}
system("cls");
menu();
}
//查
//查找特定位置数据
void searchData_1(student* head, int i) {
student* p = head->next;
int j = 1;
while (j < i) {
p = p->next;
j++;
}
if (i == j) {
cout << "查找的节点数据为" << endl;
cout << p->name << " " << p->number << " " << p->score << endl;
}
system("pause");
system("cls");
menu();
}
//查找分数相同的数据
void searchData_2(student* head, int score) {
student* f = head;
student* s = head;
int i = 0;
cout << "符合要求的节点数据为" << endl;
while (s->next != NULL) {
f = s;
s = s->next;
if (s->score == score) {
i++;
cout << s->name << " " << s->number << " " << s->score << endl;
}
}
cout << "符合要求的节点数据共有" << i << "个" << endl;
system("pause");
system("cls");
menu();
}
//功能函数
//打印当前链表
void printList(student* head) {
cout << "姓名" << "\t" << "学号" << "\t" << "成绩" << endl;
student* p = head->next;
int n = 0;
while (p) {
n++;
p = p->next;
}
p = head->next;
for (int i = 0; i < n; i++) {
cout << p->name << "\t" << p->number << "\t" << p->score << endl;
p = p->next;
}
}
//获取当前链表的节点数
int getLength(student* head) {
student* t = head->next;
int i = 0;
while (t != NULL) {
t = t->next;
i++;
}
return i;
}
int main() {
//定义节点
student* head = new student;
student* pre = head;
int n, i;
string temp;
//初始化链表
cout << "首次创建需要初始化链表" << endl;
cout << "该链表的初始节点数为:";
cin >> temp;
//输入非数字时
while (temp < "0" || temp> "9") {
cout << "错误的输入,请输入数字" << endl;
temp = '\0';
cin >> temp;
}
n = atoi(temp.c_str());
cout << "请输入链表的初始数据(名字,学号以及分数)" << endl;
for (i = 0; i < n; i++) {
student* p = new student;
cout << "第" << i + 1 << "项数据为:" << endl;
cin >> p->name >> p->number >> p->score;
//建立关系
pre->next = p;
pre = p;
p->next = NULL;
}
system("cls");
menu();
int m, j, x, score, a, b;
while (1) {
//获取当前链表的长度
int length = getLength(head);
string* name = new string[length];
int* number = new int[length];
cout << "输入序号以对链表进行操作:" << endl;
cin >> m;
switch (m) {
case 0:
return 0;
case 1:
cout << "输入节点序号以在其后添加:" << endl;
cin >> j;
if (j > length) {
cout << "错误的节点序号" << endl;
break;
}
addData(head, j);
continue;
case 2:
system("cls");
printList(head);
cout << "输入序号进行特定删除操作" << endl
<< "1.批量删除节点(姓名)" << endl
<< "2.批量删除节点(学号)" << endl
<< "3.删除特定分数的(所有)节点" << endl
<< "4.返回上一级" << endl;
cin >> j;
switch (j) {
case 1:
cout << "请输入删除的学生数量" << endl;
cin >> x;
cout << "输入要删除的学生名字(空格隔开)" << endl;
for (int i = 0; i < x; i++) {
cin >> *(name + i);
}
delData_1(head, name);
break;
case 2:
cout << "请输入删除的学生数量" << endl;
cin >> x;
cout << "输入要删除的学生学号(空格隔开)" << endl;
for (int i = 0; i < x; i++) {
cin >> *(number + i);
}
delData_2(head,number);
break;
case 3:
cout << "输入要删除的学生分数" << endl;
cin >> score;
delData_3(head, score);
break;
case 4:
system("cls");
menu();
break;
}
continue;
case 3:
cout << "输入序号进行特定修改操作" << endl
<< "1.修改特定位置的节点" << endl
<< "2.修改所有同分的节点" << endl
<< "3.返回上一级" << endl;
cin >> j;
switch (j)
{
case 1:
cout << "请输入想要修改的节点序号" << endl;
cin >> x;
changeData_1(head, x);
break;
case 2:
cout << "请输入想要修改的分数和修改后的分数" << endl;
cin >> a >> b;
changeData_2(head, a, b);
break;
case 3:
system("cls");
menu();
break;
}
continue;
case 4:
cout << "输入序号进行特定修改操作" << endl
<< "1.查找特定位置数据" << endl
<< "2.查找分数相同的数据" << endl
<< "3.返回上一级" << endl;
cin >> j;
switch (j) {
case 1:
cout << "请输入需要查找的节点序号" << endl;
cin >> x;
if (x > n) {
cout << "错误的节点序号" << endl;
break;
}
searchData_1(head, x);
case 2:
cout << "请输入需要查找的分数" << endl;
cin >> score;
if (score > 100 || score < 0) {
cout << "错误的分数" << endl;
break;
}
searchData_2(head, score);
break;
case 3:
system("cls");
menu();
break;
}
continue;
case 5:
system("cls");
printList(head);
system("pause");
system("cls");
menu();
continue;
}
}
}