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

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

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

一 关于顺序表的简单知识

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;

}
相关推荐
gorgor在码农2 分钟前
Mysql 索引底层数据结构和算法
数据结构·数据库·mysql
武昌库里写JAVA1 小时前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
一休哥助手2 小时前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
苏宸啊3 小时前
顺序表及其代码实现
数据结构·算法
lin zaixi()3 小时前
贪心思想之——最大子段和问题
数据结构·算法
夜雨翦春韭3 小时前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
一直学习永不止步3 小时前
LeetCode题练习与总结:H 指数--274
java·数据结构·算法·leetcode·数组·排序·计数排序
Amor风信子3 小时前
华为OD机试真题---跳房子II
java·数据结构·算法
Ljubim.te4 小时前
软件设计师——数据结构
数据结构·笔记
_GR5 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划