数据结构初阶 -- 顺序表

数据结构初阶 链表的讲解

目录

一. 线性表

1.1 定义

1.2 特点

二. 顺序表

2.1 定义

2.2 代码

2.3 功能需求

2.4 静态顺序表的特点以及缺点

2.5 动态的顺序表

2.6 动态顺序表接口的实现

三. 代码

头文件

主文件


一. 线性表

1.1 定义

  • 线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
  • 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

看这个定义 我们再联想前面的知识

其实顺序表本质上就是数组

但是它在数组上增加了一点内容

1.2 特点

它分为静态的和动态的

这个特点和我们做的项目 通讯录 十分相似

它是连续存储的 不能跳过元素

二. 顺序表

2.1 定义

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

2.2 代码

struct SeqList
{
	int a[100];             //数组
	int size;               //数组中存储了多少个 
};

我们说类似这个结构的 就是一个顺序表

        但是 为了我们以后改变数字方便 我们可以把这里的100 定义成一个宏 这样我们以后如果想修改顺序表的大小 只要改变宏就可以了

代码表示如下

// 静态顺序表
#define N 100
struct SeqList
{
	int a[N];             //数组
	int size;             //数组中存储了多少个
};

同样的 我们想使用顺序表来管理一个字符串

#define N 100
struct SeqList
{
	char a[N];        
	int size; 
};

        我们可以改变int类型 变为char类型的数据 但是这样每次改也太麻烦了 所以我们依旧可以再上面定义一个宏变量

#define N 100
typedef char SLDateType

struct SeqList
{
	int SLDateType[N]; 
	int size; 
};

可以使用这样的格式 方便以后一次性改变所有的变量类型

但是 这样子我们看整个结构体还是有点麻烦 我们再将这个结构体简化一下

typedef struct SeqList
{
	int SLDateType[N]; 
	int size; 
}SL;

2.3 功能需求

        在创建好这个静态表之后 我们要开始大概创建它的一些功能

比如说以下的一些功能

vovoid SeqListInit(SL* ps);
void SeqListPushBack(SL* ps, SLDateType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDateType x);
void SeqListPopFront(SL* ps);

2.4 静态顺序表的特点以及缺点

  • 特点: 如果满了就不让插入
  • 缺点: 不知道给多少合适

2.5 动态的顺序表

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

跟我们的通讯录特别相似

其实原理本质上都是一样的

2.6 动态顺序表接口的实现

初始化

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

尾插

  1.  没有空间
  2. 空间不够 扩容
  3. 空间足够

空间足够的情况

void SeqListPushBack(SL* ps, SLDateType x)
{
	ps->a[ps->size] = x;
	ps->size++;
}

代码表示如上

那么我们接下来我们写上面的两种情况

这里我们要注意的是 一开始我们将指针置空 占用的空间为0

所以说我们一开始至少要开始4个数据的空间 这里可以使用一个三目操作符解决

int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
void SeqListPushBack(SL* ps, SLDateType x)
{
	// 如果没有空间或者空间不足 我们就扩容 
	// 扩容失败就报错
	if ((ps->size)==(ps->capacity))
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp =(SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));
		if (tmp==NULL)
		{
			perror("pushback realloc");
		}
	}

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

这里我们使用一个打印函数看看整个数据的内容

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

打印结果如下

 在使用完成之后我们还需要一个接口函数来释放我们的动态开辟的内存 从而避免内存泄漏的问题

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

接下来我们看尾删函数

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

尾删的话其实我们只要将size-- 就可以

但是这里我们要注意一点 当size为0的时候 这里就不可以再删除了 所以我们还需要完善以下上面的代码

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

接下来我们看头插

void SeqListPushFront(SL* ps, SLDateType x)
{
	// 考虑扩容问题
	if ((ps->size) == (ps->capacity))
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->capacity = newcapacity;

		SLDateType* tmp = (SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));

		if (tmp == NULL)
		{
			perror("pushback realloc");
		}

		ps->a = tmp;
	}

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

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

头删

void SeqListPopFront(SL* ps)
{
	int bejin = 0;
	while (bejin<ps->size-1)
	{
		ps->a[bejin] = ps->a[bejin + 1];
		bejin++;
	}
	ps->size--;
}

 查找

int SeqListFind(SL* ps,int x)
{
	int i;
	for ( i = 0; i < ps->size; i++)
	{
		if (ps->a[i]==x)
		{
			return i;
		}
	}
	if (i==ps->size)
	{
		printf("不存在这个数");
	}
	return -1;
}

在pos位置处插入数字

void SeqListPushPos(SL* ps, int x, int y)
{
	if ((ps->size) == (ps->capacity))
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->capacity = newcapacity;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("pushback realloc");
		}
		ps->a = tmp;
	}
	// 和头插差不多 
	// 只不过头插的起始位置有点变化了
	int i;
	for (i = ps->size - 1; i >= x; i--)
	{
		(ps->a[i + 1]) = (ps->a[i]);
	}

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

在pos处删除数字

void SeqListPopPos(SL* ps, int x)
{
	assert(ps->size != 0);
	int i;
	for ( i =x; i <ps->size-1 ; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

这里我们基本实现了顺序表的所有接口函数

三. 代码

头文件

#pragma once
#define N 100
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDateType;

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


void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListPushBack(SL* ps, SLDateType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDateType x);
void SeqListPopFront(SL* ps);
void SeqListPrint(SL* ps);

主文件

#include"SeqList.h"

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


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

void SeqListDestory(SL* ps)
{
	free(ps->a);
	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, newcapacity*sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}

void SeqListPushBack(SL* ps, int x)
{
	/*SeqListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;*/

	SeqListInsert(ps, ps->size, x);

}

void SeqListPopBack(SL* ps)
{
	/*if (ps->size == 0)
		return;*/

	/*assert(ps->size > 0);
	ps->size--;*/


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

void SeqListPushFront(SL* ps, int 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++;*/


	SeqListInsert(ps, 0, x);
}

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

	/*int cur = 1;
	for (cur = 1; cur < ps->size; cur++)
	{
		ps->a[cur - 1] = ps->a[cur];
	}

	ps->size--;*/

	SeqListErase(ps, 0);
}

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

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

	SeqListCheckCapacity(ps);

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

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

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

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值