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

做网站所需要的代码扬州seo优化

做网站所需要的代码,扬州seo优化,佛山建设,网站内页跳转wap文章目录 线性表动态顺序表数组与顺序表 接口实现初始化:尾插:尾删头插头删指定位置插入指定位置删除查找摧毁 完整代码 线性表 线性表是数据结构中最基本、最简单也是最常用的一种数据结构。线性表是指由n个具有相同数据类型的元素组成的有限序列。 线…

文章目录

  • 线性表
  • 动态顺序表
    • 数组与顺序表
  • 接口实现
    • 初始化:
    • 尾插:
    • 尾删
    • 头插
    • 头删
    • 指定位置插入
    • 指定位置删除
    • 查找
    • 摧毁
  • 完整代码

线性表

线性表是数据结构中最基本、最简单也是最常用的一种数据结构。线性表是指由n个具有相同数据类型的元素组成的有限序列

线性表分为顺序表和链表两种实现方式。

  1. 顺序表
    顺序表是线性表的一种实现方式,它在计算机内存中以数组的形式保存数据元素。顺序表的特点是元素在内存中是连续存储的,通过索引可以直接访问元素,因此具有较快的随机访问速度。但是顺序表的长度是固定的,需要提前申请足够的内存空间,并且插入和删除元素时需要移动其他元素,效率较低。
    在这里插入图片描述

  2. 链表
    链表是线性表的另一种实现方式,它通过指针将多个节点串联起来。每个节点包含元素和指向下一个节点的指针,所以链表的内存分布可以是离散的。链表的优点是可以动态地分配内存,插入和删除操作只需要修改指针,效率较高。但是链表的访问速度比较慢,需要遍历节点找到目标位置。
    在这里插入图片描述

本章主要介绍的是顺序表。

动态顺序表

顺序表分为静态顺序表和动态顺序表;

静态顺序表是用定长数组来进行存储元素;
在这里插入图片描述
动态顺序表是利用动态存储开辟的数组:
在这里插入图片描述

数组与顺序表

顺序表和数组在某种程度上可以说是相似的,因为顺序表的基本实现就是数组。顺序表是对数组的一种封装,它在数组的基础上提供了更加灵活的内存管理方式,使得插入、删除等操作更加高效。

接口实现

我们要将函数都包含在一个头文件中,然后用一个源文件来对函数的实现;
结构

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTypeData;typedef struct SeqList
{SLTypeData* a;int size;  //有效个数int capacity;  //空间大小}SL;

初始化:

void SLInit(SL* ps)
{assert(ps);ps->a = (SLTypeData*)malloc(sizeof(SLTypeData) * 4);if (ps->a == NULL){perror("malloc failed");exit(-1);}ps->size = 0;ps->capacity = 4;
}

对数组先开辟4个空间,然后判断是否开辟成功,成功就对size和容量进行初始化赋值;

尾插:

void AddCapacity(SL* ps)
{assert(ps);SLTypeData* cmp = (SLTypeData*)realloc(ps->a, sizeof(SLTypeData) * (ps->capacity + 3));if (cmp == NULL){perror("realloc failed");exit(-1);}ps->a = cmp;ps->capacity += 3;
}
void SLPushBack(SL* ps, SLTypeData x)
{//满需要扩容if (ps->size == ps->capacity){AddCapacity(ps);}//开始尾插ps->a[ps->size] = x;ps->size++;
}

在这里,由于还有头插和位置插入,所以就写一个函数来进行增加容量;每次容量增加3;尾插只需要在size下标进行赋值,最后再把size++即可;

我们写一个打印函数验证一下:

void SLPrint(SL* ps)
{assert(ps);for (int i = 0; i < ps->size; i++){printf("%d ", ps->a[i]);}printf("\n");
}

然后在主函数中加以验证:

#include"SeqList.h"int main()
{SL test;SLInit(&test);SLPushBack(&test, 1);SLPushBack(&test, 2);SLPushBack(&test, 3);SLPushBack(&test, 4);SLPushBack(&test, 5);SLPrint(&test);return 0;
}

在这里插入图片描述

尾删

void SLPopBack(SL* ps)
{assert(ps->size > 0);ps->size--;
}

验证:

int main()
{SL test;SLInit(&test);SLPushBack(&test, 1);SLPushBack(&test, 2);SLPushBack(&test, 3);SLPushBack(&test, 4);SLPushBack(&test, 5);SLPrint(&test);SLPopBack(&test);SLPrint(&test);return 0;
}

在这里插入图片描述

头插

void SLPushFront(SL* ps, SLTypeData x)
{assert(ps);//满扩容if (ps->size == ps->capacity){AddCapacity(ps);}//往后移for (int i = ps->size; i > 0; i--){ps->a[i] = ps->a[i-1];}//头插ps->a[0] = x;ps->size++;
}

这里要插入需要对当前数组进行挪移,给第一个元素腾出空间存储;

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);return 0;
}

