做实验用哪些国外网站,科技布沙发脏了用什么办法清洗,项目建设背景与必要性,wordpress获取站点副标题本文由睡觉待开机原创#xff0c;转载请注明出处。 本内容在csdn网站首发 欢迎各位点赞—评论—收藏 如果存在不足之处请评论留言#xff0c;共同进步#xff01; 这里写目录标题 1.数据结构2.顺序表线性表顺序表的结构 3.动态顺序表的实现 1.数据结构
数据结构的概念… 本文由睡觉待开机原创转载请注明出处。 本内容在csdn网站首发 欢迎各位点赞—评论—收藏 如果存在不足之处请评论留言共同进步 这里写目录标题 1.数据结构2.顺序表线性表顺序表的结构 3.动态顺序表的实现 1.数据结构
数据结构的概念数据结构这个词可以拆分为“数据”和“结构”两个词所谓数据就是我们存放在内存中的一系列数字而已结构指的是组织数据的方式整体而言数据结构是计算机存储、组织数据的方式。
其中数组就是一种最基本的数据结构。
2.顺序表
在介绍顺序表之前我们先来了解一下线性表
线性表
线性表的概念 线性表linear list是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中⼴泛使⽤的数据结构常见的线性表顺序表、链表、栈、队列、字符串… 线性表的特点 在逻辑结构上是线性结构也就是一条完整的线。 在物理结构上则是一种不规则不一定连续的数据组织形式。 线性表的分类 线性表包括顺序表、链表、栈、队列、字符串…
顺序表的结构 //静态顺序表
typedef int int_n;
typedef struct seqlist
{int_n arr_n[100];int size_n;int capacity_n;
};//动态顺序表
typedef int int_t;
typedef struct SeqList
{int_t* arr;int size;int capacity;
}SL;
结论由于动态顺序表的灵活优势因而首选动态顺序表。
3.动态顺序表的实现
下面是一些功能函数声明和顺序表原型都放在.h文件中。 顺序表是由一个原型和一系列接口功能组成的因而我们首先升级一个数组为动态顺序表原型
//动态顺序表
typedef int int_t;
typedef struct SeqList
{int_t* arr;int size;int capacity;
}SL;实现各种顺序表的功能接口
//顺序表初始化的功能
void SLinit(SL* psl);
//顺序表尾插
void SLpushback(SL* psl,int_t n);
//顺序表头插
void SLpushfront(SL* psl,int_t n);
//顺序表中间插
void SLinnert(SL* psl, int pos, int_t n);
//顺序表打印
void SLprint(SL* psl);
//顺序表尾删
void SLpopback(SL* psl);
//顺序表头删
void SLpopfront(SL* psl);
//顺序表中间删
void SLpopinnert(SL* psl,int pos);下面是具体实现一些函数功能接口
#includeSeqList.h
void SLinit(SL* psl)
{psl-arr NULL;psl-size psl-capacity 0;
}void SLcheckcapacity(SL* psl)
{if (psl-capacity psl-size){//刚开始是0所以需要进行三目操作符int newcapacity psl-capacity 0 ? 4 : 2 * psl-capacity;int_t * t (int_t*)realloc(psl-arr, newcapacity * sizeof(int_t));if (t NULL){perror(reallloc fail!);exit(1);}psl-arr t;psl-capacity newcapacity;}
}void SLpushback(SL* psl,int_t n)
{assert(psl);//容量不足需要先进行判断容量是否充足在决定是否进行扩容SLcheckcapacity(psl);//容量充足则直接插入psl-arr[psl-size] n;psl-size;
}void SLpushfront(SL* psl, int_t n)
{assert(psl);//扩容问题SLcheckcapacity(psl);//所有数字往后挪动一位int i 0;for (i psl-size; i 0; i--){psl-arr[i] psl-arr[i - 1];}//插入psl-arr[0] n;psl-size;
}void SLinnert(SL* psl, int pos, int_t n)
{assert(psl);assert(pos 0 pos psl-size);//扩容问题SLcheckcapacity(psl);//中间插入中间往后所有的数字往后挪动一位int i 0;for (i psl-size; i pos; i--){psl-arr[i] psl-arr[i - 1];}//插入数据psl-arr[pos] n;psl-size;
}void SLprint(SL* psl)
{int i 0;for (i 0; i psl-size; i){printf(%d , psl-arr[i]);}printf(\n);
}void SLpopback(SL* psl)
{assert(psl-size psl);psl-size--;
}
void SLpopfront(SL* psl)
{assert(psl psl-size);int i 0;for(i 1; ipsl-size; i){psl-arr[i-1] psl-arr[i];}psl-size--;}void SLpopinnert(SL* psl, int pos)
{assert(pos 0 pos psl-size);int i 0;for (i pos; i psl-size-1; i){psl-arr[i] psl-arr[i 1];}psl-size--;
}下面是测试代码源文件内容在test.c文件中
#define _CRT_SECURE_NO_WARNINGS 1
#includeSeqList.hint main()
{SL sl;SLinit(sl);SLpushback(sl,1);SLpushfront(sl, 1);SLpushfront(sl, 1);SLpushfront(sl, 1);SLpushfront(sl, 1);SLprint(sl);SLinnert(sl, 1, 3);SLprint(sl);SLpopback(sl);SLprint(sl);SLpopfront(sl);SLprint(sl);SLpopinnert(sl,3);SLprint(sl);return 0;
}