1 顺序表 ADT

+ Status InitList(SeqList &L) 初始化顺序表

+ void printList(SeqList L) 遍历顺序表

+ int ListLength(SeqList L) 获得表长

+ Status GetElement(SeqList L, int i, ElementType &e) (按位)取值 

+ int LocateElement(SeqList L, ElementType e) (按值)查找 

+ Status ListInsert(SeqList &L, int i, ElementType e) (按位)插入 

+ Status ListDelete(SeqList &L, int i) (按位)删除 

2 编程实现

2.1 定义基础数据类型

ElementType (数据元素类型/结构体)

struct ElementType {
char data; // char -> ElementType bool operator==(const ElementType b) const{ // 重载结构体 ElementType 的运算符
return this->data == b.data;
} bool operator!=(const ElementType b) const{
return this->data != b.data;
}
};

Status (状态/枚举类型)

enum Status { ERROR, OK, OVERFLOW };

SeqList (顺序表/结构体)

#define MAXSIZE_SEQLIST 100

typedef struct{
ElementType *elements; // 存储空间的基地址
int length; // 当前长度
} SeqList; // 线性表之顺序表

2.2 初始化顺序表

Status InitList(SeqList &L)

Status InitList(SeqList &L){
L.elements = new ElementType[MAXSIZE_SEQLIST]; //为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elements){
exit(OVERFLOW); // 存储分配失败 -> (异常)退出
}
L.length = 0;
return OK;
}

2.3 遍历顺序表 

void printList(SeqList L)

void printList(SeqList L){
printf("[SeqList.h#printList] L.length: %d\n", L.length);
printf("[SeqList.h#printList] L.elements: \t");
for(int i=0; i< L.length; i++){
printf("%c \t", L.elements[i]);
}
printf("\n");
}

2.4 获得表长

int ListLength(SeqList L)

int ListLength(SeqList L){
return L.length;
}

2.5 (按位)取值 

Status GetElement(SeqList L, int i, ElementType &e)

Status GetElement(SeqList L, int i, ElementType &e){
if(i<1 || i>L.length){ // 判断i值是否合理,若不合理:返回 ERROR
return ERROR;
}
e = L.elements[ i-1 ]; // 赋值; elements[i-1] 单元存储的第 i 个数据元素
return OK;
} 

2.6 (按值)查找 

int LocateElement(SeqList L, ElementType e)

int LocateElement(SeqList L, ElementType e){
for(int i=0; i<L.length; i++){
if(L.elements[i] == e){ //(前提:已重载结构体 ElementType 的'=='运算符)
return i+1;
}
}
return -1;
}

2.7 (按位)插入 

Status ListInsert(SeqList &L, int i, ElementType e)

Status ListInsert(SeqList &L, int i, ElementType e){
if(i<1 || i>L.length+1){ // i 值不合法 (易错: 忘记 +1)
return ERROR;
}
if(L.length == MAXSIZE_SEQLIST){ // 当前存储空间已满
return ERROR;
}
for(int j=L.length; j>=i; j--){ // 插入位置 及之后的元素后移
L.elements[j] = L.elements[j-1];
}
L.elements[i-1] = e; //将新元素 e 放入 第 i 个位置
L.length += 1; //表长 + 1
return OK;
}

2.8 (按位)删除 

Status ListDelete(SeqList &L, int i)

Status ListDelete(SeqList &L, int i){
if(i<1 || i>L.length){ // i 值不合法
return ERROR;
}
for(int j=i-1; j<=L.length-1; j++){ // 删除位置 及之后的元素前移
L.elements[j] = L.elements[j+1];
}
L.length -= 1;
return OK;
}

3 测试运行(Main.cpp)

#include <stdio.h>
//#include <iostream.h>
#include <iostream>
using namespace std; #include "base.h"
#include "SeqList.h" int main(){
SeqList L; // 顺序表 InitList(L); ElementType e;
e.data = 'F';
ListInsert(L, 1, e);
printList(L); e.data = 'G';
ListInsert(L, 1, e);
printList(L); Status status = GetElement(L, 1, e);
printf("status: %d\n", status); status = ListDelete(L, 1);
printList(L); printElementType(e); return 0;
}

运行结果

