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

中国建设电工网站今天

中国建设电工网站,今天,重庆建设厅的网站首页,盐城大丰建设局网站引言 详细介绍了顺序表中各个接口的实现,一定要亲自动手敲一遍,要能想象出具体的图像 第一次敲可能不能完全靠自己敲出来(很正常),过一段时间可以根据顺序表的原理敲第二遍 孰能生巧 一、线性表 在介绍顺序表之前先…

引言

        详细介绍了顺序表中各个接口的实现,一定要亲自动手敲一遍,要能想象出具体的图像

第一次敲可能不能完全靠自己敲出来(很正常),过一段时间可以根据顺序表的原理敲第二遍

孰能生巧

一、线性表

在介绍顺序表之前先认识一下线性表,顺序表是线性表的一种

        线性表(linearlist)是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...

线性表在逻辑上是线性结构,也就说是连续的⼀条直线。

但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

二、顺序表 

概念:顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,⼀般情况下采用数组存储。(在逻辑结构上肯定是连续的)

顺序表和数组的区别?

顺序表的底层结构是数组,对数组的封装,实现了常用的增删改查等接口。

三、实现动态顺序表 

💡代码小提示

        编写代码过程中要勤测试,避免写出大量代码后再测试而导致出现问题,问题定位无从下手。 

        定义三个文件:

text.c       //测试代码是否正确
SeqList.c //Sequece List(顺序表)所有代码
SeqList.h //顺序表里所有的头文件 和 声明函数

text.c    //测试代码是否正确
SeqList.c //Sequece List(顺序表)所有代码
SeqList.h //顺序表里所有的头文件 和 声明函数

1.定义顺序表类型,引用需要的头文件 

 在SeqList.h文件中:

#pragma once   //确保头文件在一个编译单元中只被包含一次
#include<stdio.h>   //输入输出需要,perror函数需要
#include<stdlib.h>  //开辟动态内存需要
#include<assert.h>  //assert函数需要//定义动态顺序表的结构
typedef int SLDataType;
typedef struct SeqList
{SLDataType* arr;int size;        //有效数据个数//size位置是空的,待放入值int capacity;    //空间容量
}SL;   // 顺序表

2.初始化顺序表 

在SeqList.c文件中的代码:

#include"SeqList.h"  //引用头文件
void SLInit(SL* ps) //初始化
{ps->arr = NULL;    ps->size = ps->capacity = 0;
}

3.销毁顺序表 

在SeqList.c中的代码:


void SLDestroy(SL* ps)//销毁顺序表
{assert(ps);if (ps->arr)free(ps->arr);ps->arr = NULL;ps->arr = ps->capacity = 0;
}

4.对顺序表扩容 

在SeqList.c文件中的代码:

void SLCheckCapacity(SL* ps) //增容
{if (ps->size == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;//如果capacity 是 0,就先扩容4个空间//如果capacity 不是0,就扩容2倍空间SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//先将扩容后的空间存在tmp中if (tmp == NULL){perror("realloc"); //输出错误信息exit(1); //退出程序并返回1;}ps->arr = tmp; //tmp不是空,传给arrps->capacity = newCapacity; //更新空间}
}

这里采用的是2倍扩容,有效利用空间。

注意:realloc函数第二个参数就是扩容后空间的大小,而不是扩容第二个参数那么大。

5. 实现尾插和打印

在SeqList.c文件中的代码:

#include"SeqList.h"
void SLpushBack(SL* ps, SLDataType x)//尾插
{assert(ps);SLCheckCapacity(ps);  //空间不够的话就扩容ps->arr[ps->size++] = x;
}void SLprint(SL* ps)  //打印顺序表
{assert(ps);for (int i = 0; i < ps->size; i++){printf("%d ", ps->arr[i]);}printf("\n");
}

6.实现头插 

在SeqList.c文件中的代码:

#include"SeqList.h"
void SLpushFront(SL* ps, SLDataType x) //头插
{assert(ps);SLCheckCapacity(ps);for (int i = ps->size;i > 0; i--) //将顺序表中所有数据向后挪动一位{ps->arr[i] = ps->arr[i - 1];}ps->arr[0] = x;++ps->size;
}

7.实现在指定位置插入数据 

在SeqList.c文件中的代码:

#include"SeqList.h"
void SLInsert(SL* ps, int pos, SLDataType x)//在指定位置插入数据
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);//pos及之后的数据整体向后挪动一位for (int i = ps->size; i > pos; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;++ps->size;
}