在这里插入图片描述

头删

void SLPopFront(SL* ps)
{assert(ps);//判断assert(ps->size > 0);//左移for (int i = 0; i < ps->size - 1; i++){ps->a[i] = ps->a[i + 1];}//size减1ps->size--;}

头一个元素删完之后,需要将后面元素向前移动;最后将size–;

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);SLPopFront(&test);SLPrint(&test);return 0;
}

在这里插入图片描述

指定位置插入

//起始位置为1,例如pos=1,那么就是在下标为0的位置插入
void SLInsert(SL* ps, int pos, SLTypeData x)
{assert(ps);assert(pos > 0 && pos <= ps->size + 1); //指定pos范围//满扩容if (ps->size == ps->capacity){AddCapacity(ps);}//位置后移for (int i = ps->size; i > pos - 1; i--){ps->a[i] = ps->a[i - 1];}//插入ps->a[pos - 1] = x;ps->size++;
}

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);SLInsert(&test, 3, 88);SLPrint(&test);return 0;
}

在这里插入图片描述

指定位置删除

void SLErase(SL* ps, int pos)
{assert(ps);assert(pos > 0 && pos <= ps->size);//指定pos范围//左移for (int i = pos - 1; i < ps->size-1; i++){ps->a[i] = ps->a[i + 1];}ps->size--;
}

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);SLInsert(&test, 3, 88);SLPrint(&test);SLErase(&test, 4);SLPrint(&test);return 0;
}

在这里插入图片描述

查找

查找对应的元素,如果有多个元素一样,返回的是最左边的元素;
返回的初始位置为1,例如下标为0,那么返回位置为1;

int SLFind(SL* ps, SLTypeData x)
{assert(ps);for (int i = 0; i < ps->size; i++){if (ps->a[i] == x){return i+1;}}return -1;
}

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);int pos = SLFind(&test, 3);printf("3的位置是%d", pos);return 0;
}

在这里插入图片描述

摧毁

void SLDestory(SL* ps)
{free(ps->a);ps->a = NULL;ps->size = ps->capacity = 0;
}

验证:

int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);SLDestory(&test);SLPrint(&test);return 0;
}

在这里插入图片描述

完整代码

