数据结构:实验二线性表综合实验①顺序表
来源:互联网 发布:美国人性格 知乎 编辑:程序博客网 时间:2024/05/16 03:46
一.实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二.实验内容
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
三.实验报告
1.实验代码如下:
#include <iostream>using namespace std;const int MaxSize = 30;template <class T>class SeqList{public: SeqList() { length = 0; } SeqList(T a[], int n); ~SeqList() { } int Length() { return length; } T Get(int i); int Locate(T x); void Insert(int i, T x); T Delete(int i); void PrintList();private: T data[MaxSize]; int length; int i;};template <class T>SeqList<T>::SeqList(T a[], int n){ if (n > MaxSize)throw"参数非法"; for (i = 0; i < n; i++) data[i] = a[i]; length = n;}template <class T>T SeqList<T> ::Get(int i){ if (i < 1 && i > length) throw "查找位置非法"; else return data[i - 1];}template <class T>int SeqList<T> ::Locate(T x){ for (int i = 0; i < length; i++) if (data[i] == x) return i + 1; return 0;}template <class T>void SeqList<T> ::Insert(int i, T x){ if (length >= MaxSize) throw "上溢错误"; if (i < 1 || i > length + 1) throw "位置"; for (int j = length; j >= i; j--) data[j] = data[j - 1]; data[i - 1] = x; length++;}template <class T>T SeqList<T> ::Delete(int i){ if (length == 0) throw "下溢"; if (i < 1 || i > length) throw "位置"; T x = data[i - 1]; for (int j = i; j < length; j++) data[j - 1] = data[j]; length--; return x;}template <class T>void SeqList<T> ::PrintList(){ for (int i = 0; i < length; i++) cout << data[i] << ",";}void main(){ int score[5] = { 66, 71, 68, 45, 93 }; SeqList<int> ScoreList(score, 5); cout << "学生成绩为:" ; ScoreList.PrintList(); try { ScoreList.Insert(2, 63); } catch (char *s) { cout << s << ", " << endl; } cout << endl<<"执行插入操作后数据为:" ; ScoreList.PrintList(); cout << endl<<"成绩为63的元素位置为:"; cout << ScoreList.Locate(63) << endl; cout << endl<<"执行删除第1个元素操作,删除前成绩为:" <<endl; ScoreList.PrintList(); try { ScoreList.Delete(1); } catch (char *s) { cout << s << ", " << endl; } cout <<endl<< "删除后成绩为:" << endl; ScoreList.PrintList();}
2.相关操作的算法表达
插入操作:1.如果表满了,则抛出上溢异常;
2.如果元素的插入位置不合理,则抛出位置非法;
3.将最后一个元素直至第i个元素分别向后移动一个位置;
4.将元素x填入位置i处;
5.表长加1。
删除操作:1.如果表空,则抛出下溢异常;
2.如果删除位置不合理,则抛出删除位置非法;
3.取出被删除元素;
4.将下标为i,i+1,…,n-1处的元素分别移到下标i-1,i,…,n-2处;
5.表长减1,返回被删除值。
查找操作:(1)按位查找
1. 在顺序表中查找第i个元素存储在数组中下标为i-1的位置;
2. 找到即输出数据。
(2)按值查找
1.从第一位开始依次查找与x值相同的元素;
2.找到即输出下标为i的元素的序号i+1;
3.查找失败即退出循环。
输出操作:1.按照下标,依次输出各元素。
3.实验结果
四.实验总结
建立一个由5个学生成绩的顺序表,学生成绩分别为66, 71, 68, 45, 93 ,对表在位置2插入63,插入后学生成绩为66, 63,71, 68, 45, 93 ,删除位置3的成绩71,删除后为66, 63, 68, 45, 93 .
- 数据结构:实验二线性表综合实验①顺序表
- 《数据结构》实验二 线性表综合实验
- 《数据结构》实验二: 线性表综合实验——(1)顺序表
- 实验二 线性表综合实验之顺序表
- 实验二 线性表综合实验——顺序表
- 实验二 线性表综合实验之《顺序表》
- 实验二线性表综合实验-顺序表
- 数据结构:实验二线性表综合实验②单链表
- 数据结构:实验二线性表综合实验③双链表
- 数据结构线性表的综合实验——顺序表
- 《数据结构》实验二:线性表的实验(顺序表)
- 《数据结构》实验二:线性表的实验 【顺序表】
- 《数据结构》实验二:线性表实验(顺序表)
- 《数据结构》实验二:线性表的实验(顺序表)
- 实验二线性表综合实验
- 实验二 线性表综合实验(1)
- 实验二 线性表综合实验
- 实验二线性表综合实验3
- 高可用的一些思考和理解
- Tomcat配置 直接用ip访问项目
- 【LeetCode】C# 14、Longest Common Prefix
- Automated Variable Weighting in k-Means Type Clustering
- 数据库管理工具Navicat Premium
- 数据结构:实验二线性表综合实验①顺序表
- gcc/g++参数介绍
- 20、C#里面方法的创建和显示
- android中AlertDialog包含EditText时弹出键盘(dia和软键盘同时弹出)
- Leetcode 169. Majority Element (第四周作业)
- html的几个标签
- Ubuntu 16.04 安装 Octave-4.2.1
- 数据挖掘Apriori算法
- 数据结构5————栈的应用2-非递归解决迷宫和马踏棋盘问题