8.测试头插,尾插,任意位置插入元素 

在text.c文件中的代码:

#include"SeqList.h"void text01() //测试尾插
{SL s1;SLInit(&s1);SLpushBack(&s1,1);SLpushBack(&s1,2);SLpushBack(&s1,3);SLprint(&s1);SLDestroy(&s1);
}
void text02() //测试头插
{SL s2;SLInit(&s2);SLpushFront(&s2, 1);SLpushFront(&s2, 2);SLpushFront(&s2, 3);SLprint(&s2);SLDestroy(&s2);
}void text03() //测试在指定位置插入元素
{SL s3;SLInit(&s3);SLpushFront(&s3, 1);SLpushFront(&s3, 2);SLpushFront(&s3, 3);SLInsert(&s3, 2, 10);SLprint(&s3);SLDestroy(&s3);
}
int main()
{//text01(); //测试尾插//text02(); //测试头插text03(); //测试在指定位置插入元素return 0;
}

注意:一定不用忘记对顺序表的初始化 

text03运行的结果:

9.实现尾删,头删

在SeqList.c中的代码:

void SLPopBack(SL* ps) //尾删,直接让size减1即可
{//ps:限制参数不能直接给NULL//ps->size:顺序表为空assert(ps && ps->size);--ps->size;
}void SLPopFront(SL* ps) //头删,从第二位到最后一位整体向前挪动一位即可,最后size--
{assert(ps && ps->size);for (int i = 0; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1];}--ps->size;
}

10. 实现任意位置删除元素

在SeqList.c中的代码:

void SLErase(SL* ps, int pos) //删除pos位置的元素
{assert(ps);assert(pos >= 0 && pos <=ps->size);for (int i = pos; i < ps->size - 1; i++) //pos及其位置之后的元素整体向前挪动一位{ps->arr[i] = ps->arr[i + 1];}--ps->size;
}

11.测试头删,尾删,删除任意位置的元素

 在text.c中的代码:

#include"SeqList.h"
void text04()
{SL s4;SLInit(&s4);SLpushFront(&s4, 1);SLpushFront(&s4, 2);SLpushFront(&s4, 3);SLInsert(&s4, 2, 10);SLPopBack(&s4);SLPopFront(&s4);SLprint(&s4);SLDestroy(&s4);
}void text05() //测试删除任意位置的元素
{SL s5;SLInit(&s5);SLpushFront(&s5, 1);SLpushFront(&s5, 2);SLpushFront(&s5, 3);SLInsert(&s5, 2, 10);SLErase(&s5, 2);SLprint(&s5);SLDestroy(&s5);
}int main()
{//text01(); //测试尾插//text02(); //测试头插text03(); //测试在指定位置插入元素//text04();//测试尾删和头删text05(); //测试删除任意位置的元素return 0;
}

text03和text05运行的结果: 

12.查找元素所在的位置,找不到返回-1 

 在SeqList.c中的代码:

#include"SeqList.h"
int SLFind(SL* ps, int x) //查找元素x的位置
{assert(ps);for (int i = 0; i <= ps->size - 1; i++){if (ps->arr[i] == x)return i;}return -1;
}

 13.测试查找元素所在的位置

在text.c中的代码:

#include"SeqList.h"
int text06()
{SL s6;SLInit(&s6);SLpushFront(&s6, 1);SLpushFront(&s6, 2);SLpushFront(&s6, 3);SLInsert(&s6, 2, 10);int r1 = SLFind(&s6, 2);int r2 = SLFind(&s6, 9);SLprint(&s6);printf("2元素的位置:%d\n", r1);printf("9元素的位置:%d\n", r2);SLDestroy(&s6);
}int main()
{//text01(); //测试尾插//text02(); //测试头插text03(); //测试在指定位置插入元素//text04();//测试尾删和头删//text05(); //测试删除任意位置的元素text06(); //测试查找元素的位置return 0;
}

运行结果:(下标是从0开始的)

四、顺序表所有代码

在SeqList.h中的代码(核心):

