数据结构---顺序表(C++)
顺序表
- 是用一段地址连续的存储单元依次存储线性表的数据元素。
- 通常用一维数组来实现
基本操作:
- 初始化
- 销毁
- 求长
- 按位查找
- 按值查找
- 插入元素
- 删除位置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++)的更多相关文章
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...
- hrbust-1545-基础数据结构——顺序表(2)
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) ...
- c数据结构 顺序表和链表 相关操作
编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...
- 数据结构顺序表删除所有特定元素x
顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...
- 数据结构顺序表Java实现
Java实现顺序表算法:1:首先我们需要定义我们的接口,关于顺序表的一些基本的操作:顺序表中的操作都有增删改查. //List接口 public interface IList { //返回线性表的大 ...
- python算法与数据结构-顺序表(37)
1.顺序表介绍 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素.顺序表可以分配一段连续的存储空间 ...
- 数据结构——顺序表(sequence list)
/* sequenceList.c */ /* 顺序表 */ /* 线性表的顺序存储是指在内存中用地址连续的一块存储空间顺序存放线性表中的各项数据元素,用这种存储形式的线性表称为顺序表. */ #in ...
- 数据结构顺序表中Sqlist *L,&L,Sqlist *&L
//定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...
- Java数据结构——顺序表
一个线性表是由n(n≥0)个数据元素所构成的有限序列. 线性表逻辑地表示为:(a0,a1,…,an-1).其中,n为线性表的长度,n=0时为空表.i为ai在线性表中的位序号. 存储结构:1.顺序存储, ...
随机推荐
- 修改UILabel的行间距
在iOS开发中 有时候为了调整一些UI效果 我们需要调整UILabel之间的行间距: contentLabel.text:label上显示的文字内容; 5:label行间距; contentLab ...
- 【运维】使用FC命令辅助查杀DLL木马
使用FC命令辅助查杀DLL木马 在windows系统中,system32目录下是木马隐身的好地方,查找起来非常困难,许多木马都削尖了脑袋往那里钻,DLL木马也不例外.针对这一点用户可以在安装好系统和必 ...
- posix thread概述(示例代码)
一个简单的alarm实例 errors.h头文件 #ifndef __ERRORS_H #define __ERORRS_H #include<stdio.h> #include<u ...
- Java队列实现
队列数组实现:队列长度有限,但是考虑到平时一般都使用有界队列,这应该也不算是个缺点 public class Queue { private Object[] objs; private int he ...
- Redis主备复制
Redis 支持 Master-Slave(主从)模式,Redis Server 可以设置为另一个 Redis Server 的主机(从机),从机定期从主机拿数据.特殊的,一个从机同样可以设置为一个 ...
- Executor 和Executors
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...
- Nodejs v4.x.0API文档学习(1)简介
文档参考地址:https://nodejs.org/dist/latest-v4.x/docs/api/ 简介 下面是用nodejs编写的一个web服务的例子,返回"Hello World& ...
- 目前电脑的硬件尺寸参数,计划弄个小一些的ATX机箱
显卡:讯景R9 370x 尺寸:234×115×39mm 主板:技嘉GA-970A-DS3P 尺寸: 30.5X21.5 cm
- JAVA构造器、this、super
构造器是为了创建一个类的实例.这个过程也可以在创建一个对象的时候用到: Platypus p1 = new Platypus(); 相反,方法的作用是为了执行java代码. 修饰符,返回值和命名的不同 ...
- scala学习笔记:match表达式
写了一个超级长的表达式,估计不是最简洁的: scala> def foo(ch:Any)=ch match { case true=>"male";case false ...