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

数据结构与算法系列之顺序表的实现

在这里插入图片描述

这里写目录标题

  • 顺序表的优缺点:
  • 注意事项
  • test.c(动态顺序表)
  • SeqList.h
  • SeqList.c
  • 各接口函数功能详解
    • void SLInit(SL* ps);//定义
    • void SLDestory(SL* ps);
    • void SLPrint(SL* ps);
    • void SLPushBack(SL* ps ,SLDataType * x );
    • void SLPopBack(SL* ps);
    • void SLPushFront(SL* ps,SLDataType * x );
    • void SLPopFront(SL* ps);
    • void SLCheckCapacity(SL* ps);
    • void SLInsert(SL* ps, int pos, SLDataType x);//pos位置插入
    • void SLErase(SL* ps, int pos);//pos位置删除
    • int SLFind(SL* ps, SLDataType x);

顺序表的优缺点:

优点:存储密度高,可以随机存取结点。
缺点:1.长度为定值,中途无法扩充
2.插入删除需要移动结点,效率低

注意事项

1.N个数头删和尾删的时间复杂度是O(N)
N个数头插和尾插的时间复杂度是O(N^2)
//当数组只有一个元素时,移动1次,有两个元素时,移动两次,有三个元素时移动三次… 所以成等差数列经大O表达式为(N^2)

2.柔性数组与顺序表的关系
柔性数组是与结构体变量占一个空间
而顺序表是结构体额外开辟的空间

3.不能用realloc进行缩容
//缩容之后,可能缩容的部分会被使用,在进行扩容的话,不能回到原样

4.free()
//不能在动态数组中间进行释放,必须开辟多大空间,就一次释放完

顺序表
在这里插入图片描述
柔性数组
在这里插入图片描述

test.c(动态顺序表)

int main()
{SL s;SLInit(&s);int option = 0;while (option != -1){menu();scanf("%d", &option);if (option == 1){printf("请依次输入要尾插的数据");int x = 0;while (x != -1){scanf("%d", &x);SLPushBack(&s, x);}}else if (option == 7){SLPrint(&s);}}return 0; 
}

SeqList.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define INT_CAPACITY 4
typedef int  SLDataType;typedef struct SepList
{SLDataType* a;int size;int capacity;
}SL;void SLInit(SL* ps);
void SLDestory(SL* ps);
void SLPrint(SL* ps);void SLPushBack(SL* ps ,SLDataType * x );//尾插
void SLPopBack(SL* ps);//尾删
void SLPushFront(SL* ps,SLDataType * x );//头插
void SLPopFront(SL* ps);//头删
void SLCheckCapacity(SL* ps);//扩容void SLInsert(SL* ps, int pos, SLDataType x);//pos位置插入
void SLErase(SL* ps, int pos);//pos位置删除
int  SLFind(SL* ps, SLDataType x);

SeqList.c

#include "SeqList.h"
void SLCheckCapacity(SL* ps)
{assert(ps);if (ps->size == ps->capacity){		//realloc 可能会原地扩容 也可能异地扩容SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity* 2));if (tmp == NULL){perror("realloc fail");return;}ps->a = tmp;ps->capacity *= 2;}}void SLInit(SL* ps)
{assert(ps);ps->a = (SLDataType*)malloc(sizeof(SLDataType)*  INT_CAPACITY);if (ps->a == NULL){perror("malloc fail");return;}ps->size = 0;ps->capacity = INT_CAPACITY;
}
void SLDestory(SL* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->size = 0;ps->capacity = 0;
}
void SLPushBack(SL* ps, SLDataType  x)
{assert(ps);SLCheckCapacity(ps);//扩容ps->a[ps->size++] = x;//SLInsert(ps,ps->size, x)尾插
}
void SLPopBack(SL* ps)
{assert(ps);assert(ps->size > 0);/*if (ps->size == 0){return;}*/ps->size--;//尾删SLErase(ps, ps->size-1);
}
void SLPushFront(SL* ps ,SLDataType x)
{assert(ps);SLCheckCapacity(ps);int end = ps->size - 1;while (end >= 0){ps->a[end + 1] = ps->a[end];end--;}ps->a[0] = x;ps->size++;}
void SLPopFront(SL* ps)
{assert(ps);assert(ps->size > 0);int begin = 1;while (begin < ps->size){ps->a[begin - 1] = ps->a[begin];begin++;}ps->size--;
}
void SLPrint(SL* ps)
{assert(ps);for (int i = 0; i < ps->size; i++){printf("%d ", ps->a[i]);}
}void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);//可以等于size,相当于尾插SLCheckCapacity(ps);int end = ps->size-1;while (end >= pos){ps->a[end + 1] = ps->a[end];end--;}ps->a[pos] = x;ps->size++;
}void SLErase(SL* ps, int pos)
{assert(ps);assert(pos >= 0 && pos < ps->size);int begin = pos + 1;while (begin < ps->size){ps->a[begin-1] = ps->a[begin];begin++;}ps->size--; 
}int SLFind(SL* ps, SLDataType x)
{assert(ps);for (int i = 0; i < ps->size; i++){if (ps->a[i] == x)return i;}return -1;}

