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

从上图可以看到,其基本结构包含一个指向数据数组的头指针,当前表长为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. Tomcat源码调试环境搭建

    我们一般都是为了解决某个问题,才去看源码的.Java体系就是这点好处,源码唾手可得.遇到问题,最后的解决方法总是可以从源码中找到. 参考了网上的文章,过程整理如下: 1. 下载和导入 官网下载编译好的 ...

  2. 复选框之checked属性

    今天无意中看到同事在学习复选框里面的checked属性的应用,当时看了一下,感觉熟悉而又陌生,发现checked属性其实还是挺奇怪的,感觉这里很有必要做一下笔记: 1.html中的checked属性. ...

  3. 在阿里云服务器上安装完成并启动Tomcat后,通过http不能访问--解决办法

    在阿里云服务器上安装完成并启动Tomcat后,通过http不能访问的原因是阿里云平台为了安全设置了安全组策略,必须我们授权的端口,其他计算机才能通过http访问 解决办法:(这里以阿里轻量应用服务器为 ...

  4. jQuery:下拉列表的联动

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  5. 让自己写的项目支持Cocoapods管理

    学会使用别人的 Pods 依赖库以后, 你一定对创建自己的依赖库很有兴趣吧,现在我们一起来制作自己的Pods依赖库. 1.创建自己的 github 仓库 上图中标识出了6处地方 Repository ...

  6. MVVM探索:从ViewModel关闭Window的最佳实践

    在WPF里使用MVVM开发的时候,似乎总是不可避免的会遇到这样一个问题:ViewModel在处理完业务之后需要关闭这个Window,这时候要怎么处理? 网上有很多解决方案:有的在ViewModel抛出 ...

  7. int指令

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  8. 【笔记】vue-cli 打包后路径问题出错的解决方法

    几天之前打包自己的vue 项目上传到远程服务器上面 但是遇到了如下几个问题: 1. 线上浏览页面时是空白页面 2. 打包后资源文件(js, css 文件)引用的路径不正确 3. 开发环境中使用到的如: ...

  9. .net Core学习笔记2 实现列表的条件筛选,排序,分页

    打开vs,完善上次"简单粗暴"的项目 发现上次的实体类的导航属性有点问题,这是更改后的 namespace ProductMvc.Models { public class Pro ...

  10. Tomcat服务器的Web安全的解决方法

    .概述 在任何一种WEB应用开发中,不论大中小规模的,每个开发者都会遇到一些需要保护程序数据的问题,涉及到用户的LOGIN ID和PASSWORD.那么如何执行验证方式更好呢?实际上,有很多方式来实现 ...