数据结构与算法(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 <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* a;
	int size;
	int capacity;
}SL;

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

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

//结构体初始化
void SeqListInit(SL* ps)
{
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

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;
	}
}

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++;
}

头插的核心思想:将全体数据元素向后移动。比如现在有数组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");
}

3.6、销毁接口实现

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

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到下标为最后一个的所有元素全部向前移动。

3.8、尾插接口实现

void SeqListPushBack(SL* ps, SLDataType x)
{
	//先检查扩容
	SeqListCheckCapacity(ps);

	ps->a[ps->size] = x;
	ps->size++;
}

3.9、尾删接口实现

void SeqListPopBack(SL* ps)
{
	if (ps->size > 0)
	{
		ps->size--;
	}
}

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;
}

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++;
}

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--;
}

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

4、全代码展示

这里使用三个文件:

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

4.1、seqlist.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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);

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--;
}

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;
}

5、顺序表的优缺点

顺序表的缺陷:

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

顺序表的优点:

  • 支持随机访问。
  • 存储密度大
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让我三行代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值