数据结构学习——线性表、顺序表

1.线性表

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

线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。

image-20240331084641957

2.顺序表

2.1概念及结构

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

注:顺序表只能从头开始连续存储。

顺序表一般可以分为:

  1. 静态顺序表:使用定长数组存储元素

    image-20240304154453711

  2. 动态顺序表:使用动态开辟的数组存储。

    image-20240304154954903

    我们思考一个问题:为什么容量空间不够时候,我们经常扩容是进行2倍扩容呢?而不是其他倍数或者常数呢?

    因为2倍扩容相对合适,越扩频率越低而且c++库里面扩容也是2倍扩容。但是1.5倍扩容或者一倍扩容都可以。

2.2接口实现

2.2.1接口实现代码:

静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间 大小,所以下面我们实现动态顺序表:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SLDataType;//数据类型最好typedef下,否则数据类型会有各种变化,变化以后所有都得修改typedef struct SeqList
{//int* a;SLDataType* a; //指向动态开辟的数组int size;      //有效数据int capacity;  //空间容量
}SL;void SLInit(SL* psl);//初始化
void SLDestroy(SL* psl);//释放空间void SLPrint(SL* psl);//打印
void SLCheakCapacity(SL* psl);//检查空间容量是否够用,不够则扩容//头尾插入删除
void SLPushBack(SL* psl, SLDataType x);//尾插
void SLPushFront(SL* psl, SLDataType x);//头插
void SLPopBack(SL* psl);//尾删
void SLPopFront(SL* psl);//头删//任意下标位置的插入删除
void SLInsert(SL* psl, int pos, SLDataType x);//插入
void SLErase(SL* psl, int pos);//删除//找到返回下标
//没有找到返回-1
int SLFind(SL* psl, SLDataType x);
#include"SeqList.h"void SLInit(SL* psl)
{psl->a = NULL;psl->size = 0;psl->capacity = 0;
}void SLDestroy(SL* psl)
{assert(psl);if (psl->a != NULL){free(psl->a);psl->a != NULL;psl->size = 0;psl->capacity = 0;}
}void SLPrint(SL* psl)
{assert(psl);for (int i = 0; i < psl->size ;i++){printf("%d ", psl->a[i]);}printf("\n");
}void SLCheakCapacity(SL* psl)
{assert(psl);if (psl->size == psl->capacity){int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * newCapacity);if (tmp == NULL){perror("ralloc fail");return;}psl->a = tmp;psl->capacity = newCapacity;}
}void SLPushBack(SL* psl, SLDataType x)
{assert(psl);SLCheakCapacity(psl);psl->a[psl->size] = x;psl->size++;
}void SLPushFront(SL* psl, SLDataType x)
{assert(psl);SLCheakCapacity(psl);int end = psl->size - 1;while (end >= 0){psl->a[end + 1] = psl->a[end];--end;}psl->a[0] = x;psl->size++;
}void SLPopBack(SL* psl)
{assert(psl);if (psl->size == 0){return;}assert(psl->size >0);psl->size--;
}
void SLPopFront(SL* psl)
{assert(psl);assert(psl->size > 0);for (int begin = 0; begin < psl->size; begin++){psl->a[begin] = psl->a[begin+1];}psl->size--;
}void SLInsert(SL* psl, int pos, SLDataType x)
{assert(psl);assert(pos >= 0 && pos <= psl->size);//如果等于则是头插尾插SLCheakCapacity(psl);int end = psl->size;while (end > pos){psl->a[end] = psl->a[end - 1];end--;}psl->a[end] = x;psl->size++;
}//void SLInsert(SL* psl, int pos, SLDataType x)
//{
//	assert(psl);
//	assert(pos >= 0 && pos <= psl->size);
//
//	SLCheakCapacity(psl);
//
//	// 挪动数据
//	int end = psl->size - 1;
//	while (end >= pos)
//	{
//		psl->a[end + 1] = psl->a[end];
//		--end;
//	}
//
//	psl->a[pos] = x;
//	psl->size++;
//}void SLErase(SL* psl, int pos)
{assert(psl);assert(pos >= 0 && pos < psl->size);int begin = pos;for(;begin<psl->size ;begin++){ psl->a[begin] = psl->a[begin + 1];}psl->size--;
}int SLFind(SL* psl, SLDataType x)
{assert(psl);for (int j = 0; j < psl->size; j++){if (psl->a[j] == x){return j;}}return -1;
}
#include"SeqList.h"void TestSL1()
{SL sl;SLInit(&sl);SLPushBack(&sl, 1);SLPushBack(&sl, 2);SLPushBack(&sl, 3);SLPrint(&sl);SLPushFront(&sl, 10);SLPushFront(&sl, 20);SLPushFront(&sl, 30);SLPrint(&sl);SLPopBack(&sl);SLPopBack(&sl);SLPrint(&sl);SLPopFront(&sl);SLPopFront(&sl);SLPrint(&sl);
}void TestSL2()
{SL sl;SLInit(&sl);SLPushBack(&sl, 1);SLPushBack(&sl, 2);SLPushBack(&sl, 3);SLPushBack(&sl, 4);SLPushBack(&sl, 5);SLPrint(&sl);SLInsert(&sl, 3, 50);SLInsert(&sl, 3, 40);SLPrint(&sl);SLErase(&sl, 5);SLPrint(&sl);int pos = SLFind(&sl, 3);if (pos != -1){SLErase(&sl , pos);}SLPrint(&sl);}int main()
{TestSL2();// 越界一定报错吗?//int a[10];// 越界读基本不会报错//printf("%d\n", a[10]);//printf("%d\n", a[11]);// 越界写可能会报错//a[10] = 1;//a[11] = 1;//因为越界的检查是一种抽查行为return 0;
}

