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

从上图可以看到,其基本结构包含一个指向数据数组的头指针,当前表长为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++数据结构学习之顺序表的更多相关文章
- C++ 数据结构学习一(顺序表)
//SequentialList.h 顺序表模板类 #ifndef SEQUENTIAL_LIST_HXX#define SEQUENTIAL_LIST_HXX using std::cout; us ...
- 【数据结构】之顺序表(Java语言描述)
之前总结过使用C语言描述的顺序表数据结构.在C语言类库中没有为我们提供顺序表的数据结构,因此我们需要自己手写,详细的有关顺序表的数据结构描述和C语言代码请见[我的这篇文章]. 在Java语言的JDK中 ...
- 【数据结构】之顺序表(C语言描述)
顺序表是线性表的一种,它将元素存储在一段连续的内存空间中,表中的任意元素都可以通过下标快速的获取到,因此,顺序表适合查询操作频繁的场景,而不适合增删操作频繁的场景. 下面是使用 C语言 编写的顺序表的 ...
- C语言学习笔记-顺序表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "coni ...
- 数据结构 单链表&顺序表
顺序表: 一般使用数组(C语言中的数组采用顺序存储方式.即连续地址存储)来描述. 优点:在于随机访问元素, 缺点:插入和和删除的时候,需要移动大量的元素. 链表: 优点:插入或删除元素时很方便,使用灵 ...
- 数据结构——Java实现顺序表
一.分析 什么是顺序表?顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表.一个标准的顺序表需要实现以下基本操作: 1.初始化顺序表 ...
- 数据结构之线性顺序表ArrayList(Java实现)
一.ListMe接口: import java.util.ArrayList; //实现线性表(顺序表和链表)的接口://提供add get isEmpty size 功能public interfa ...
- 数据结构之动态顺序表(C实现)
线性表有2种,分为顺序表和链表. 顺序表: 采用顺序存储方式,在一组地址连续的存储空间上存储数据元素的线性表(长度固定) 链表: 有3种,单链表.双向链表.循环链表(长度不固定) seqList.h ...
- 【c++版数据结构】之顺序表的实现
SeqList.h #ifndef SEQLIST_H #define SEQLIST_H #include<iostream> using namespace std; typedef ...
随机推荐
- js keys方法和foreach方法区别
keys和foreach都有遍历对象的功能,但他们可以遍历的对象类型是不一样的,foreach是数组对象的方法,而keys是Object对象的方法.换句话说,foreach只能数组对象使用,而keys ...
- React Native之Image组件
同 HTML 的 img 元素一样,React Native 提供的 Image 组件可以用来显示各种途径的图片,比如网络图片.本地图片.照相机图片等. 虽然效果是一样的.然而用法还是有区别的. 1. ...
- 利用JAVA API远程进行HDFS的相关操作
学习HDFS有一段时间了,现在把自己总结的HDFS的相关操作代码展示给大家. 主要有HDFS的增删改查,文件的追加,windows本地文件的上传,hdfs文件的下载,文件重命名,创建目录,文件是否存在 ...
- iOS关闭键盘的两种简单方法
方法一: //1 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; ,为了关闭弹出的软键盘要遍历然后调用resig ...
- Java 对二值化图片识别连通域
用Java 对 已经 二值化了的图片 标记连通域 每块的连通域都标记不一样的数字 public static void main(String [] args) throws IOException ...
- JAVA正则表达式 Pattern和Matcher
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...
- 全新的软件项目,好的开始决定了成功一半!(需求&计划)
刚看完“无问西东”,电影里说人总归还是要留下些足迹(文字)的,那么赶紧跑图书馆来留下些文字. 最近去瑞士启动了一个新的项目,那么早上做项目,晚上总结留下了一张张思维导图来记录当时的感受, 手稿如下,字 ...
- banner无缝轮播【小封装】
转载:http://www.qdfuns.com/notes/23446/d1691a1edf5685396813cc85ae6ab10f.html 一直在重复的写banner,写了了好几个,然后每次 ...
- linux的nvme驱动需要关心的统计项
blk-mq-sysfs.c生成了一些其他的nvme的统计项, 有多少个online的cpu,在驱动加载的时候会默认生成多少个队列,除非内存不足或者在保留内核中,则会减少. [root@localho ...
- python_18_反射
什么是反射? -- 通过输入字符串来获取和修改 类(属性+方法),用字符串来映射内存对象,用于人机交互 反射有哪几种方法? -- getattr() --获取字符串 ...