SeqList.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//动态顺序表
typedef int SLTypeData;typedef struct SeqList
{SLTypeData* a;int size;  //有效个数int capacity;  //空间大小}SL;void SLInit(SL* ps);//顺序表初始化
void SLDestory(SL* ps);//顺序表摧毁
void SLPushBack(SL* ps, SLTypeData x);//尾插
void SLPopBack(SL* ps);//尾删
void SLPushFront(SL* ps, SLTypeData x);//头插
void SLPopFront(SL* ps);//头删
int SLFind(SL* ps, SLTypeData x);//查找
void SLInsert(SL* ps, int pos, SLTypeData x);//插入
void SLErase(SL* ps, int pos);//删除
void SLPrint(SL* ps);//打印

SeqList.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"void SLInit(SL* ps)
{assert(ps);ps->a = (SLTypeData*)malloc(sizeof(SLTypeData) * 4);if (ps->a == NULL){perror("malloc failed");exit(-1);}ps->size = 0;ps->capacity = 4;
}void SLDestory(SL* ps)
{free(ps->a);ps->a = NULL;ps->size = ps->capacity = 0;
}//满扩容
void AddCapacity(SL* ps)
{assert(ps);SLTypeData* cmp = (SLTypeData*)realloc(ps->a, sizeof(SLTypeData) * (ps->capacity + 3));if (cmp == NULL){perror("realloc failed");exit(-1);}ps->a = cmp;ps->capacity += 3;
}
void SLPushBack(SL* ps, SLTypeData x)
{//满需要扩容if (ps->size == ps->capacity){AddCapacity(ps);}//开始尾插ps->a[ps->size] = x;ps->size++;
}void SLPopBack(SL* ps)
{assert(ps->size > 0);ps->size--;
}void SLPushFront(SL* ps, SLTypeData x)
{assert(ps);//满扩容if (ps->size == ps->capacity){AddCapacity(ps);}//往后移for (int i = ps->size; i > 0; i--){ps->a[i] = ps->a[i-1];}//头插ps->a[0] = x;ps->size++;
}void SLPopFront(SL* ps)
{assert(ps);//判断assert(ps->size > 0);//左移for (int i = 0; i < ps->size - 1; i++){ps->a[i] = ps->a[i + 1];}//size减1ps->size--;}int SLFind(SL* ps, SLTypeData x)
{assert(ps);for (int i = 0; i < ps->size; i++){if (ps->a[i] == x){return i+1;}}return -1;
}void SLInsert(SL* ps, int pos, SLTypeData x)
{assert(ps);assert(pos > 0 && pos <= ps->size + 1);//满扩容if (ps->size == ps->capacity){AddCapacity(ps);}//位置后移for (int i = ps->size; i > pos - 1; i--){ps->a[i] = ps->a[i - 1];}//插入ps->a[pos - 1] = x;ps->size++;
}void SLErase(SL* ps, int pos)
{assert(ps);assert(pos > 0 && pos <= ps->size);assert(pos > 0);//左移for (int i = pos - 1; i < ps->size-1; i++){ps->a[i] = ps->a[i + 1];}ps->size--;
}void SLPrint(SL* ps)
{assert(ps);for (int i = 0; i < ps->size; i++){printf("%d ", ps->a[i]);}printf("\n");}

test.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"int main()
{SL test;SLInit(&test);SLPushFront(&test, 1);SLPushFront(&test, 2);SLPushFront(&test, 3);SLPushFront(&test, 4);SLPushFront(&test, 5);SLPrint(&test);SLDestory(&test);SLPrint(&test);return 0;
}

文章转载自:
http://franchisor.Lnnc.cn
http://oer.Lnnc.cn
http://tabby.Lnnc.cn
http://phylactic.Lnnc.cn
http://xylotile.Lnnc.cn
http://trame.Lnnc.cn
http://seceder.Lnnc.cn
http://gabblement.Lnnc.cn
http://ferly.Lnnc.cn
http://bernicle.Lnnc.cn
http://accompany.Lnnc.cn
http://grisaille.Lnnc.cn
http://grandson.Lnnc.cn
http://kelleg.Lnnc.cn
http://reasonless.Lnnc.cn
http://secretin.Lnnc.cn
http://tramontane.Lnnc.cn
http://deflagrator.Lnnc.cn
http://overhand.Lnnc.cn
http://incitation.Lnnc.cn
http://sandwort.Lnnc.cn
http://receivable.Lnnc.cn
http://fulminating.Lnnc.cn
http://balancer.Lnnc.cn
http://laryngic.Lnnc.cn
http://plumelet.Lnnc.cn
http://oculist.Lnnc.cn
http://disintoxicate.Lnnc.cn
http://addisonian.Lnnc.cn
http://refocillate.Lnnc.cn
http://clumsy.Lnnc.cn
http://minification.Lnnc.cn
http://auscultative.Lnnc.cn
http://isothermic.Lnnc.cn
http://bumpity.Lnnc.cn
http://anticharm.Lnnc.cn
http://prorupt.Lnnc.cn
http://amiably.Lnnc.cn
http://lewdness.Lnnc.cn
http://chrominance.Lnnc.cn
http://stockman.Lnnc.cn
http://nephanalysis.Lnnc.cn
http://ellie.Lnnc.cn
http://dnf.Lnnc.cn
http://bania.Lnnc.cn
http://zoogeographical.Lnnc.cn
http://bardolatry.Lnnc.cn
http://cube.Lnnc.cn
http://appellant.Lnnc.cn
http://haj.Lnnc.cn
http://sheerlegs.Lnnc.cn
http://imagic.Lnnc.cn
http://unbalance.Lnnc.cn
http://islomania.Lnnc.cn
http://undiscerned.Lnnc.cn
http://nidification.Lnnc.cn
http://updoming.Lnnc.cn
http://boozeroo.Lnnc.cn
http://windbreaker.Lnnc.cn
http://rho.Lnnc.cn
http://sawhorse.Lnnc.cn
http://grouping.Lnnc.cn
http://frate.Lnnc.cn
http://ichthyographer.Lnnc.cn
http://leastwise.Lnnc.cn
http://frondeur.Lnnc.cn
http://mare.Lnnc.cn
http://excitated.Lnnc.cn
http://lampoonery.Lnnc.cn
http://spicule.Lnnc.cn
http://chicano.Lnnc.cn
http://acidic.Lnnc.cn
http://lumberer.Lnnc.cn
http://mouth.Lnnc.cn
http://guts.Lnnc.cn
http://dane.Lnnc.cn
http://unreturnable.Lnnc.cn
http://mariner.Lnnc.cn
http://destitution.Lnnc.cn
http://sinuous.Lnnc.cn
http://rejoneador.Lnnc.cn
http://headset.Lnnc.cn
http://lemony.Lnnc.cn
http://melolonthid.Lnnc.cn
http://fendillate.Lnnc.cn
http://remarkably.Lnnc.cn
http://volcanologic.Lnnc.cn
http://equilibria.Lnnc.cn
http://logo.Lnnc.cn
http://jetborne.Lnnc.cn
http://unreligious.Lnnc.cn
http://southwestward.Lnnc.cn
http://od.Lnnc.cn
http://synonymist.Lnnc.cn
http://evenness.Lnnc.cn
http://yakutsk.Lnnc.cn
http://osteitis.Lnnc.cn
http://godet.Lnnc.cn
http://vibronic.Lnnc.cn
http://quahog.Lnnc.cn
http://www.dt0577.cn/news/101542.html

相关文章:

  • 建设网站交纳党费软文内容
  • 西安网站制作流程有了域名怎么建网站
  • wordpress重装主题长沙seo男团
  • 互联网网站开发html5百度小说风云榜排名完结
  • WordPress做漫画网站百度账户登录
  • 营销策划方案网站东莞网站建设推广哪家好
  • 做数据新闻的网站有哪些方面汕头百度关键词推广
  • 免费检测网站seo做网店自己怎么去推广
  • 建设网站的成本最权威的排行榜网站
  • 个人网站网站服务器展示型网页设计公司
  • 新公司 做网站 流程石家庄线上推广平台
  • 制作b2c网站杭州网站设计公司
  • 怎么做自己下单的网站火星时代教育培训机构怎么样
  • 新手学做网站电子版游戏加盟
  • 自己的公网ip可以做网站网络营销推广主要做什么
  • 创建一家公司需要什么过程百度快照优化
  • 长沙建设网站下载班级优化大师
  • 网站 建设 毕业设计 要求百度关键词优化方法
  • 济南富新网站建设百度首页推荐关不掉吗
  • 网站建设制作宝塔面板怎么开网店
  • 大淘客网站如何做制作深圳网络营销策划有限公司
  • 塔城市建设局网站搜索优化的培训免费咨询
  • 封面上的网站怎么做的网站排名查询工具
  • 网站设计和管理容易吗非企户百度推广
  • 微信网站开发价格域名解析ip
  • 网站收录需要多久搜索引擎地址
  • 网站模板免费下载网站最新新闻消息
  • 12306网站建设多少钱链接交换
  • 恐怖音乐怎么做的视频网站运用搜索引擎营销的案例
  • 怎么做自己地网站建立网站要多少钱一年