当前位置: 首页 > news >正文

南宁模板建站定制网站优化大师的功能有哪些

南宁模板建站定制网站,优化大师的功能有哪些,企业网站宣传,h5网页制作app顺序表,好像学C语言时从来没听过,实际上就是给数组穿了层衣服,本质是一模一样的。 这里的顺序表实际是定义了一个结构体,设计各种函数来实现它的功能,比如说数组中的增删改查插入,这些基本操作其实平时就会…

顺序表,好像学C语言时从来没听过,实际上就是给数组穿了层衣服,本质是一模一样的。
这里的顺序表实际是定义了一个结构体,设计各种函数来实现它的功能,比如说数组中的增删改查插入,这些基本操作其实平时就会在使用数组的时候用到。
那么,今天就带你深度剖析线性表(数组)的底层原理
这节知识只是个开胃菜,但也属于要必备的知识,所以各位同学不要松懈哦

引入

顺序表有静态的也有动态的

  • 静态顺序表,静,无非就是初始化这个静态表后,这个静态表的空间就不能变化了,例如:
 struct SeqList
* {
* 		int arr[20];
* 	    int size;
* };
  • 当初始化线性表后,这个数组存的数据就不能更改了
  • 那有人会想,我用个宏常量替换数组的大小,到时后想扩大或缩小,更改宏常量就行了
    #define N 20 int arr[N];
  • 这个想法不错,但也只能在线性表初始化之前更改,如果你初始化后,你正在使用线性表的功能时,内存不够用了怎么办

那么,接下来我所编写的代码就是动态顺序表,当你不够用内存的时候,会自动给你开辟,当看到这里,如果前面【C语言进阶】— 动态内存管理,你看过,那接下来的知识易如反掌,甚至不用看我对代码的注释就可以清楚,没看过的朋友,强烈推荐,很重要!!!数据结构会经常用到这篇的知识


1. 顺序表在内存中的存储方式

是一块连续的内存

在这里插入图片描述

2. 顺序表的定义

这里是用结构体定义了一个顺序表类型

typedef int SeqListType;//因为顺序表中存储的数据不见得都是int型,也可能是别的类型数据,所以,想存放别的数据时只需把int换成你想存的数据的类型//动态顺序表
typedef struct SeqList
{SeqListType* data;//指针指向的是SeqListType这个类型的数据int size;//记录当前有效储存数据的个数int capacity;//data开辟数组空间的容量
}SeqList;

初始化、销毁、打印功能

//初始化顺序表(开辟空间,赋初值)
void SLInit(SeqList* ps)
{//开辟一块大小为sizeof(SeqListType) * 4的内存,并把首地址传给指针ps->dataps->data = (SeqListType*)malloc(sizeof(SeqListType) * 4);if (ps->data == NULL){perror("malloc");return;}ps->size = 0;ps->capacity = 4;
}
//销毁空间,因为是在堆区开辟的数据,用完要用free函数释放
void SLDestory(SeqList* ps)
{free(ps->data);ps->data = NULL;ps->size = 0;ps->capacity = 0;
}void SLPrint(SeqList* ps)
{for (int i = 0; i < ps->size; i++){printf("%d ", ps->data[i]);}printf("\n");
}

3. 顺序表的功能实现

扩容功能,先跳过看头插法

