数据结构---顺序表(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.顺序存储, ...
随机推荐
- Linux shell之数组
引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...
- jqzoom基于jQuery的图片放大镜
1.引入jQuery和jqzoom插件 <script src="/js/common/jquery-1.6.2.js" type="text/javascript ...
- Bootloader简介
来介绍一下Bootloader,在专用的嵌入式开发板上运行GNU/Linux 系统已经变得越来越流行.一个嵌入式Linux 系统从软件的角度看通常可以分为四个层次: 1.引导加载程序.包括固化在固件( ...
- nodejs 微信中使用file组件上传图片在某些机型上点击无反应
看下下面的代码: <form action="/" class="file_upload" method="post" enctype ...
- highcharts 去掉右下角链接
去掉右下角的highcharts.com链接需要加入以下代码: credits: { enabled:false }, 如果不设置,那么默认为显示.
- windows server 2012R2 网络慢的那些事
前段时间公司新采购了一台ibm的服务器,装的是 windows server 2012R2, 在做完项目迁移后,发现项目访问数据库缓慢,于是逐项查找原因,最后终于找到解决办法 以Administrat ...
- 移动端web学习总结
前言: 一直想做一个移动端的阶段性学习总结,但是工作太忙总是加班.现在总算可以抽出一点时间来写一写,把知道的都写下来,这样就算忘掉了,也能很快想起来,不要太机智啊,哈哈哈! 一.移动端页面常识 1.常 ...
- 收藏2个mongodb connector网址
https://github.com/plaa/mongo-spark https://github.com/mongodb/mongo-hadoop http://codeforhire.com/2 ...
- 使用jsonp跨域请求
一.异步对象,不能实现跨域请求 在站点A中访问站点B的数据: 站点A代码: window.onload = function () { document.getElementById("bt ...
- asp.net中常用的几种身份验证方式
转载:http://www.cnblogs.com/dinglang/archive/2012/06/03/2532664.html 前言 在B/S系统开发中,经常需要使用"身份验证&q ...