• 数据结构与算法(C语言版)P2---线性表之顺序表


    前景回顾

    数据结构
    数据的逻辑结构
    数据的存储结构
    数据的运算
    线性结构
    非线性结构
    线性表
    队列
    字符串,数组,广义表
    树形结构
    图形结构
    索引,排序,插入,删除,修改等

    1、线性表

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

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

    顺序表本质上就是数组。

    在这里插入图片描述

    2、顺序表

    2.1、概念及结构

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

    顺序表就是数组,但是在数组的基础上,它还要求数据是从头开始存储并且是连续存储的,不能跳跃间隔。

    顺序表一般可以分为:

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

    在这里插入图片描述

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

    在这里插入图片描述

    3、顺序表的实现

    【说明】:这里使用动态数组来实现顺序表。

    顺序表主要有以下几个接口功能:

    • 打印(SeqListPrint)
    • 结构体初始化(SeqListInit)
    • 释放空间(SeqListDestory)
    • 检查扩容(SeqListCheckCapacity)
    • 头插(SeqListPushBack)
    • 头删(SeqListPopBack)
    • 尾插(SeqListPushFront)
    • 尾删(SeqListPopFront)
    • 查找元素(SeqListFind)
    • 在指定pos下标位置插入元素(SeqListInsert)
    • 删除pos位置的数据(SeqListErase)

    3.1、定义结构体

    #pragma once
    #include 
    #include 
    #include 
    
    typedef int SLDataType;
    
    typedef struct SeqList
    {
    	SLDataType* a;
    	int size;
    	int capacity;
    }SL;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    a指针变量表示需要动态开辟的数组。
    size变量表示数组中有效数据个数。
    capacity变量表示动态开辟数组的空间大小。

    3.2、结构体初始化接口实现

    //结构体初始化
    void SeqListInit(SL* ps)
    {
    	ps->a = NULL;
    	ps->size = ps->capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.3、检查扩容接口实现

    因为我们采用动态开辟数组的形式来实现顺序表,所以当我们在进行任何一个插入操作时,都需要先申请空间,以便数据的插入。
    这里有几种情况需要我们处理:

    • 整个顺序表没有空间,扩容
    • 空间不够,扩容。
    • 空间足够,直接插入数即可。
    void SeqListCheckCapacity(SL* ps)
    {
    	if (ps->size == ps->capacity)
    	{
    		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
    		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * newcapacity);
    		if (tmp == NULL)
    		{
    			printf("realloc fail\n");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newcapacity;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.4、头插接口实现

    void SeqListPushFront(SL* ps, SLDataType x)
    {
    	SeqListCheckCapacity(ps);
    
    	int end = ps->size - 1;
    	while (end >= 0)
    	{
    		ps->a[end + 1] = ps->a[end];
    		end--;
    	}
    	ps->a[0] = x;
    	ps->size++;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    头插的核心思想:将全体数据元素向后移动。比如现在有数组int b = [1,2,3,4,5],现在想头插数据6,可以采取这样的方法:把1,2,3,4,5全部整体向后移一位。然后再把6进行头插。

    3.5、打印接口实现

    void SeqListPrint(SL* ps)
    {
    	for (int i = 0; i < ps->size; i++)
    	{
    		printf("%d ", ps->a[i]);
    	}
    	printf("\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.6、销毁接口实现

    void SeqListDestroy(SL* ps)
    {
    	free(ps->a);
    	ps->a = NULL;
    	ps->size = ps->capacity =  0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.7、头删接口实现

    void SeqListPopFront(SL* ps)
    {
    	//这里需要加个判断,判断size是否>0,否则防止越界访问
    	if (ps->size > 0)
    	{
    		int begin = 1;
    		while (begin < ps->size)
    		{
    			ps->a[begin - 1] = ps->a[begin];
    			begin++;
    		}
    		ps->size--;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    头删核心思想:把数组中下标为1到下标为最后一个的所有元素全部向前移动。

    3.8、尾插接口实现

    void SeqListPushBack(SL* ps, SLDataType x)
    {
    	//先检查扩容
    	SeqListCheckCapacity(ps);
    
    	ps->a[ps->size] = x;
    	ps->size++;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.9、尾删接口实现

    void SeqListPopBack(SL* ps)
    {
    	if (ps->size > 0)
    	{
    		ps->size--;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.10、查询指定元素

    //查询指定元素,并返回下标
    int SeqListFind(SL* ps, SLDataType x)
    {
    	for (int i = 0; i < ps->size; i++)
    	{
    		if (x == ps->a[i])
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.11、在指定pos下标位置插入

    void SeqListInsert(SL* ps, int pos, SLDataType x)
    {
    	assert(pos >= 0 && ps->size >= pos);
    
    	SeqListCheckCapacity(ps);
    
    	int end = ps->size - 1;
    	while (pos <= end)
    	{
    		ps->a[end + 1] = ps->a[end];
    		end--;
    	}
    	ps->a[pos] = x;
    	ps->size++;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3.12、删除pos下标位置的数据

    void SeqListErase(SL* ps, int pos)
    {
    	assert(pos >= 0 && ps->size > pos);
    
    	int begin = pos + 1;
    	while (begin < ps->size)
    	{
    		ps->a[begin - 1] = ps->a[begin];
    		begin++;
    	}
    	ps->size--;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    以上就是使用动态数组实现顺序表的过程。下面展示全代码。

    4、全代码展示

    这里使用三个文件:

    • seqlist.h:用于结构体、各种函数接口的声明。
    • seqlist.c:用于各种函数接口的定义。
    • test.c:用于创建顺序表,实现顺序表。

    4.1、seqlist.h

    #pragma once
    #include 
    #include 
    #include 
    
    typedef int SLDataType;
    
    typedef struct SeqList
    {
    	SLDataType* a;
    	int size;
    	int capacity;
    }SL;
    
    //结构体初始化
    void SeqListInit(SL* ps);
    
    //检查扩容
    void SeqListCheckCapacity(SL* ps);
    
    //头插
    void SeqListPushFront(SL* ps, SLDataType x);
    
    //打印
    void SeqListPrint(SL* ps);
    
    //销毁
    void SeqListDestroy(SL* ps);
    
    //头删
    void SeqListPopFront(SL* ps);
    
    //尾插
    void SeqListPushBack(SL* ps, SLDataType x);
    
    //尾删
    void SeqListPopBack(SL* ps);
    
    //查询指向元素,并返回下标
    int SeqListFind(SL* ps, SLDataType x);
    
    //在指定pos下标位置插入
    void SeqListInsert(SL* ps, int pos, SLDataType x);
    
    //删除pos下标位置的数据
    void SeqListErase(SL* ps, int pos);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    4.2、seqlist.c

    #include "list.h"
    
    //结构体初始化
    void SeqListInit(SL* ps)
    {
    	ps->a = NULL;
    	ps->size = ps->capacity = 0;
    }
    
    //检查扩容
    void SeqListCheckCapacity(SL* ps)
    {
    	if (ps->size == ps->capacity)
    	{
    		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
    		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * newcapacity);
    		if (tmp == NULL)
    		{
    			printf("realloc fail\n");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newcapacity;
    	}
    }
    
    //头插
    void SeqListPushFront(SL* ps, SLDataType x)
    {
    	SeqListCheckCapacity(ps);
    
    	int end = ps->size - 1;
    	while (end >= 0)
    	{
    		ps->a[end + 1] = ps->a[end];
    		end--;
    	}
    	ps->a[0] = x;
    	ps->size++;
    }
    
    //打印
    void SeqListPrint(SL* ps)
    {
    	for (int i = 0; i < ps->size; i++)
    	{
    		printf("%d ", ps->a[i]);
    	}
    	printf("\n");
    }
    
    //销毁
    void SeqListDestroy(SL* ps)
    {
    	free(ps->a);
    	ps->a = NULL;
    	ps->size = ps->capacity =  0;
    }
    
    //头删
    void SeqListPopFront(SL* ps)
    {
    	//这里需要加个判断,判断size是否>0,否则防止越界访问
    	if (ps->size > 0)
    	{
    		int begin = 1;
    		while (begin < ps->size)
    		{
    			ps->a[begin - 1] = ps->a[begin];
    			begin++;
    		}
    		ps->size--;
    	}
    }
    
    //尾插
    void SeqListPushBack(SL* ps, SLDataType x)
    {
    	//先检查扩容
    	SeqListCheckCapacity(ps);
    
    	ps->a[ps->size] = x;
    	ps->size++;
    }
    
    //尾删
    void SeqListPopBack(SL* ps)
    {
    	if (ps->size > 0)
    	{
    		ps->size--;
    	}
    }
    
    //扩展接口
    //查询指定元素,并返回下标
    int SeqListFind(SL* ps, SLDataType x)
    {
    	for (int i = 0; i < ps->size; i++)
    	{
    		if (x == ps->a[i])
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    
    //在指定pos下标位置插入
    void SeqListInsert(SL* ps, int pos, SLDataType x)
    {
    	assert(pos >= 0 && ps->size >= pos);
    
    	SeqListCheckCapacity(ps);
    
    	int end = ps->size - 1;
    	while (pos <= end)
    	{
    		ps->a[end + 1] = ps->a[end];
    		end--;
    	}
    	ps->a[pos] = x;
    	ps->size++;
    }
    
    //删除pos下标位置的数据
    void SeqListErase(SL* ps, int pos)
    {
    	assert(pos >= 0 && ps->size > pos);
    
    	int begin = pos + 1;
    	while (begin < ps->size)
    	{
    		ps->a[begin - 1] = ps->a[begin];
    		begin++;
    	}
    	ps->size--;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    4.3、test.c

    #include "list.h"
    
    int main()
    {
    	SL sl;
    	SeqListInit(&sl);
    	SeqListPushFront(&sl, 1);
    	SeqListPushFront(&sl, 2);
    	SeqListPushFront(&sl, 3);
    	
    	int pos = SeqListFind(&sl, 2);
    	SeqListErase(&sl, pos);
    	
    
    	SeqListPrint(&sl);
    	SeqListDestroy(&sl);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5、顺序表的优缺点

    顺序表的缺陷:

    • 空间不够了需要扩容,扩容是需要付出代价的。
    • 避免频繁的扩容,我们满了基本都是扩2倍。这可能就会导致一定空间的浪费。
    • 顺序表要求数据从开始位置连续存储,那么我们在头部或者中间位置插入、删除数据就需要挪动数据,有性能消耗,效率不高。

    顺序表的优点:

    • 支持随机访问。
    • 存储密度大
  • 相关阅读:
    记一次stm32开发的环境搭建过程
    我在windows环境下的YOLOV3环境搭建过程
    Docker 日志
    可信计算专业课(持续更新中)
    技术篇——废水除铊、除铊吸附树脂技术
    CCC数字钥匙设计【NFC】--通过NFC进行车主配对Phase3
    Java 设计模式 Builder 模式 链式编程
    Docker部署xxl-job
    css 高级选择器
    合规、高效,加快药企数字化转型,全新打造药企文档资源中心
  • 原文地址:https://blog.csdn.net/m0_57776598/article/details/132910027