free错误:

1.free位置不对

2.存在越界访问

2.2.2 接口实现思路

SList.c :

void SLCheakCapacity(SL* psl);

检查空间是否足够,若不够则:空间为0则开辟4个空间,若不为零,则将原有的空间的大小乘以2

void SLCheakCapacity(SL* psl)
{assert(psl);if (psl->size == psl->capacity){int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * newCapacity);if (tmp == NULL){perror("ralloc fail");return;}psl->a = tmp;psl->capacity = newCapacity;}
}

image-20240319160311346

尾插:

void SLPushBack(SL* psl, SLDataType x)

先判断psl链表是否为空,为空直接结束,不为空继续执行。再检查空间是否够用,不够则开辟,够则把x的值给数组a,然后size++。

void SLPushBack(SL* psl, SLDataType x)
{assert(psl);SLCheakCapacity(psl);psl->a[psl->size] = x;psl->size++;
}

头插:

void SLPushFront(SL* psl, SLDataType x);
void SLPushFront(SL* psl, SLDataType x)
{assert(psl);SLCheakCapacity(psl);int end = psl->size - 1;while (end >= 0){psl->a[end + 1] = psl->a[end];--end;}psl->a[0] = x;psl->size++;
}

思路如下:

image-20240319163811791

尾删:

void SLPopBack(SL* psl);

void SLPopBack(SL* psl)
{assert(psl);if (psl->size == 0){return;}assert(psl->size >0);psl->size--;
}

image-20240319211228190

头删:

void SLPopFront(SL* psl);
void SLPopFront(SL* psl)
{assert(psl);assert(psl->size > 0);for (int begin = 0; begin < psl->size; begin++){psl->a[begin] = psl->a[begin+1];}psl->size--;
}

image-20240319211511721

任意下标位置的插入:

void SLInsert(SL* psl, int pos, SLDataType x);//插入
void SLInsert(SL* psl, int pos, SLDataType x)
{assert(psl);assert(pos >= 0 && pos <= psl->size);//如果等于则是头插尾插SLCheakCapacity(psl);int end = psl->size;while (end > pos){psl->a[end] = psl->a[end - 1];end--;}psl->a[end] = x;psl->size++;
}

image-20240319212326944

任意下标位置的删除:

void SLErase(SL* psl, int pos);//删除
void SLErase(SL* psl, int pos)
{assert(psl);assert(pos >= 0 && pos < psl->size);int begin = pos;for(;begin<psl->size ;begin++){ psl->a[begin] = psl->a[begin + 1];}psl->size--;
}

image-20240319213303535

找下标:

//找到返回下标
//没有找到返回-1
int SLFind(SL* psl, SLDataType x);
int SLFind(SL* psl, SLDataType x)
{assert(psl);for (int j = 0; j < psl->size; j++){if (psl->a[j] == x){return j;}}return -1;
}

image-20240319213814208

2.3 数组相关面试题

  1. 删除排序数组中的重复项。

    26. 删除有序数组中的重复项 - 力扣(LeetCode)

    代码:

    int removeDuplicates(int* nums, int numsSize) 
    {int src=1;int dst=0;while(src<numsSize){if(nums[src]==nums[dst]){src++;}else{dst++;nums[dst]=nums[src];src++;}}return dst+1;
    }

    解题思路:

    image-20240305191118302

  2. 合并两个有序数组。

    88. 合并两个有序数组 - 力扣(LeetCode)

    代码如下:

    void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
    {int j = m+n - 1;int n1 = m - 1;int n2 = n - 1;while(n1>=0 && n2>=0){if(nums1[n1]>nums2[n2]){nums1[j--]=nums1[n1--];}else{nums1[j--]=nums2[n2--];}}while(n2>=0){nums1[j--]=nums2[n2--];}
    }

    解题思路:

    image-20240305193102132

2.4 顺序表的问题及思考

问题: OJ链接 1. 中间/头部的插入删除,时间复杂度为O(N) 2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。 3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到 200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。 思考:如何解决以上问题呢?下面给出了链表的结构来看看。
= m - 1;
int n2 = n - 1;
while(n1>=0 && n2>=0)
{
if(nums1[n1]>nums2[n2])
{
nums1[j–]=nums1[n1–];
}
else
{
nums1[j–]=nums2[n2–];
}
}
while(n2>=0)
{
nums1[j–]=nums2[n2–];
}
}