#pragma once   //确保头文件在一个编译单元中只被包含一次
#include<stdio.h>   //输入输出需要,perror函数需要
#include<stdlib.h>  //开辟动态内存需要
#include<assert.h>  //assert函数需要//定义动态顺序表的结构
typedef int SLDataType;
typedef struct SeqList
{SLDataType* arr;int size;        //有效数据个数int capacity;    //空间容量
}SL;              // 顺序表void SLprint(SL* ps);  //打印顺序表void SLInit(SL* ps);//初始化void SLDestroy(SL* ps);//销毁顺序表void SLCheckCapacity(SL* ps); //增容void SLpushBack(SL* ps, SLDataType x);//尾插void SLpushFront(SL* ps, SLDataType x); //头插void SLInsert(SL* ps, int pos, SLDataType x);//在指定位置插入数据void SLPopBack(SL* ps); //尾删void SLPopFront(SL* ps); //头删void SLErase(SL* ps, int x); //删除pos位置的数据int SLFind(SL* ps, int x); //查找元素x的位置

在SeqList.c中的文件(核心): 

#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* ps) //初始化
{ps->arr = NULL;ps->size = ps->capacity = 0;
}void SLDestroy(SL* ps)//销毁顺序表
{assert(ps);if (ps->arr)free(ps->arr);ps->arr = NULL;ps->arr = ps->capacity = 0;
}void SLprint(SL* ps)  //打印顺序表
{assert(ps);for (int i = 0; i < ps->size; i++){printf("%d ", ps->arr[i]);}printf("\n");
}void SLCheckCapacity(SL* ps) //增容
{if (ps->size == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));if (tmp == NULL){perror("realloc");exit(1); //退出程序并返回1;}ps->arr = tmp;ps->capacity = newCapacity;}
}void SLpushBack(SL* ps, SLDataType x)//尾插
{assert(ps);SLCheckCapacity(ps);ps->arr[ps->size++] = x;
}
void SLpushFront(SL* ps, SLDataType x) //头插
{assert(ps);SLCheckCapacity(ps);for (int i = ps->size;i > 0; i--) //将顺序表中所有数据向后挪动一位{ps->arr[i] = ps->arr[i - 1];}ps->arr[0] = x;++ps->size;
}void SLInsert(SL* ps, int pos, SLDataType x)//在指定位置插入数据
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(ps);//pos及之后的数据整体向后挪动一位for (int i = ps->size; i > pos; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[pos] = x;++ps->size;
}void SLPopBack(SL* ps) //尾删,直接让size减1即可
{//ps:限制参数不能直接给NULL//ps->size:顺序表为空assert(ps && ps->size);--ps->size;
}void SLPopFront(SL* ps) //头删,从第二位到最后一位整体向前挪动一位即可,最后size--
{assert(ps && ps->size);for (int i = 0; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1];}--ps->size;
}void SLErase(SL* ps, int pos) //删除pos位置的元素
{assert(ps);assert(pos >= 0 && pos <=ps->size);for (int i = pos; i < ps->size - 1; i++){ps->arr[i] = ps->arr[i + 1];}--ps->size;
}int SLFind(SL* ps, int x) //查找元素x的位置
{assert(ps);for (int i = 0; i <= ps->size - 1; i++){if (ps->arr[i] == x)return i;}return -1;
}

在text.c中的代码(测试写的函数是否能实现功能):


#include"SeqList.h"void text01()
{SL s1;SLInit(&s1);SLpushBack(&s1,1);SLpushBack(&s1,2);SLpushBack(&s1,3);SLprint(&s1);SLDestroy(&s1);
}
void text02()
{SL s2;SLInit(&s2);SLpushFront(&s2, 1);SLpushFront(&s2, 2);SLpushFront(&s2, 3);SLprint(&s2);SLDestroy(&s2);
}void text03()
{SL s3;SLInit(&s3);SLpushFront(&s3, 1);SLpushFront(&s3, 2);SLpushFront(&s3, 3);SLInsert(&s3, 2, 10);SLprint(&s3);SLDestroy(&s3);
}void text04()
{SL s4;SLInit(&s4);SLpushFront(&s4, 1);SLpushFront(&s4, 2);SLpushFront(&s4, 3);SLInsert(&s4, 2, 10);SLPopBack(&s4);SLPopFront(&s4);SLprint(&s4);SLDestroy(&s4);
}void text05() //测试删除任意位置的元素
{SL s5;SLInit(&s5);SLpushFront(&s5, 1);SLpushFront(&s5, 2);SLpushFront(&s5, 3);SLInsert(&s5, 2, 10);SLErase(&s5, 2);SLprint(&s5);SLDestroy(&s5);
}int text06()
{SL s6;SLInit(&s6);SLpushFront(&s6, 1);SLpushFront(&s6, 2);SLpushFront(&s6, 3);SLInsert(&s6, 2, 10);int r1 = SLFind(&s6, 2);int r2 = SLFind(&s6, 9);SLprint(&s6);printf("2元素的位置:%d\n", r1);printf("9元素的位置:%d\n", r2);SLDestroy(&s6);
}int main()
{//text01(); //测试尾插//text02(); //测试头插text03(); //测试在指定位置插入元素//text04();//测试尾删和头删//text05(); //测试删除任意位置的元素text06(); //测试查找元素的位置return 0;
}