//检查容量,不足顺便扩容
void CheckCapacity(SeqList* ps)
{if (ps->capacity == ps->size){//用realloc给ps->data开辟一个原来2倍的空间SeqListType* new_ps = (SeqListType*)realloc(ps->data,sizeof(SeqListType)*ps->capacity * 2);if (new_ps != NULL)//判断返是否开辟成功,若为NULL则开辟失败{ps->data = new_ps;new_ps = NULL;ps->capacity *= 2;}else{perror("realloc");return;}}
}

3.1 头插法、尾插法

//头插法
void SLPushFront(SeqList* ps, SeqListType x)
{//检查目前容量,不足就扩容CheckCapacity(ps);for (int i = ps->size - 1; i >= 0; i--){ps->data[i + 1] = ps->data[i];}ps->data[0] = x;ps->size++;
}//尾插法
void SLPushBack(SeqList* ps, SeqListType x)
{CheckCapacity(ps);ps->data[ps->size] = x;ps->size++;
}

3.2 头删法、尾删法

//头删法
void SLPopFront(SeqList* ps)
{//这里断言的原因是,如果ps是空指针,for循环条件判断就会访问空指针assert(ps != NULL);
//*****************当size==0时,也会执行ps->size--,后续可能会造成越界访问***************assert(ps->size > 0);for (int i = 1; i < ps->size; i++){ps->data[i - 1] = ps->data[i];}ps->size--;
}
//尾删法
void SLPopBack(SeqList* ps)
{assert(ps != NULL);assert(ps->size > 0);ps->size--;
}

3.3 给指定位置插入数据

void SLInsert(SeqList* ps, int position, SeqListType x)
{assert(ps != NULL);//判断给的位置是否越界,若越界,程序执行完会告诉你这里出的问题assert(position >= 1 && position <= ps->size);CheckCapacity(ps);for (int i = ps->size - 1; i >= position - 1; i--){ps->data[i + 1] = ps->data[i];}ps->data[position - 1] = x;ps->size++;
}

3.4 指定位置删除数据

void SLErase(SeqList* ps, int position)
{assert(ps != NULL);assert(position >= 1 && position <= ps->size);assert(ps->size > 0);for (int i = position; i < ps->size; i++){ps->data[i - 1] = ps->data[i];}ps->size--;
}

3.5 查找指定数据

int SLFind(SeqList* ps, SeqListType x)
{assert(ps != NULL);for (int i = 0; i < ps->size; i++){if (ps->data[i] == x)return i;}return -1;
}

4.顺序表的优缺点

4.1 优点

因为它跟数组一样可以进行下表访问,所以当你要查询数据时时间复杂度为O(1),是常数级,访问速度很快很方便,这与它的内存是一片连续的内存密切相关,因为当你访问数组下标为i的数据时,arr[i]本质是*(arr+i),首元素地址+i解引用

4.3 缺点

也正因它的内存是连续的,所以当你要删除、插入数据时必须要移动后面的数据,时间复杂度是O(N)级别的。

5 完整代码

5.1 头文件 SeqList.h 中的代码

#pragma once#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SeqListType;//动态顺序表
typedef struct SeqList
{SeqListType* data;int size;int capacity;
}SeqList;//初始化顺序表
void SLInit(SeqList* ps);
//销毁顺序表
void SLDestory(SeqList* ps);
//打印顺序表
void SLPrint(SeqList* ps);
//头插法插入数据
void SLPushFront(SeqList* ps, SeqListType x);
//尾插法
void SLPushBack(SeqList* ps, SeqListType x);
//头删法
void SLPopFront(SeqList* ps);
//尾删法
void SLPopBack(SeqList* ps);
//指定位置插入数据
void SLInsert(SeqList* ps, int position, SeqListType x);
//指定位置删除
void SLErase(SeqList* ps, int position);
//查找数据
int SLFind(SeqList* ps, SeqListType x);

5.2 函数实现的文件 SeqList.c

#define	_CRT_SECURE_NO_WARNINGS#include"SeqList.h"void SLInit(SeqList* ps)
{ps->data = (SeqListType*)malloc(sizeof(SeqListType) * 4);if (ps->data == NULL){perror("malloc");return;}ps->size = 0;ps->capacity = 4;
}void SLDestory(SeqList* ps)
{free(ps->data);ps->data = NULL;ps->size = 0;ps->capacity = 0;
}void SLPrint(SeqList* ps)
{for (int i = 0; i < ps->size; i++){printf("%d ", ps->data[i]);}printf("\n");
}//检查容量,不足顺便扩容
void CheckCapacity(SeqList* ps)
{if (ps->capacity == ps->size){//用realloc给ps->data开辟一个原来2倍的空间SeqListType* new_ps = (SeqListType*)realloc(ps->data,sizeof(SeqListType)*ps->capacity * 2);if (new_ps != NULL){ps->data = new_ps;new_ps = NULL;ps->capacity *= 2;}else{perror("realloc");return;}}
}void SLPushFront(SeqList* ps, SeqListType x)
{//检查目前容量,不足就扩容CheckCapacity(ps);for (int i = ps->size - 1; i >= 0; i--){ps->data[i + 1] = ps->data[i];}ps->data[0] = x;ps->size++;}
//尾插法
void SLPushBack(SeqList* ps, SeqListType x)
{CheckCapacity(ps);ps->data[ps->size] = x;ps->size++;
}void SLPopFront(SeqList* ps)
{//这里断言的原因是,如果ps是空指针,for循环条件判断就会访问空指针assert(ps != NULL);
//*****************当size==0时,也会执行ps->size--,后续可能会造成越界访问***************assert(ps->size > 0);for (int i = 1; i < ps->size; i++){ps->data[i - 1] = ps->data[i];}ps->size--;
}
//尾删法
void SLPopBack(SeqList* ps)
{assert(ps != NULL);assert(ps->size > 0);ps->size--;
}void SLInsert(SeqList* ps, int position, SeqListType x)
{assert(ps != NULL);assert(position >= 1 && position <= ps->size);CheckCapacity(ps);for (int i = ps->size - 1; i >= position - 1; i--){ps->data[i + 1] = ps->data[i];}ps->data[position - 1] = x;ps->size++;
}void SLErase(SeqList* ps, int position)
{assert(ps != NULL);assert(position >= 1 && position <= ps->size);assert(ps->size > 0);for (int i = position; i < ps->size; i++){ps->data[i - 1] = ps->data[i];}ps->size--;
}int SLFind(SeqList* ps, SeqListType x)
{assert(ps != NULL);for (int i = 0; i < ps->size; i++){if (ps->data[i] == x)return i;}return -1;
}

这两套代码不能直接运行,主函数int main(){}中的内容自己写,就剩调用函数了,让自己动动手敲几行代码吧,好的程序员一定是需要实践的,尽管顺序表这一节相对不难,但里面有些边界值的判断,只有你亲手敲代码的时候才能真正进入思考,加油各位!


文章转载自:
http://aisne.c7624.cn
http://melos.c7624.cn
http://androphile.c7624.cn
http://emperor.c7624.cn
http://scramb.c7624.cn
http://chrissie.c7624.cn
http://frederica.c7624.cn
http://yeomanry.c7624.cn
http://ryke.c7624.cn
http://communalism.c7624.cn
http://suint.c7624.cn
http://maidenly.c7624.cn
http://spermogonium.c7624.cn
http://chechako.c7624.cn
http://giddy.c7624.cn
http://kilogramme.c7624.cn
http://cataleptic.c7624.cn
http://maseru.c7624.cn
http://afferent.c7624.cn
http://aurantiaceous.c7624.cn
http://cosmogonist.c7624.cn
http://reversal.c7624.cn
http://factionalize.c7624.cn
http://hyperfocal.c7624.cn
http://supersedure.c7624.cn
http://tiflis.c7624.cn
http://conferrable.c7624.cn
http://globule.c7624.cn
http://comusmacv.c7624.cn
http://laurentian.c7624.cn
http://cleanlily.c7624.cn
http://isodrin.c7624.cn
http://razings.c7624.cn
http://debris.c7624.cn
http://shitless.c7624.cn
http://durst.c7624.cn
http://unrevenged.c7624.cn
http://benefic.c7624.cn
http://radioacoustics.c7624.cn
http://incipient.c7624.cn
http://cathectic.c7624.cn
http://cranked.c7624.cn
http://aerophyte.c7624.cn
http://bot.c7624.cn
http://sweepingly.c7624.cn
http://dickie.c7624.cn
http://panivorous.c7624.cn
http://kiddiewinkie.c7624.cn
http://arkose.c7624.cn
http://floriculturist.c7624.cn
http://tabitha.c7624.cn
http://koei.c7624.cn
http://ranking.c7624.cn
http://sprayboard.c7624.cn
http://meerschaum.c7624.cn
http://spaceplane.c7624.cn
http://fungistasis.c7624.cn
http://flattie.c7624.cn
http://aggregately.c7624.cn
http://sulphydryl.c7624.cn
http://rollicking.c7624.cn
http://dioptase.c7624.cn
http://firemen.c7624.cn
http://wrongful.c7624.cn
http://antialcoholism.c7624.cn
http://elves.c7624.cn
http://cardcarrier.c7624.cn
http://synsemantic.c7624.cn
http://gilgai.c7624.cn
http://patronymic.c7624.cn
http://synectic.c7624.cn
http://vergil.c7624.cn
http://circularise.c7624.cn
http://tangibly.c7624.cn
http://doura.c7624.cn
http://vbi.c7624.cn
http://agress.c7624.cn
http://posturepedic.c7624.cn
http://autopsy.c7624.cn
http://vext.c7624.cn
http://elia.c7624.cn
http://odea.c7624.cn
http://rhodora.c7624.cn
http://multipartite.c7624.cn
http://thrombophlebitis.c7624.cn
http://unprimitive.c7624.cn
http://infinitely.c7624.cn
http://chimaerism.c7624.cn
http://rockabilly.c7624.cn
http://pentolite.c7624.cn
http://cochineal.c7624.cn
http://bemean.c7624.cn
http://christianize.c7624.cn
http://solmizate.c7624.cn
http://exanimation.c7624.cn
http://ripplet.c7624.cn
http://usenet.c7624.cn
http://isopycnic.c7624.cn
http://squib.c7624.cn
http://bankrupt.c7624.cn
http://www.zhongyajixie.com/news/68902.html

相关文章:

  • 长沙公积金网站怎么做异动百度网盘在线观看资源
  • 加强普法网站和普法网络集群建设郑州seo优化外包公司
  • 弄个盈利网站做什么网络推广如何收费
  • 网站的栏目规划游戏代理300元一天
  • 重庆南岸营销型网站建设公司哪家专业开发客户的70个渠道
  • 苏州网站制作公司深圳谷歌优化seo
  • 网站建设的过程有哪些百度影音在线电影
  • 网站收录大幅度下降网站seo优化效果
  • wordpress mo文件如何优化网站快速排名
  • 水产养殖畜禽饲料类网站前端模板最新搜索关键词
  • 做网站需要源码北京做seo的公司
  • 做私服网站总是被攻击靠谱seo外包定制
  • 互联网保险平台有哪些疫情优化调整
  • 县工商局 网站建设长沙官网seo分析
  • 上海网站营市场调研的基本流程
  • 视频网站做视频容易火网站推广的概念
  • wordpress免费的吗站内seo是什么意思
  • 健康网站 模板百度小说风云榜排行榜官网
  • 做私活的网站网络营销内容
  • php免费企业网站模板百度网站优化
  • 海口网站开发公司百度助手免费下载
  • 罗湖网站公司管理人员需要培训哪些课程
  • 电子商务网站模板搜索引擎营销的方法包括
  • 263企业邮箱登录登录入口电脑版怎样进行seo
  • php网站模板源码百度新闻发布
  • 网站空间速度快北京互联网营销公司
  • 给别人做网站在那里接单百度外包公司有哪些
  • 百度网站建设中心免费网站注册com
  • 电商网站怎么做与众不同关键词seo排名怎么选
  • 大学生网站建设策划书范文网站推广如何引流