[SeqList.h#printList] L.length: 1
[SeqList.h#printList] L.elements: F
[SeqList.h#printList] L.length: 2
[SeqList.h#printList] L.elements: G F
status: 1
[SeqList.h#printList] L.length: 1
[SeqList.h#printList] L.elements: F
[base.h#printElementType] ElementType.data: G

4 参考资料

1 《数据结构(C语言版 第二版)》.严蔚敏.李冬梅.吴伟民

[C++]数据结构:线性表之顺序表的更多相关文章

  1. [数据结构 - 第3章] 线性表之顺序表(C++实现)

    一.类定义 顺序表类的定义如下: #ifndef SEQLIST_H #define SEQLIST_H typedef int ElemType; /* "ElemType类型根据实际情况 ...

  2. C#线性表之顺序表

    线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...

  3. 数据结构Java实现02----线性表与顺序表

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  4. 数据结构Java实现01----线性表与顺序表

    一.线性结构: 如果一个数据元素序列满足: (1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素: (2)第一个数据元素没有前驱数据元素: (3)最后一个数据元素没有 ...

  5. [C++]线性链表之顺序表<一>

    顺序表中数据元素的存储地址是其序号的线性函数,只要确定了存储顺序表的起始地址(即 基地址),计算任意一个元素的存储地址的时间是相等的,具有这一特点的存储结构称为[随机存储]. 使用的基本数据结构:数组 ...

  6. c/c++ 线性表之顺序表

    线性表之顺序表 存储在连续的内存空间,和数组一样. 下面的代码,最开始定义了一个能存8个元素的顺序表,当超过8个元素的时候,会再追加开辟空间(函数:reInit). 实现了以下功能: 函数 功能描述 ...

  7. [C++]线性链表之顺序表<二>

    /*   @content 线性链表之顺序表   @date 2017-3-21 1:06   @author Johnny Zen  */ /* 线性表     顺序表     链式表[带头指针/不 ...

  8. python基础下的数据结构与算法之顺序表

    一.什么是顺序表: 线性表的两种基本的实现模型: 1.将表中元素顺序地存放在一大块连续的存储区里,这样实现的表称为顺序表(或连续表).在这种实现中,元素间的顺序关系由它们的存储顺序自然表示. 2.将表 ...

  9. 线性表之顺序表C++实现

    线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...

随机推荐

  1. 「Django」Django内置email发送邮件

    Django内置email发送邮件 1.首先在settings.py文件设置相关参数 STATIC_URL = '/static/' # 设置邮件域名 EMAIL_HOST = 'smtp.163.c ...

  2. node.js接收异步任务结果的两种方法----callback和事件广播

    事件广播 发送方调用emit方法,接收方调用on方法,无论发送方或是接收方,都会工作在一个频道 声明了一个模块,用于读取mime.json中的记录 var fs = require('fs'); va ...

  3. Java内存模型与垃圾回收笔记

    内存模型 栈. 局部变量(基本类型)与对象引用:线程隔离.每个方法执行时会创建一个栈帧,存储局部变量等. 堆. 对象实例:线程共享. 方法区.类信息.常量(final).静态变量.符号引用: 线程共享 ...

  4. [2019牛客多校第三场][G. Removing Stones]

    题目链接:https://ac.nowcoder.com/acm/contest/883/G 题目大意:有\(n\)堆石头,每堆有\(a_i\)个,每次可以选其中两堆非零的石堆,各取走一个石子,当所有 ...

  5. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products

    链接: https://codeforces.com/contest/1247/problem/D 题意: You are given n positive integers a1,-,an, and ...

  6. 8、组件注册-@Import-给容器中快速导入一个组件

    8.组件注册-@Import-给容器中快速导入一个组件 8.1 给容器中注册组建的方式 包扫描+组建标注注解(@Controller.@Service.@Repository.@Component)[ ...

  7. 【安卓基础】WebView开发优化基础

    最近工作很忙,不仅要抽空进行管理,还有开发任务在身,幸好有一些规划进行指导,所以还能顺利解决问题.在管理和技术上面,我认为技术是硬实力,管理是软实力,自己需要多点时间花在技术上. 回归正题,在项目中的 ...

  8. Django-常用异常

    1 from rest_framework.authentication import BasicAuthentication raise AuthenticationFailed(res.dict) ...

  9. [python]函数默认参数顺序问题

    python 函数参数定义有四类: 1.必选参数:调用函数时候必须赋值的参数. a,须以正确的顺序传入函数b,调用时的数量必须和声明时的一样 def exa(x): return x #b作为参数进入 ...

  10. CF1217C

    CF1217C 题意: 给定一个01串,一个good01串的定义是这个01串所代表的二进制数字刚好等于它的长度,允许前导零,问这个01串当中有几个good子串 解法: 枚举每一段连续的 $ 0 $ , ...