文章转载自:

http://nXuV6QRR.wmmtL.cn
http://bsrY2Umx.wmmtL.cn
http://nXgs1MIh.wmmtL.cn
http://nacCO2FS.wmmtL.cn
http://yzmgBd7O.wmmtL.cn
http://XVDH5aHu.wmmtL.cn
http://L0BtcLvJ.wmmtL.cn
http://rudwhmSY.wmmtL.cn
http://vLwavfTL.wmmtL.cn
http://L9M7xnvj.wmmtL.cn
http://pJSNVmMM.wmmtL.cn
http://gDmf0keH.wmmtL.cn
http://PNCyuIzi.wmmtL.cn
http://MWD4WIkF.wmmtL.cn
http://DkYvgl3X.wmmtL.cn
http://m35IpUPH.wmmtL.cn
http://Tyc0szbk.wmmtL.cn
http://def0IiCt.wmmtL.cn
http://PAfPh0kx.wmmtL.cn
http://6fG1M7ju.wmmtL.cn
http://oKCZOvIn.wmmtL.cn
http://iOl5NKTS.wmmtL.cn
http://LfVd4SoL.wmmtL.cn
http://nF3dRO6d.wmmtL.cn
http://m0lXrleQ.wmmtL.cn
http://3tLQOmR7.wmmtL.cn
http://LL2rKdCB.wmmtL.cn
http://AWjiIAh9.wmmtL.cn
http://ffiB5Cca.wmmtL.cn
http://lkJ6lW4L.wmmtL.cn
http://www.dtcms.com/wzjs/644336.html

相关文章:

  • 便宜网站开发培训制作图片下载什么软件
  • 自己搭建网站需要多少钱百度h5官网登录
  • wordpress 导出优化大师安卓版
  • 山东省住房和建设厅网站网站建设专家价格
  • 惠州做企业网站的网站开发工程师学什么区别
  • 用ip访问没有备案的网站更新网站 seo
  • 无锡商业网站建设网站空间管理平台
  • 网站建设 云计算韩雪冬网站设计
  • 佛山做网站公司有哪些网站官网怎么做
  • 网站充值 下模板全国黄页大全
  • 河源公司做网站免费查公司查老板
  • 辽宁平台网站建设公司平度网站建设公司电话
  • 网站开发哪里好未央免费做网站
  • 建设商城网站的合肥网络seo
  • 网站建设与制作 试卷与答案开发公司施工管理事业部领导如何同下属协调沟通
  • 北京网站建设seo公司哪家好wordpress禁止适应屏幕
  • 南平网站设计自己怎样做淘客网站
  • 网站建设时如何建立客户信赖感注册一个公司的流程
  • 都匀住房和城乡建设部网站体育视频网站建设
  • 容县网站开发美食网站php源码
  • 常用素材网站吉林省长春网站建设
  • 做logo找灵感的网站天宫院网站建设
  • 常州网站建设运营天津公司网站如何制作
  • 建筑网站设计大全宣传片公司哪家好
  • 美发营销型网站商服网站模板
  • c 开发手机网站开发一般做网站带宽选择多大的
  • 网站建设wix网络营销是什么?
  • 好看的网站首页欣赏c 手机网站开发模板
  • 政协网站信息化建设的作用济南家居行业网站开发
  • 网站制作出租黄页引流推广链接