线性表综合实验(静态链表)
来源:互联网 发布:手机如何管理淘宝店铺 编辑:程序博客网 时间:2024/05/21 09:52
一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二. 实验内容
1.建立一个由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>struct SNode{ DataType data; int next; //指针域(也称游标) };template <class DataType>class StaticLink{public: StaticLink(DataType a[], int n); ~StaticLink(); int Length(); DataType Get(int index); int Locate(string x); void Insert(DataType x, int index); DataType Delete(int i); void PrintList();private: int first; // 定义头指针 int avail; SNode<Student> SList[MaxSize]; //定义结点数组 int length;};template <class DataType>void StaticLink<DataType> ::PrintList(){ int s = first; cout << "-------------------------------------" << endl; cout << "姓名" << "\t" << "语文" << "\t" << "数学" << "\t" << "英语" << "\t" << "总分" << "\t" << endl; for (int i = 0; i <length; i++) { s = SList[s].next; cout << SList[s].data << endl; } cout << "-------------------------------------" << endl;}template <class DataType>int StaticLink<DataType>::Length(){ return length;}template <class DataType>DataType StaticLink<DataType>::Get(int index){ int s = first; for (int i= 0; i <index; i++) { s = SList[s].next; } return SList[s].data;}template <class DataType>int StaticLink<DataType>::Locate(string x){ int i = 1; int s = first; for (; i < length; i++) { s = SList[s].next; if (SList[s].data.name == x) break; } return i;}template <class DataType>void StaticLink<DataType>::Insert(DataType x, int index){ int s = first; for (int i = 1; i <index; i++) { s = SList[s].next; } int i = avail; //此i变量与循环变量i无关 avail = SList[avail].next; SList[i].data = x; SList[i].next = SList[s].next; SList[s].next = i; SList[i].data.Sum = SList[i].data.Chinese + SList[i].data.Math + SList[i].data.English; //赋值Sum length++;}template <class DataType>StaticLink<DataType>::StaticLink(DataType a[], int n){ avail = 1; first = 1; length = 0; for (int i = 0; i < MaxSize; i++) //初始化空间 { SList[i].next= i + 1; } SList[MaxSize].next = -1; for (int i = n-1; i >=0; i--) //逆向赋值,避免逆序输入 { int s = avail; avail = SList[avail].next; SList[s].data = a[i]; SList[s].next = SList[first].next; SList[first].next = s; length++; } }template <class DataType>DataType StaticLink<DataType>::Delete(int i){ int q = 1; int s = first; if (i<1 || i>length) //判断输入位置是否异常 throw "删除位置异常"; else { for (; q < i; q++) //省略第一个条件 { s = SList[s].next; } q = SList[s].next; DataType x = SList[q].data; SList[s].next = SList[q].next; SList[q].next = avail; avail = q; length--; return x; }}template <class DataType>StaticLink <DataType>::~StaticLink(){ length = 0; cout << "链表已成功删除。" << endl;}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; } StaticLink<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 << "在第二与第三间插入“秋分”同学的成绩" << endl; demo.Insert({ "秋分",95,86,93}, 3); demo.PrintList(); cout << "链表总长为:"; cout << demo.Length() << endl; cout << "-------------------------------------" << endl; cout << "删除第3位同学的成绩:" << demo.Delete(3).name<<endl; demo.PrintList(); return 0;}
阅读全文
0 0
- 实验二 线性表综合实验(静态链表)
- 线性表综合实验(静态链表)
- 实验二 线性表综合实验之静态链表
- 实验二 线性表综合实验之《静态链表》
- 实验二 线性表综合实验之《静态链表》
- 线性表综合实验之静态链表的实现
- 线性表综合实验——静态链表
- 《数据结构》实验二:线性表综合实验——(4)静态链表
- 实验二 线性表综合实验(循环链表)
- 实验二 线性表综合实验——静态链表
- 数据结构:实验二线性表综合实验④静态链表
- 线性表综合实验(双链表)
- 线性表综合实验(循环链表)
- 线性表综合实验--双链表
- 实验二 线性表综合实验(单链表)
- 实验二 线性表综合实验(双链表)
- 实验二 线性表的综合实验(c++)
- 实验二 线性表的综合实验(c++)
- C 函数指针与指针函数
- python中DictVectorizer的使用
- linux中date/passwd/file命令
- 数据结构实验:二叉树的遍历(C语言版)
- 机器学习技法:01 Linear Support Vector Machine
- 线性表综合实验(静态链表)
- webstorm激活码
- WebViewDialog默认背景透明的加载网页的对话框, 红包、活动弹窗
- 使用Xshell5连接虚拟机VMware中安装的CentOS7系统
- webdriver保存验证码截图
- 感知机:Perceptron Learning Algorithm
- hadoop之 hadoop 2.2.X 弃用的配置属性名称及其替换名称对照表
- 前端笔记----jquery入门知识点总结
- [BZOJ3994][SDOI2015]约数个数和(莫比乌斯反演)