第二章 线性表
通过顺序表实现纤线性表的存储
主要内容:记录学生学号与成绩的线性表
#include <bits/stdc++.h>
#include <Windows.h>
using namespace std;
const int maxsize = 1e4 + 10;
//建立学生信息的结构体
struct Student
{
string name;
int ID;
int score;
}s;
//重载结构体的是否等于判断
bool operator==(Student a, Student b)
{
if (a.ID == b.ID && a.name == b.name && a.score == b.score)
return true;
else return false;
}
//重载结构体的输出流<<
ostream& operator<<(ostream& output, Student& s)
{
output << s.name << "\t\t" << s.ID << "\t\t" << s.score << endl;
return output;
}
//创建学生信息的顺序表
struct List
{
Student* element;
int len;
};
List L;
//顺序表的初始化
int init_list(List& L)
{
L.element = new Student[maxsize];
L.len = 0;
return 1;
}
//获取顺序表中某个位置的值
bool get_elem(List L, int i, Student& s)
{
if (i<1 || i>L.len) return false;
s = L.element[i - 1];
return true;
}
//在顺序表的中查找某个元素
int find_elem(List L, Student s)
{
for (int i = 0; i < L.len; i++)
{
if (L.element[i] == s)
return i - 1;
}
return -1;
}
//向顺序表中插入某个元素
bool insert_elem(List& L, int i, Student s)
{
if (i<1 || i>L.len) return false;
if (L.len == maxsize) return false;
for (int j = L.len - 1; j >= i - 1; j--)
L.element[j + 1] = L.element[j];
L.element[i - 1] = s;
++L.len;
return true;
}
//向顺序表中删除某个元素
bool delete_elem(List& L, int i, Student s)
{
if (i<1 || i>L.len) return false;
for (int j = i; j <= L.len - 1; j++)
L.element[j - 1] = L.element[j];
return true;
}
//遍历顺序表
void forr(List L)
{
for (int i = 0; i <= L.len - 1; i++)
cout << L.element[i] << "\n";
}
void mume()
{
system("cls");
printf(" 1 : 增加学生信息 \n");
printf(" 2 : 删除学生信息 \n");
printf(" 3 : 按位置查询学生信息 \n");
printf(" 4 : 查询学生位置 \n");
printf(" 5 : 遍历学生信息 \n");
printf(" 6 : 退出 \n");
int choice;
cout << "请输入功能选项 : ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
{
system("cls");
int position; //该同学的位置
string str;
int id, s;
cin >> str >> id >> s >> position;
Student temp = { str,id,s };
bool b = insert_elem(L, position, temp);
if (b) cout << "插入成功" << endl;
else cout << "出现异常" << endl;
system("pause");
mume();
break;
}
case 2:
{
system("cls");
int position; //该同学的位置
string str;
int id, s;
cin >> str >> id >> s >> position;
Student temp = { str,id,s };
bool b = delete_elem(L, position, temp);
if (b) cout << "删除成功" << endl;
else cout << "出现异常" << endl;
system("pause");
mume();
break;
}
case 3:
{
system("cls");
int position;
cin >> position;
Student s;
bool b = get_elem(L, position, s);
if (b) cout << s << "\n";
else cout << "查无此人" << endl;
system("pause");
mume();
break;
}
case 4:
{
system("cls");
string str;
int id, s;
cin >> str >> id >> s;
Student temp = { str,id,s };
int index = find_elem(L, temp);
if (index == -1) cout << "查无此人" << endl;
else cout << index << endl;
system("pause");
mume();
break;
}
case 5:
{
system("cls");
forr(L);
system("pause");
mume();
break;
}
case 6:
{
return;
}
default:
{
system("cls");
cout << "输入数字错误" << endl;
cout << "即将返回功能列表" << endl;
system("pause");
mume();
break;
}
}
}
int main()
{
cout << "请输入当前学生人数 : ";
int n; //顺序表的元素个数
cin >> n;
cout << "\n";
init_list(L); //创建顺序表
//顺序表的初始化
for (int i = 1; i <= n; i++)
{
string str;
int id, s;
cin >> str >> id >> s;
Student temp = { str,id,s };
L.element[i - 1] = temp;
L.len++;
}
mume();
return 0;
}