解题思路:[外链图片转存中...(img-Iu1JfGs6-1712583978516)]## 2.4 顺序表的问题及思考问题: OJ链接 1. 中间/头部的插入删除,时间复杂度为O(N) 2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。 3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到 200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。 思考:如何解决以上问题呢?下面给出了链表的结构来看看。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/678899.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ICode国际青少年编程竞赛- Python-2级训练场-列表入门

ICode国际青少年编程竞赛- Python-2级训练场-列表入门 1、 Dev.step(3)2、 Flyer.step(1) Dev.step(-2)3、 Flyer.step(1) Spaceship.step(7)4、 Flyer.step(5) Dev.turnRight() Dev.step(5) Dev.turnLeft() Dev.step(3) Dev.turnLeft() Dev.step(7) Dev.turnLeft() Dev.…

pytest教程-41-钩子函数-pytest_runtest_teardown

领取资料&#xff0c;咨询答疑&#xff0c;请➕wei: June__Go 上一小节我们学习了pytest_runtest_call钩子函数的使用方法&#xff0c;本小节我们讲解一下pytest_runtest_teardown钩子函数的使用方法。 pytest_runtest_teardown 钩子函数在每个测试用例执行完成后被调用&…

数据结构学习——二叉树

1. 树概念及结构 1.1 树的概念 树是一种非线性的数据结构&#xff0c;它是由n&#xff08;n>0&#xff09;个有限结点组成一个具有层次关系的集合。把它叫做树是因为它看起来像一棵倒挂的树&#xff0c;也就是说它是根朝上&#xff0c;而叶朝下的。 有一个特殊的结点&…

【Docker学习】docker run的端口映射-p和-P选项

docker run的端口映射选项分为-p&#xff08;小写&#xff0c;全称--publish&#xff09;&#xff0c;-P&#xff08;大写&#xff0c;全称--publish-all&#xff09;&#xff0c;之前认为只有改变容器发布给宿主机的默认端口号才会进行-p的设置&#xff0c;而不改变默认端口号…

Java面试八股文(MySQL篇)

数据库三范式 数据库事务介绍 索引介绍 SQL优化手段 mysql union 与 union all 语法及用法 并发事务带来的问题 大表如何优化 索引类型介绍 MYSQL数据库锁介绍

leetcode91.解码方法(动态规划)

问题描述&#xff1a; 一条包含字母 A-Z 的消息通过以下映射进行了 编码 &#xff1a; A -> "1" B -> "2" ... Z -> "26" 要 解码 已编码的消息&#xff0c;所有数字必须基于上述映射的方法&#xff0c;反向映射回字母&#xff08;可…

如何才能做好源代码防泄密

目前很多企业都拥有自己的研发机构&#xff0c;其研发成果往往体现在源代码和技术文档方面&#xff0c;这些核心机密&#xff0c;如何防止研发参与人员泄密&#xff0c;如何防止核心成员把研究成果带走另立山头&#xff0c;或者提供给竞争对手&#xff0c;是一个很现实的一个问…

Vue-路由介绍

目录 一、思考引入 二、路由介绍 一、思考引入 单页面应用程序&#xff0c;之所以开发效率高&#xff0c;性能高&#xff0c;用户体验好&#xff0c;是因为页面按需更新。 而如果要按需更新&#xff0c;首先需要明确&#xff1a;访问路径和组件的对应关系。该关系通过路由来…

Vue CLI配置代理、2.0、3.0

一、vue cli2.0 代理配置 proxy: {/api:{target: "http://localhost:8067",pathRewrite: {/api: }}, } 一、vue cli3.0 代理配置 proxy: {/api: {target: http://localhost:8067,pathRewrite: {/api: }} }

【C++】滑动窗口:最大连续1的个数

1.题目 2.算法思路 其实在做这道题的时候并不需要真的把0翻转成1&#xff0c;只需要找到最长的子数组且该子数组中0的个数不大于K&#xff0c;就可以了&#xff01; 当然我们首先想到的是暴力穷举法&#xff1a; 找到所有符合题意的子数组&#xff0c;跳出最长的那个就可以了…

NodeMCU ESP8266 操作 SSD1306 OLED显示屏详解(图文并茂)

文章目录 1 模块介绍2 接线介绍3 安装SSD1306驱动库4 源码分析4.1 硬件兼容性4.2 可能存在的问题总结1 模块介绍 我们将在本教程中使用的OLED显示屏是SSD1306型号:单色0.96英寸显示屏,像素为12864,如下图所示。 OLED显示屏不需要背光,这在黑暗环境中会产生非常好的对比度。…

仓库管理系统需求调研要点

仓库管理系统需求调研 一、仓库的作用 仓库分类 原材料仓库&#xff1a;用于存放生产所需的原材料和零部件&#xff0c;需要保持原材料的质量和数量稳定。半成品仓库&#xff1a;存放生产过程中的半成品和在制品&#xff0c;需要保持良好的生产流程和及时出库。成品仓库&#x…