顺序表专题

1.顺序表概念和结构

1.1. 线性表

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

1.2.顺序表

顺序表的底层结构是数组,对数组的封装,实现了常用的增删改查等接口。顺序表是线性表的一种,在逻辑结构上是连续的,在物理结构上也是连续的。

1.3为什么会有顺序表?

对于数组,有以下操作:
1.修改。直接arr[pos]=x。比较简单
2.插入数据。找数组中已有的元素个数,再插入数据。麻烦
3.删除数据。找数组中已有的元素个数,再删除数据。麻烦
4.……
我们将对于数组比较麻烦的操作,比如增删查改封装成更为简单的操作,这就是顺序表的作用。

2.顺序表分类

2.1.静态顺序表

//定义顺序表结构
#define N 100
typedef int SLDataType;//要存其他类型改这里就行了
//静态顺序表
struct SeqList
{
	SLDataType arr[N];
	int size;//有效数据个数
};

这个size定了就是定了改不了了。
静态顺序表缺陷:空间给少了不够用,给多了造成空间浪费。

2.2.动态顺序表

typedef int SLDataType;//要存其他类型改这里就行了
//typedef char SLDataType;
//动态顺序表
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据个数
	int capacity;//空间大小
}SL;  

//typedef struct SeqList SL; 

动态顺序表可以通过capacity来改变空间大小。

3.实现动态顺序表

3.1.准备

  1. 我们需要两个.c文件:
    一个<SeqList.c>来具体实现操作
    一个<test.c>来检验操作的实现
  2. 我们需要一个.h文件:
    一个<SeqList.h>里面是各种操作声明

使用逻辑:在<SeqList.c>中是具体的代码实现,在<SeqList.h>是对操作的声明,在<test.c>包含一下<SeqList.h>就可以使用各种现场的操作了

链接: 理解头文件和源文件(如果需要)

3.2.<SeqList.h>

#pragma once

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

//定义顺序表结构
//#define N 100

//静态顺序表
//struct SeqList
//{
//	int arr[N];
//	int size;//有效数据个数
//};


typedef int SLDataType;//要存其他类型改这里就行了
//typedef char SLDataType;
//动态顺序表
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据个数
	int capacity;//空间大小
}SL;  

//typedef struct SeqList SL; 


//顺序表初始化
void SLInit(SL *ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//顺序表的打印
void SLPrint(SL s);



//尾部插入
void SLPushBack(SL* ps, SLDataType x);
//头部插入
void SLPushFront(SL* ps, SLDataType x);
//尾部删除
void SLPopBack(SL* ps);
//头部删除
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”中都有一一对应实现

3.3<SeqList.c>

#define _CRT_SECURE_NO_WARNINGS

#include"SeqList.h"//这里主要是动态顺序表的类型定义SL

//顺序表的初始化
void SLInit(SL *ps)
{
	ps->arr = NULL;//初始情况下没数据置为空
	ps->size = ps->capacity = 0;
	//指针要用->;不能用.
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

做各种操作之前大多要判断现有空间,这里封装成函数。

void SLCheckCapacity(SL* ps)
{
	//插入数据之前先看空间够不够
	if (ps->capacity == ps->size)
	{
		//空间满了不能插入了,要申请空间
		//malloc calloc(malloc基础上初始化) realloc int arr[100]--->增容
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * 2 * sizeof(SLDataType));//要申请多大的 
		if (tmp == NULL)//不能直接用p->arr 万一申请失败会有影响
		{
			perror("realloc fail");
			exit(1);//直接退出程序不再执行
		}
		//空间申请成功
		ps->arr = tmp;
		ps->capacity = newCapacity;

	}
}

顺序表的打印

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

尾插头插

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	//温柔的解决方式
	/*if (ps==NULL)
	{
		return;
	}
	*/
	assert(ps); //等价于assert(ps!=NULL)

	//ps->arr[ps->size] = x;
	//++ps->size;
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
	
}


//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	//先让顺序表整体往后移动
	for (int i = ps->size; i>0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[1]=arr[0]
	}
	ps->arr[0] = x;
	ps->size++;
}

尾删头删

//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//顺序表不为空
	ps->arr[ps->size - 1] = -1;
	--ps->size;
}
//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);

	//数据整体往前挪动一位
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
		//arr[size-2]=arr[size-1]
	}
	ps->size--;
}

在指定位置之前插入数据

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	//插入数据:空间够不够
	SLCheckCapacity(ps);
	//让pos及之后的数据整体往后挪动一位
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[pos+1] = arr[pos]
	}
	ps->arr[pos] = x;
	ps->size++;
}

删除指定位置的数据

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

	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

查找

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			//找到啦
			return i;
		}
	}
	//没有找到
	return -1;
}

3.4.<test.h>

#define _CRT_SECURE_NO_WARNINGS

#include"SeqList.h"

void SLTest01()
{
	SL s1;
	//SLInit(s1);//传值  
	SLInit(&s1);//传地址
	//增删查改操作
	//顺序表的销毁
	SLPushBack(&s1, 1);
	SLPushBack(&s1, 2);
	SLPushBack(&s1, 3);
	SLPushBack(&s1, 4);
	SLPrint(s1);
	//SLPushBack(NULL, 4);
	SLPushFront(&s1, 5);
	SLPushFront(&s1, 6);
	//测试头删
	SLPopBack(&s1);
	SLPrint(s1);
	SLPopFront(&s1);
	SLPrint(s1);
	SLPopBack(&s1);
	SLPrint(s1);
	//......
	SLDestroy(&s1);
}

int main()
{
	SLTest01();
	return 0;
}

这里只是个示例,当然想怎么用怎么用。

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值