顺序表数据结构

1.线性表

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

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

2.顺序表

2.1概念及结构

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

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

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

2.2 接口实现(大概思路)

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

typedef int SLDataType;

// 顺序表的动态存储

typedef struct SeqList

{
  SLDataType* array;  // 指向动态开辟的数组

  size_t size ;       // 有效数据个数

  size_t capicity ;   // 容量空间的大小

}SeqList;

// 基本增删查改接口

// 顺序表初始化
2.3 数组相关面试题 

1. 原地移除数组中所有的元素val,要求时间复杂度为O(N),空间复杂度为O(1)。OJ链接

2. 删除排序数组中的重复项。OJ链接

3. 合并两个有序数组。OJ链接

2.4 顺序表的问题及思考 

问题:

1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。

3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到

200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考:如何解决以上问题呢?下面给出了链表的结构来看看。

3.链表 

3.1 链表的概念及结构 

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。

void SeqListInit(SeqList* psl);

// 检查空间,如果满了,进行增容

void CheckCapacity(SeqList* psl);

// 顺序表尾插

void SeqListPushBack(SeqList* psl, SLDataType x);

// 顺序表尾删

void SeqListPopBack(SeqList* psl);

// 顺序表头插

void SeqListPushFront(SeqList* psl, SLDataType x);

// 顺序表头删

void SeqListPopFront(SeqList* psl);

// 顺序表查找

int SeqListFind(SeqList* psl, SLDataType x); 

// 顺序表在pos位置插入x

void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);

// 顺序表删除pos位置的值

void SeqListErase(SeqList* psl, size_t pos);

// 顺序表销毁

void SeqListDestory(SeqList* psl);

// 顺序表打印

void SeqListPrint(SeqList* psl);

代码实现

SeqList.h

头文件

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

静态顺序表 -- 开少了不够用 开多了浪费

struct SeqList

{

 SLDataType a[N];

 int size;

};

动态顺序表 -- 按需申请

typedef int SLDataType;

#define INIT_CAPACITY 4

typedef struct SeqList

{

SLDataType* a;

int size; // 有效数据个数

int capacity; // 空间容量

}SL;

增删查改

void SLInit(SL* ps);

void SLDestroy(SL* ps);

void SLPrint(SL* ps);

void SLCheckCapacity(SL* ps);

void SLPushBack(SL* ps, SLDataType x);

void SLPopBack(SL* ps);

void SLPushFront(SL* ps, SLDataType x);

void SLPopFront(SL* ps);

void SLInsert(SL* ps, int pos, SLDataType x);

void SLErase(SL* ps, int pos);

int SLFind(SL* ps, SLDataType x);

SeqList.c

初始化

#include"SeqList.h"

void SLInit(SL* ps)
{
	assert(ps);

	ps->a = (SLDataType*)malloc(sizeof(SLDataType)* INIT_CAPACITY);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}

	ps->size = 0;
	ps->capacity = INIT_CAPACITY;
}

销毁

void SLDestroy(SL* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

输出

void SLPrint(SL* ps)
{
	assert(ps);

	for (int i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");

扩容

void SLCheckCapacity(SL* ps)
{
	assert(ps);

	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}
}

尾插

void SLPushBack(SL* ps, SLDataType x)
{
	//assert(ps);

	 扩容
	//SLCheckCapacity(ps);

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

	SLInsert(ps, ps->size, x);
}

尾删

void SLPopBack(SL* ps)
{
	//assert(ps);

	 暴力检查
	//assert(ps->size > 0);

	 温柔的检查
	if (ps->size == 0)
	//	//return;

	ps->a[ps->size - 1] = 0;
	//ps->size--;

	SLErase(ps, ps->size-1);
}

头插

void SLPushFront(SL* ps, SLDataType x)
{
	/*assert(ps);

	SLCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

	ps->a[0] = x;
	ps->size++;*/

	SLInsert(ps, 0, x);
}

头删

void SLPopFront(SL* ps)
{
	//assert(ps);
	//assert(ps->size > 0);

	//int begin = 1;
	//while (begin < ps->size)
	//{
	//	ps->a[begin - 1] = ps->a[begin];
	//	++begin;
	//}

	//ps->size--;

	SLErase(ps, 0);
}

在pos位置插入x

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);

	SLCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

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

删除pos位置的值

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		++begin;
	}

	ps->size--;
}

顺序表查找

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for(int i = 0; i < ps->size; ++i)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}

  • 39
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值