顺序表

  • 是用一段地址连续的存储单元依次存储线性表的数据元素。
  • 通常用一维数组来实现

基本操作:

  • 初始化
  • 销毁
  • 求长
  • 按位查找
  • 按值查找
  • 插入元素
  • 删除位置i的元素
  • 判空操作
  • 遍历操作

示例代码:


//声明.h
const int MAXSIZE = 100; template<typename dataType>
class SeqList {
public:
SeqList(); //无参构造
SeqList(dataType a[], int length); //有参构造
~SeqList();
int seqlist_Length();
dataType get_i(int i); //查找第i位的元素
int search_x(dataType x); //查找值为x的元素
void insert_x(int n, dataType x); //在第n位插入值位x的元素
dataType delete_i(int i); //删除第i位元素
void print_list();
private:
dataType data_[MAXSIZE];
int length_;
};
//定义.h
#include<iostream>
using std::cout;
using std::endl; template<typename dataType>
inline SeqList<dataType>::SeqList()
{
length_ = 0;
} template<typename dataType>
inline SeqList<dataType>::SeqList(dataType a[], int length)
{
if (length > MAXSIZE)
{
throw"长度非法";
}
for (int i = 0; i < length; ++i)
{
data_[i] = a[i];
}
length_ = length;
} template<typename dataType>
inline SeqList<dataType>::~SeqList()
{} template<typename dataType>
inline int SeqList<dataType>::seqlist_Length()
{
return length_;
} template<typename dataType>
inline dataType SeqList<dataType>::get_i(int i)
{
if (i > length_ || i < 1)
{
throw"查找位置不存在";
}
else
{
return data_[i - 1];
} } template<typename dataType>
inline int SeqList<dataType>::search_x(dataType x)
{ for (int i = 0; i < length_; ++i)
{
if (data_[i] == x)
{
return i + 1;
}
}
return 0;
} template<typename dataType>
inline void SeqList<dataType>::insert_x(int n, dataType x)
{
if (length_ == MAXSIZE)
{
throw"表已满";
}
if (n >= length_ + 1 || n<1)
{
throw"插入位置非法";
}
for (int i = length_; i >= n; i--)
{
data_[i] = data_[i - 1];
}
data_[n - 1] = x;
++length_;
} template<typename dataType>
inline dataType SeqList<dataType>::delete_i(int i)
{
if (length_ == 0)
{
throw"表为空";
} if (i<1 || i>length_)
{
throw"删除位置非法";
}
int x = data_[i - 1];
for (int j = i; j < length_; j++)
{
data_[j - 1] = data_[j];
}
length_--;
return x;
} template<typename dataType>
inline void SeqList<dataType>::print_list()
{
for (int i = 0; i < length_; i++)
{
cout << data_[i];
}
cout << endl;
}
//main.cpp
#include"announced.h" int main()
{
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

数据结构---顺序表(C++)的更多相关文章

  1. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

  2. hrbust-1545-基础数据结构——顺序表(2)

    http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) ...

  3. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  4. 数据结构顺序表删除所有特定元素x

    顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...

  5. 数据结构顺序表Java实现

    Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...

  6. python算法与数据结构-顺序表(37)

    1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...

  7. 数据结构——顺序表(sequence list)

    /* sequenceList.c */ /* 顺序表 */ /* 线性表的顺序存储是指在内存中用地址连续的一块存储空间顺序存放线性表中的各项数据元素,用这种存储形式的线性表称为顺序表. */ #in ...

  8. 数据结构顺序表中Sqlist *L,&L,Sqlist *&L

    //定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...

  9. Java数据结构——顺序表

    一个线性表是由n(n≥0)个数据元素所构成的有限序列. 线性表逻辑地表示为:(a0,a1,…,an-1).其中,n为线性表的长度,n=0时为空表.i为ai在线性表中的位序号. 存储结构:1.顺序存储, ...

随机推荐

  1. Android_listView

    package com.example.app5; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...

  2. 有关于Algorithm的基础介绍

    Niklaus Wirth:Algorithm + Data Structures = Programs 这句话呢,觉得很正确,算法和程序是不同的概念,算法的思想呢有递推,枚举,分治,贪婪,试探法,模 ...

  3. MongoDB自定义函数部分 定义及引用

    1. //定义一个Sum的函数 db.system.js.save({_id:"Sum", value:function(key,values) { ; ;i <values ...

  4. 猪满满 购物APP

    猪满满是专注“省钱,赚钱”的购物App,使用自定义tabar分为四大类,分别是首页,超返,发现,我的. 首页:使用UItableview,自定义cell展示商品. 超返:自定义Button分为综合,返 ...

  5. Python(2.7.6) 标准日志模块 - Logging Handler

    Python 标准日志模块使用 Handler 控制日志消息写到不同的目的地,如文件.流.邮件.socket 等.除了StreamHandler. FileHandler 和 NullHandler ...

  6. 【MINA】序列化和反序列化我们要考虑的问题

    概念 序列化:将java对象转换为字节序列的过程叫做序列化 反序列化:将字节对象转换为java对象的过程叫做反序列化 要解决的问题 1.序列化时间 2.反序列化时间 3.bytes大小 4.操作方便 ...

  7. IIS经典模式和集成模式在管道模型中的不同

    问题: 有时候我们配置IIS后可能运行提示以下错误: HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 其中由 ...

  8. VMWare中安装CentOS6.6不能上网的解决办法

    1.首先在虚拟机中将网络配置设置成NAT 2.在windows系统,我的电脑-管理-服务 中开启VMware NAT service和VMware DHCP service. 3.在CentOS里面打 ...

  9. 在页面生命周期执行时 Page 对象在 SaveState 阶段都发生了什么事?

    先看下 SaveViewState 的源码: // Answer any state this control or its descendants want to save on freeze. / ...

  10. GridView 自定义表头

    //修改表头 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.Ro ...