各接口函数功能详解

void SLInit(SL* ps);//定义

assert(ps);//进行assert判断ps这个结构体指针是否存在ps->a = (SLDataType*)malloc(sizeof(SLDataType)*  INT_CAPACITY);//使用malloc函数进行扩容,必须用free()释放 if (ps->a == NULL){perror("malloc fail");return;}ps->size = 0;初始化ps->capacity = INT_CAPACITY;

void SLDestory(SL* ps);

{assert(ps);free(ps->a);ps->a = NULL;ps->size = 0;ps->capacity = 0;
}

void SLPrint(SL* ps);

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

void SLPushBack(SL* ps ,SLDataType * x );

{assert(ps);SLCheckCapacity(ps);//判断是否扩容//扩容ps->a[ps->size++] = x;//SLInsert(ps,ps->size, x)尾插
}

void SLPopBack(SL* ps);

    assert(ps);assert(ps->size > 0);因为尾删判断数组是否有元素存在,如果没有元素则不可以尾删/*if (ps->size == 0){return;}*/ps->size--;//尾删SLErase(ps, ps->size-1);

void SLPushFront(SL* ps,SLDataType * x );

{assert(ps);SLCheckCapacity(ps);//是否扩容int end = ps->size - 1;while (end >= 0){ps->a[end + 1] = ps->a[end];end--;}ps->a[0] = x;ps->size++;}

在这里插入图片描述

void SLPopFront(SL* ps);

{assert(ps);assert(ps->size > 0);int begin = 1;while (begin < ps->size){ps->a[begin - 1] = ps->a[begin];begin++;}ps->size--;
}

在这里插入图片描述

void SLCheckCapacity(SL* ps);

	
void SLCheckCapacity(SL* ps)
{assert(ps);if (ps->size == ps->capacity){		//realloc所以扩容成功后,可能会原地扩容 也可能异地扩容,所以用ps->a = tmp来重新接收SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity* 2));if (tmp == NULL){perror("realloc fail");return;}ps->a = tmp;ps->capacity *= 2;}}

void SLInsert(SL* ps, int pos, SLDataType x);//pos位置插入

//可用于头插和尾插

assert(ps);assert(pos >= 0 && pos <= ps->size);//可以等于size,相当于尾插SLCheckCapacity(ps);int end = ps->size-1;while (end >= pos){ps->a[end + 1] = ps->a[end];end--;}ps->a[pos] = x;ps->size++;

void SLErase(SL* ps, int pos);//pos位置删除

//可用于头删和尾删


assert(ps);assert(pos >= 0 && pos < ps->size);int begin = pos + 1;while (begin < ps->size){ps->a[begin-1] = ps->a[begin];begin++;}ps->size--; 

int SLFind(SL* ps, SLDataType x);

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

在这里插入图片描述

http://www.lryc.cn/news/25165.html

相关文章:

  • 基于Linux_ARM板的驱动烧写及连接、挂载详细过程(附带驱动程序)
  • python-爬虫-字体加密
  • 计算机组成原理4小时速成5:输入输出系统,io设备与cpu的链接方式,控制方式,io设备,io接口,并行串行总线
  • 安全狗受聘成为福州网信办网络安全技术支撑单位
  • RV1126 在Ubuntu18.04开发环境搭建
  • 如何在 C++ 中调用 python 解析器来执行 python 代码(一)?
  • 操作系统权限提升(二十三)之Linux提权-通配符(ws)提权
  • Zookeeper下载和安装
  • Biomod2 (上):物种分布模型预备知识总结
  • 操作指南:如何高效使用Facebook Messenger销售(二)
  • 计算机三级|网络技术|中小型网络系统总体规划与设计方案|IP地址规划技术|2|3
  • 为什么一定要做集成测试?
  • 前端:CSS
  • CMMI—组织级过程定义(OPD)
  • 华为OD机试真题Python实现【猜字谜】真题+解题思路+代码(20222023)
  • 软测入门(三)Selenium(Web自动化测试基础)
  • 备战蓝桥杯——sort函数
  • 华为机试题:HJ86 求最大连续bit数(python)
  • 机器学习复习--logistic回归简单的介绍和代码调用
  • uniapp小程序接入腾讯地图sdk
  • 总结JavaScript中的条件判断与比较运算
  • 算法练习-排序(一)
  • CentOS7.6快速安装Docker
  • CentOS 7安装N卡驱动和CUDA和cuDNN
  • Java开发 - 分页查询初体验
  • C语言循环语句do while和嵌套循环语句讲解
  • 【计算机视觉】OpenCV 4高级编程与项目实战(Python版)【7】:拼接图像
  • 王道操作系统课代表 - 考研计算机 第二章 进程与线程 究极精华总结笔记
  • C++修炼之练气期三层——函数重载
  • 在linux上运行jar程序操作记录