数据结构--顺序表的使

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int Maxsize = 100;
template<class T>
class SeqList
{
public:SeqList() {}SeqList(T a[], int n);//~SeqkList() {}int Length();T Get(int i);int Locate(T x);void Insert(int i,T x);T Delete(int i);int Empty();void PrintList();
private:T data[Maxsize];int length;
};
template<class T>
SeqList<T>::SeqList(T a[], int n)
{if (n > Maxsize){cout << "Error";return;}for (int i = 0; i < n; i++){data[i] = a[i];}length = n;
}
template<class T>
int SeqList<T>::Empty()
{if (length == 0)cout << "This List is Empty!";
}
template<class T>
int SeqList<T>::Length()
{return length;
}
template<class T>
void SeqList<T>::PrintList()
{for (int i = 0; i < length; i++)cout << data[i] << "\t";cout << endl;
}
template<class T>
T SeqList<T>::Get(int i) {if (i < 1 || i>length) {cout << "search Error!";return 0;}else {cout << 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 "shangyi!";if (i<1 || i>length)throw "Insert Error!";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) {T x;if (length == 0)throw "xiayi!";if (i<1 || i>length)throw "Delete Error!";for (int j = i; j < length; j++) {data[j - 1] = data[j];}length--;return x;
}
int main()
{int r[5] = { 1,2,3,4,5 };SeqList<int>L(r, 5);cout << "Now the list's order are:";cout << endl;L.PrintList();try {L.Insert(2, 8);cout<< "Now ,as we operatethe list,and its order are:";L.PrintList();cout << endl;}catch (char* str) { cout << str << endl; }cout << "Now the List's length is:"<< L.Length();cout << endl;cout << "Please input what you Insert'element'value:";int x;cin >> x;int i = L.Locate(x);if (i == 0)cout << "search Error!" << endl;elsecout << "element" << x << "in:" << i << endl;//Searchtry {cout << "input you want to search No.element:";cin >> i;cout << "the No." << i << "element is :" << L.Get(i) << endl;}catch (char* str) { cout << str << endl; }//Deletetry {cout << "Please input what you delete No.element:";cin >> i;x = L.Delete(i);cout << "you delete is:" << x;cout << endl;cout << "Now the order are:";L.PrintList();}catch (char* str) { cout << str << endl; }return 0;
}

顺序表。。。不用模板

#include<iostream>
using namespace std;const int Maxsize = 100;class SeqList {
private:int data[Maxsize];int length;
public:// SeqList();SeqList(int data[], int n);~SeqList();int Length();int Get(int i);int Locate(int x);void Insert(int i,int x);int Delete(int i);int Empty();void PrintList();
};//SeqList::SeqList() {}//建立长度为n的顺序表
SeqList::SeqList(int a[], int n) {if (n > Maxsize)throw"参数非法";for (int i = 0; i < n; i++) {data[i] = a[i];}length = n;
}
SeqList::~SeqList() {}int SeqList::Length() {return length;
}int SeqList::Get(int i) {if (i<1 || i>length)throw"查找非法!";else{return data[i - 1];}
}//按值查找
int SeqList::Locate(int x) {for (int i = 0; i < length; i++) {if (data[i] == x)return x + 1;}return 0;
}//插入操作
void SeqList::Insert(int i, int x) {if (i<1 || i>length)throw"插入错误!";if (length == Maxsize)throw"上溢!";for (int j = length; j >= i; j--) {data[j] = data[j - 1];}data[i - 1] = x;length++;
}//删除操作
int SeqList::Delete(int i) {int x;//存下被删元素if (i<1 || i>length)throw"删除错误!";if (length == 0)throw"下溢!";x=data[i - 1];for (int j = i; j < length; j++) {data[j-1] = data[j];}length--;return x;
}//判空
int SeqList::Empty() {if (length == 0)cout << "空表";return 1;
}//遍历
void SeqList::PrintList() {for (int i = 0; i < length; i++)cout << data[i] << "\t";cout << endl;
}
int main() {int arr[5] = { 1,2,4,5,6 };SeqList L(arr, 5);cout << "线性表的数据为:";L.PrintList();try {L.Insert(2, 8);L.Insert(5, 10);cout << "Now the SeqList's data are:";L.PrintList();}catch (char* str) { cout << str; }cout << "Now the length of the SeqList is:" << L.Length() << endl;cout << "Please input you want to find the data:";int x,i;cin >> x;i = L.Locate(x);if (i == 0)cout << "find false!" << endl;else{cout << "data" << x << "'s location is :" << i << endl;}try {cout << "Please input what you want to find the NO.data:" << endl;cin >> i;cout << "the No." << i << "data'value is:" << L.Get(i);cout << endl;}catch (char* str) {cout << str << endl;}try {cout << "Please input what you want to dalete the No.data'value;";cin >> i;x = L.Delete(i);cout << "your delete data is:" << x << endl;cout << "NOw the SeqList'data are:";L.PrintList();}catch (char* str) { cout << str << endl; }return 0;
}

数据结构--顺序表的使用相关推荐

  1. C语言链表的转置算法,c语言编程集 数据结构 顺序表 点链表 数制转换 矩阵转置.doc...

    c语言编程集 数据结构 顺序表 点链表 数制转换 矩阵转置 #include "stdio.h" #include "malloc.h" /*typedef s ...

  2. 数据结构-顺序表(动态分配存储空间)

    数据结构-顺序表(动态分配存储空间) (1)顺序表的结构定义: 结构型定义:(动态分配存储空间) /*** 动态分配存储空间*/ #define InitSize 100 //动态分配存储空间时,不限 ...

  3. C语言/C++常见习题问答集锦[八十三]之数据结构顺序表(operand types are error: no match for “operator==“)

    C语言/C++常见习题问答集锦[八十三]之数据结构顺序表{operand types are error: no match for "operator=="} 程序之美 前言 主 ...

  4. Educoder头歌数据结构顺序表及其应用

    头歌实践平台答案educoder 数据结构-顺序表及其应用 第1关:顺序表的实现之查找功能 /***************************************************** ...

  5. 数据结构——顺序表的合并

    数据结构--顺序表的合并 具体要求:写一个函数,其函数的功能是将非递增顺序表LA和LB合并到非递增顺序表LC中 数据结构-顺序表的操作之合并顺序表 一.顺序表的结构 首先要定义的是顺序表的结构体,只有 ...

  6. python顺序表数组_数据结构 | 顺序表

    什么是数据结构? 数据结构是指相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成. 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表.集合与字典等都 ...

  7. 8.基本数据结构-顺序表和链表

    一.内存 - 计算机的作用:对数据进行存储和运算.首先我们需要知道我们目前使用的计算机都是二进制的计算机,就以为着计算机只可以存储和运算二进制的数据.例如下载好的一部电影,该电影可以存储到计算机中,计 ...

  8. 数据结构顺序表基本流程

    生活中很多事物是有顺序关系的,如班级座位从前到后是按排的顺序,从左到右是按列的顺序,可以很方便的定位到某一个位置,但如果座位是散乱的,就很难定位. 在程序中,经常需要将一组(通常是同为某个类型的)数据 ...

  9. C#数据结构-顺序表

    顺序表,顾名思义存储在计算机指定内存区域的一块连续的存储结构,跟我们一起排队做广播体操的那种方式 存储物理结构:物理内存空间上是连续的 存储逻辑关系:存储值之间的关系为一对一 使用场景:一般访问数据量 ...

最新文章

  1. 程序(进程)内存分布 解析
  2. iphone 使用委托(delegate)在不同的窗口之间传递数据
  3. js oop写法小例子
  4. 《世界已无法阻挡Python入侵!》(附学习资源)
  5. QT Windows下生成动态链接库
  6. opencv python3 文本区域识别_使用等高线从图像中提取文本区域 - Opencv,Python
  7. varnish与squid比较
  8. CSS3属性——“box-flex”
  9. 关于[知识竞赛现场管理系统-双屏PPT版]内置的第三方答题平台以及[评委计分系统-双屏专业版]的特殊疑难问题 汇编
  10. pycharm汉化包
  11. 《手把手教你学DSP》总结1
  12. matlab数值拟合r2_MATLAB数据拟合实例(给出两组数据拟合y=ax±b).doc
  13. 某运动APP登录协议分析
  14. 计算机接口论文摘要,计算机接口技术论文_计算机接口技术
  15. php 公众号授权登录,微信公众号授权登录
  16. 基于python的证件照_利用python自动生成证件照
  17. RN如何使用原生的AndroidUI组件
  18. 房产经纪龙头居安思危孵化「贝壳」,如何用数字化解找房之痛?
  19. 《5G网络协议与客户感知》读书笔记 | 会话管理信令序列
  20. tomcat搭建简易网站

热门文章

  1. [SAP]JCO数据类型映射关系表
  2. cf英文名字格式好看的_cf好听的英文名字分享 让你起合适的名字
  3. 白噪音——真乃助眠神器!
  4. 面向对象编程与关系型数据库间的不一致(Object-relational impedance mismatch)
  5. 最大似然估计(ML)
  6. OPPO连续点击android版本9,oppo游戏中心下载安装正版
  7. SpringBoot系列:Spring Boot定时任务Spring Schedule,springboot视频教程迅雷
  8. 守候在凌晨2点的伤心
  9. ZKP应用:石头剪刀布游戏
  10. 基于QT实现的图的可视化程序地铁换乘指南系统