一.实验目的
巩固间接寻址的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二. 实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。用间接寻址来实现,分别输出结果。
#include <iostream>
#include<string>
using namespace std;
const int MaxSize = 10;
struct Student //定义Student结构体
{
string name;
int Chinese;
int Math;
int English;
int Sum;
};
ostream& operator << (ostream& os, const Student &ob) //重载左移运算符,使其能直接输出自定义类型“Student”
{
os << ob.name << "\t";
os << ob.Chinese << "\t";
os << ob.Math << "\t";
os << ob.English << "\t";
os << ob.Sum << "\t";
return os;
}
template <class DataType>
class AddressList
{
public:
AddressList(DataType a[], int n);
~AddressList();
int Length();
DataType Get(int index);
int Locate(string x);
void Insert(DataType x, int index);
DataType Delete(int i);
void PrintList();
private:
int length;
DataType **table; //模拟指针表, table指向数组,数组中存储类型DataType的指针
};
template <class DataType>
void AddressList<DataType> ::PrintList()
{
cout << "-------------------------------------" << endl;
cout << "姓名" << "\t" << "语文" << "\t" << "数学" << "\t" << "英语" << "\t" << "总分" << "\t" << endl;
for (int i = 0; i <length; i++)
{
cout << *table[i]<<endl;
}
cout << "-------------------------------------" << endl;
}
template <class DataType>
int AddressList<DataType>::Length()
{
return length;
}
template <class DataType>
DataType AddressList<DataType>::Get(int index)
{
if (index<0 || index>length)
throw "查找位置异常";
return *table[index-1];
}
template <class DataType>
int AddressList<DataType>::Locate(string x)
{
int i = 0;
for (; i < length; i++)
{
if (table[i]->name == x)
return i+1;}
return -1;
}
template <class DataType>
void AddressList<DataType>::Insert(DataType x, int index)
{
if (index<0 || index>length)
throw "插入位置异常";
if (length == MaxSize)
throw "数据已满!";
index = index - 1; //因为数组元素从0开始,所以令index-1
for (int i = length - 1; i >= index; i--)
{
//第index个元素之后的元素向后移动一位
table[i + 1] = table[i];
}
table[index] = new Student; // 新建一个间接地址
*table[index] = x;
table[index]->Sum = table[index]->Chinese + table[index]->Math + table[index]->English; //对Sum赋值
length++;
}
template <class DataType>
AddressList<DataType>::AddressList(DataType a[], int n)
{
table = new Student*[MaxSize]; //初始化模拟指针
length = 0;
for (int i = 0; i < n; i++)
{
table[i] = new Student; //新建一个间接地址
*table[i] = a[i];
length++;
}
}
template <class DataType>
DataType AddressList<DataType>::Delete(int index)
{
if (index<0 || index>length)
throw "删除位置异常";
index = index - 1; //因为数组元素从0开始,所以令index-1
DataType x = *table[index]; //暂存被删元素
for (int i = index; i < length; i++)
{
//将第index个元素之后的元素向前移动一个
table[i] = table[i+1];
}
length--;
return x;
}
template <class DataType>
AddressList <DataType>::~AddressList()
{
for (int i = 0; i < length; i++) //遍历删除
{
delete table[i];
}
delete[] table;
}
int main()
{
Student stu[MaxSize]; //最大容纳量
int num;
string TempName;
cout << "请输入学生人数:";
cin >> num;
for (int i = 0; i < num; i++)
{
cout << "输入“break”退出输入" << endl;
cout << "请输入第" << i + 1 << "名学生姓名:";
cin >> TempName;
if (TempName == "break")
{
num = i;
break;
}
stu[i].name = TempName;
cout << "请输入第" << i + 1 << "名学生语文成绩:";
cin >> stu[i].Chinese;
cout << "请输入第" << i + 1 << "名学生数学成绩:";
cin >> stu[i].Math;
cout << "请输入第" << i + 1 << "名学生英语成绩:";
cin >> stu[i].English;
stu[i].Sum = stu[i].Chinese + stu[i].Math + stu[i].English;
cout << "-------------------------------------" << endl;
}
AddressList<Student> demo(stu, num);
demo.PrintList();
cout << "链表总长为:";
cout << demo.Length() << endl;
cout << "-------------------------------------" << endl;
cout << "查找第三名学生成绩:" << endl;
cout << "-------------------------------------" << endl;
cout << "姓名" << "\t" << "语文" << "\t" << "数学" << "\t" << "英语" << "\t" << "总分" << "\t" << endl;
cout << demo.Get(3) << endl;
cout << "-------------------------------------" << endl;
cout << "查找姓名为“小明”的位置:" << demo.Locate("小明") << endl;
cout << "-------------------------------------" << endl;
cout << "链表总长为:";
cout << demo.Length() << endl;
cout << "-------------------------------------" << endl;
cout << "删除第3位同学的成绩:" << demo.Delete(3).name << endl;
demo.PrintList();
return 0;
}
输入
输出
三、实验心得
做了前几个链表和顺序表的实验后,写间接寻址的模板比前面轻松了许多,我个人感觉就是前两个的模板结合在一起,有好也有不好,好处是在按位查找时省力很多,坏处是牺牲了插入和删除这两个小功能的性能,代码也相对复杂。