数据结构——顺序表的实现

数据结构------顺序表的实现

  • [一 关于顺序表的简单知识](#一 关于顺序表的简单知识)
  • [二 动态顺序表](#二 动态顺序表)

一 关于顺序表的简单知识

1.顺序表的底层结构是数组 ,在数组的基础上增加了增,删,查,改等方法。

2.顺序表的分类:静态顺序表和动态顺序表

静态顺序表的缺陷:给小了,空间不够;给大了,造成空间浪费。
动态顺序表:可以实现动态增容(成倍数的增加,一般成二倍的形式增加)

3.顺序表是线性表的一种,在物理结构和逻辑结构上都是线性的。

二 动态顺序表

由于静态顺序表的不灵活性,所以一般使用动态顺序表,接下来,我主要给大家讲解动态顺序表。

但是,在此之前,我还是把静态顺序表给大家讲清楚。

c 复制代码
#define N 100;//添加宏定义,可以更容易的更改底层数组大小
struct SeqList
{
 int arr[N];//静态顺序表底层结构是一个固定大小的数组,由此造成了它的不灵活性
 int size;//有效数据长度
 }
 

接下来,就是动态顺序表了。

动态顺序表的头文件

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
//定义顺序表
typedef int SLDateList;
typedef struct SeqList
{
	SLDateList* arr;//由于动态顺序表不知道数组的大小,所以使用指针。
	int size;
	int capacity;

}SL;

//初始化
void SLInit(SL* ps);

//销毁
void SLDestory(SL* ps);

//尾插
void SLPushBack(SL* ps, SLDateList x);
//头插
void SLPushFront(SL* ps, SLDateList x);
//尾删
void SLPopBack(SL* ps);
//头删
void SLPopFront(SL* ps);
//打印
void SLPrint(SL ps);
//查找
int SLFind(SL* ps, SLDateList x);
//在指定位置插入数据
void SLInit(SL* ps, SLDateList pos, SLDateList x);
//在指定位置删除数据
void SLErase(SL* ps, SLDateList pos);

动态顺序表的源文件

c 复制代码
#include"SE.h"

void SLInit(SL * ps)
{
	ps->arr = NULL;
	ps-> size = ps-> capacity = 0;
 }
//头插,尾插都要判断顺序表是否为空
void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//注意是相等,不是赋值
		SLDateList* tmp = (SLDateList*)realloc(ps->arr, newcapacity * sizeof(SLDateList));
		if (tmp == NULL)
		{
			perror("realloc file!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

void SLPushBack(SL* ps, SLDateList x)
{
	assert(ps);//顺序表不能传空
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
	
}


void SLPushFront(SL* ps, SLDateList 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 SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	--ps->size;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = ps->size; i<ps->size-1; i--)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;

}

void SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d",ps.arr[i]);
	}
	printf("\n");
}

int SLFind(SL* ps, SLDateList x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
		else
			return -1;
	}
}
void SLInit(SL* ps, SLDateList pos, SLDateList x)
{
	assert(ps);
	assert(pos>=0 && pos<=ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
	
}
void SLErase(SL* ps, SLDateList pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	SLCheckCapacity(ps);
	for (int i = pos ; i<ps->size-1; i++)
	{
		ps->arr[i - 1] = ps->arr[i];//size-2 = size-1
	}
	ps->size--;
}

void SLDestory(SL* ps)
{
	if (ps->arr)//销毁谁,销毁的是已经申请过空间的数组
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;

}
相关推荐
睡不醒的kun19 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌19 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long19 小时前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn19 小时前
Redis7底层数据结构解析
前端·数据结构·bootstrap
我星期八休息19 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list
和编程干到底21 小时前
数据结构 栈和队列、树
数据结构·算法
爱编程的化学家21 小时前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
shan&cen1 天前
Day04 前缀和&差分 1109. 航班预订统计 、304. 二维区域和检索 - 矩阵不可变
java·数据结构·算法
屁股割了还要学1 天前
【数据结构入门】排序算法(4)归并排序
c语言·数据结构·学习·算法·排序算法
Chance_to_win1 天前
数据结构之顺序表
数据结构