顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型。它通常包含三个私有成分,即指向数据数组的头指针、当前表长以及表的实际容量。表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组中的每个元素。其基本结构类型如下图所示:

从上图可以看到,其基本结构包含一个指向数据数组的头指针,当前表长为5,但是该顺序表实际表的容量有7。

下面附上实现该数据结构的各项操作代码

在C.h文件中存放可能要用到的C++库:

 #ifndef _C_H_
#define _C_H_
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<algorithm>
#include<ctime>
#include<cstdarg>
#include<assert.h>
using namespace std;
#endif // _C_H_

在SequenceTable.h存放对应的数据结构类型:

 #ifndef _SEQUENCETABLE_H_
#define _SEQUENCETABLE_H_
template<typename T>class STL{
private:
T *elem; //save the base address of STL
int length; //save the current length of STL;
int listsize; //save the opacity of STL
public:
//a function to create k length STL, if k doesn't exist, it can use default function to create 1 length STL
STL(int k=){
elem = new T[k];
length = ;
listsize = k;
}
//destruct STL
~STL(){
delete[]elem;
}
int getLength();
int getListsize();
void Insert(int k, int data); //a function to insert elem in the specific location
void Delete(int k, int data); //a function to delete elem in the specific location
int getElem(int k); //get elem in the specific location
int getLocation(int data);
void ListEmpty();
void showSTL();
}; template<typename T>
int STL<T>::getListsize(){
return listsize;
} template<typename T>
int STL<T>::getLength(){
return length;
} template<typename T>
void STL<T>::Insert(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
int t; //an empty bottle
//insert data while satisfy this situation
while(length<listsize && k<=length){
if(k<length){
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
else{
length++;
t = elem[k];
elem[k] = data;
data = elem[k+];
k++;
}
}
if(k==(length+)){
if(length<listsize){
length++;
elem[k] = data;
}
else{
listsize++;
length++;
elem[k] = data;
}
}
} template<typename T>
void STL<T>::Delete(int k, int data){
//confirm whether the k is reasonable
if(k <= || k > (length+)){
cout<<"k is unreasonable!"<<endl;
}
//insert data while satisfy this situation
if(elem[k]==data){
while(k<=length){
if(k<length){
elem[k] = elem[k+];
k++;
}
else{
k++;
}
}
length--;
}
else{
cout<<"delete error!"<<endl;
}
} template<typename T>
int STL<T>::getLocation(int data){
int i = ; //consider when the length is 0 but the listsize is 1
while(i<=length){
if(elem[i] == data){
return i;
}
i++;
}
} template<typename T>
int STL<T>::getElem(int k){
if(k<= || k>=(length+)){
cout<<"k is unreasonable!"<<endl;
}
else{
return elem[k];
} } template<typename T>
void STL<T>::ListEmpty(){
length = ;
} template<typename T>
void STL<T>::showSTL(){
for(int i=;i<=length; i++){
cout<<elem[i]<<endl;
}
}
#endif

然后再main.cpp文件中实现对该数据结构的调用:

 #include "C.h"
#include "SequenceTable.h"
typedef int T;
int main(){
STL<T> L;
for(int i=; i<;i++){
L.Insert(i+, i+);
}
cout<<L.getLocation()<<endl;
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.showSTL();
int a = L.getElem();
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
L.Delete(,);
L.showSTL();
cout<<L.getLength()<<endl;
cout<<L.getListsize()<<endl;
}

运行实现:

C++数据结构学习之顺序表的更多相关文章

  1. C++ 数据结构学习一(顺序表)

    //SequentialList.h 顺序表模板类 #ifndef SEQUENTIAL_LIST_HXX#define SEQUENTIAL_LIST_HXX using std::cout; us ...

  2. 【数据结构】之顺序表(Java语言描述)

    之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...

  3. 【数据结构】之顺序表(C语言描述)

    顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...

  4. C语言学习笔记-顺序表

    #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...

  5. 数据结构 单链表&顺序表

    顺序表: 一般使用数组(C语言中的数组采用顺序存储方式.即连续地址存储)来描述. 优点:在于随机访问元素, 缺点:插入和和删除的时候,需要移动大量的元素. 链表: 优点:插入或删除元素时很方便,使用灵 ...

  6. 数据结构——Java实现顺序表

    一.分析 什么是顺序表?顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表.一个标准的顺序表需要实现以下基本操作: 1.初始化顺序表 ...

  7. 数据结构之线性顺序表ArrayList(Java实现)

    一.ListMe接口: import java.util.ArrayList; //实现线性表(顺序表和链表)的接口://提供add get isEmpty size 功能public interfa ...

  8. 数据结构之动态顺序表(C实现)

    线性表有2种,分为顺序表和链表. 顺序表: 采用顺序存储方式,在一组地址连续的存储空间上存储数据元素的线性表(长度固定) 链表: 有3种,单链表.双向链表.循环链表(长度不固定) seqList.h ...

  9. 【c++版数据结构】之顺序表的实现

    SeqList.h #ifndef SEQLIST_H #define SEQLIST_H #include<iostream> using namespace std; typedef ...

随机推荐

  1. Solr学习笔记2(V7.2)---导入自己的数据

    学而不思则罔,思而不学则殆,总是看文档不动手效果是不好的.没有实地的从自己的数据库获取数据测试一下始终是空,总结一下自己的操作步骤吧. 第一步准备配置文件 E:\Solr\server\solr\co ...

  2. php表单提交并发送邮件给某个邮箱(示例源码)

    今天老板要求做一个需求,在官网上做一个页面提交的表单,并且当表单点击后,把表单的内容直接提交并通过发送邮件的方式到老板指定的邮箱,下面就分享 一下我的做法 首先建立一个html文档,把页面制作好,并且 ...

  3. nginx.conf文件

    user  www www; worker_processes auto; error_log  /home/wwwlogs/nginx_error.log  crit; pid        /us ...

  4. Java 中判断类和实例之间的关系

    判断类与实例的关系有以下三种方式 1.instanceof关键字,用来判断对象是否是类的实例 (对象 => 类 )   2.isAssignableFrom,用来判断类型间是否存在派生关系 (类 ...

  5. eclipse导入项目之后报错

    一.项目本身就有错 二.jdk版本的问题 参考网址:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html 从别的地方导入一个项目 ...

  6. U8g2库I2C总线再次突破性调试成功

    这次采用U8X8的方式,不再采用u8g2,因为后者一直报内存问题.所以采用了不占内存的u8x8模式 同时u8g2库文件的示例代码也是很牛逼的, 里面还有message box 非常的可爱运行效果也非常 ...

  7. 智能家居esp8266对接机智云

    依然存在稳定性问题 机智云官网--机智云 一个比较详细的教程--esp8266 一开始采用的是esp12f   可是他太不稳定,总是掉线,机智云的固件我也是刷了无数遍,哎太难了. 我比较懒,走过了太多 ...

  8. OpenFlow交换机的实现总结

    先粗略介绍,后续会逐渐完善. OpenFlow交换机通过使用OpenFlow协议的安全通道与控制器进行通信.其具体实现如下示意图所示: 对于一个新到达的数据流,交换机通常的做法是,把该数据包发送给控制 ...

  9. Matlab产生TestBeach所需要的波形数据

    在用vivado仿真的时候,很多情况下需要自己产生波形来代替AD采样波形.以前的做法都是用DDS内部产生所需要的波形来模仿外部输入,后来发现这种做法不仅麻烦,而且不易修改,对仿真很不友好.于是改用ma ...

  10. AppScan 工作原理

    Rational AppScan(简称 AppScan)其实是一个产品家族,包括众多的应用安全扫描产品,从开发阶段的源代码扫描的 AppScan source edition,到针对 Web 应用进行 ...