数据结构(Data Structure)(第二集)(顺序列表(Sequential List))(C语言)
来源:互联网 发布:js获取disabled属性值 编辑:程序博客网 时间:2024/06/05 19:58
在后面四集所讨论的是关于data structure中的几种基础而且常听到的结构体,列表(Lists),链表(Link List),堆栈(stack),列队(queue), 今天先从顺序列表(Sequential List)开始。
先说说编程中经常运用的数据类型吧!
抽象数据类型是什么?
一般来说,我们大多数运用的数据类型包括:
1. int
2. double/float
3. bool
4. char
以上其实都是由编程语言中的内置类型(build-in data types)。
数据类型的准确定义: 用来形容一系列值的集合(sets of value),以及定义值的一系列操作(sets of operation)
值(value)以变量的形式储存。
今天的重点——抽象数据类型 (Abstract Data Type)
可以简称为 (ADT)
ADT其实是一个数据对象(data object)的模型, 由一个结构体产生。
1. 列表(List)
什么是列表?
顾名思义,就是一系列以A1, A2, A3, … , An的形式存放的列表。
那要怎么定义一个列表的结构体?
我们直接来看一个以数组(array)形式而写成的一个顺序列表(Sequential List)的代码
a. 定义一个顺序列表的数据类型
/*Description: Define a data type of sequential list*/#define MAXLEN 100typedef struct SeqList{ int data[MAXLEN]; //a array that stores some numbers int length; //size of list}
b. 建立一个空顺序表(Create an empty sequential list)
//Create an empty sequential listSeqList *Create_ll(){ SeqList *L; L = (SeqList *) malloc (sizeof(SeqList)); L->length = 0; return L;}
c. 对新的顺序列表进行初始化 Time of Complexity: O(n)
//Initialize a sequential listvoid initialize(SeqList *L){ int i; printf("Please input the length of the list:\n"); scanf("%d", &L->Length); printf("Please input the values of the elements:\n"); for(i=0;i<L->Length;i++){ scanf("%d",&L->data[i]); } }
d. 在列表中进行插入操作 Time of Complexity: O(n)
/*a: 要插入的数字(也可以是char,取决于列表要插入的数据) i: 要插入的位置*/ int Insert(SeqList *L, int a, int i){ int j; //check if list is full(列表是否满) if(isFull(L)){ printf("Sorry,the list is full."); } //check if index is correct(输入的位置是否属于列表) if(i <= 0 || i > L->length){ printf("Sorry, your index has mistakes"); } //Check if index is the last one of list(是否最后一位置) if(i == L->length){ L->data[i] = a; L->length++; } /* the index is in the middle of list, so go through the list from bigger length to smaller length */ for(j = L->length-1; j >= i; j--){ L->data[j+1] = L->data[j]; L->data[i] = a; L->length++; }}
e. 列表中进行删除操作 Time of Complexity: O(n)
/*i: 想删除的位置Description: 这函数可以把某个位置的数字用后面位置的数字代替*///delete a character from arrayvoid Delete_sl(SeqList *L, int i) { int j; for (j = i; j < L->length; j++) { //将后面的数字往前移 L->data[j] = L->data[j+1]; } L->length--;}
f. 查找操作 Time of Complexity: O(n)
//to find the location of characterint Find_sl(SeqList *L, int a){ int i; for(i = 0; i < L->length; i++){ if(L->data[i] == a){ return i; } }}
g. 检查列表是否为空 Time of Complexity: O(1)
//Check if the list is emptyint isEmpty(SeqList *L){ if(L->length == 0){ return TRUE; }else{ return FALSE; }}
h. 检查列表是否已满 Time of Complexity: O(1)
//check if the list is fullint isFull(SeqList *L){ if(L->length == MAXLEN){ return TRUE; }else{ return FALSE; }}
以上就是关于顺序列表结构体的定义, 以下附上如何删除字符串中重复字符的代码
Time of Complexity: O(n^3)
//check if the array has repeated characters for (i = 0; i < L->length; i++) { for (j = i+1; j < L->length; j++) { if (L->data[j] == L->data[i]) { Delete_sl(L, i); break; } } }
Hints:
这最后一小段代码是根据字符串数组来写的,上面structure是根据integer整型来写的,所以大家如果在试代码的时候记得需要修改structure里面的数据类型。
思路: 是用循环的形式将每个字符与后面的字符逐一比较,如果相等,就将其删除(删除方法如上面的删除操作相同)
这一集是我对Sequential List的一些了解,如大家有什么异议,欢迎一起讨论!
- 数据结构(Data Structure)(第二集)(顺序列表(Sequential List))(C语言)
- 数据结构(Data Structure)(第三集)(链表(Linked List))
- 线性表——Data Structure(C语言描述)
- 常用的数据结构(data structure)
- 数据结构(Data Structure) 第一集(算法分析)
- Data Structure?(hdu4217)
- 数据结构(C++) Data Structure learning——第一章 线性表(SEC01)
- 数据结构--顺序栈实现(c语言)
- 【数据结构】实现顺序表(c语言)
- 数据结构-顺序表(C语言)
- 数据结构-顺序栈(C语言)
- 数据结构-顺序队列(C语言)
- 数据结构-循环顺序队(C语言)
- 数据结构(C语言)顺序栈
- 数据结构——C语言描述 第二章(1) 顺序表
- VHDL顺序语句(Sequential Statements)
- 数据结构(Data Structure)——1、栈(Stack)
- 数据结构(Data Structure)——0、数据类型
- @RequestBody, @ResponseBody 注解详解
- 神经网络 I:概述及发展
- 内核对象
- Centos7 下 denyHosts 安装及使用
- 微手云小程序联合领航国际万群联动,跨界融合
- 数据结构(Data Structure)(第二集)(顺序列表(Sequential List))(C语言)
- Java基础总结
- 面试中的那些 Git 问题
- python--二分查找
- openwrt中luci学习笔记
- 关于java中equals与==区别
- OO设计原则 -- OO设计的原则及设计过程的全面总结
- 关于“ 无法启动此程序,因为计算机中丢失opencv_world320.dll”的错误提示
- 【金融财经】金融市场一周简报(2017-10-20)