顺序表
1、数据结构相关概念
数据结构是由“数据”和“结构”两词组合⽽来。
什么是数据?
常⻅的数值1、2、3、4…、教务系统⾥保存的⽤⼾信息(姓名、性别、年龄、学历等等)、⽹⻚⾥⾁眼可以看到的信息(⽂字、图⽚、视频等等),这些都是数据
什么是结构?
当我们想要使⽤⼤量使⽤同⼀类型的数据时,通过⼿动定义⼤量的独⽴的变量对于程序来说,可读性⾮常差,我们可以借助数组这样的数据结构将⼤量的数据组织在⼀起,结构也可以理解为组织数据的⽅式
概念:数据结构是计算机存储、组织数据的⽅式。数据结构是指相互之间存在⼀种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么⽅式构成,以及数据元素之间呈现的结构。
总结:
1)能够存储数据(如顺序表、链表等结构)
2)存储的数据能够⽅便查找
2、为什么需要数据结构?
通过数据结构,能够有效将数据组织和管理在⼀起。按照我们的⽅式任意对数据进⾏增删改查等操作。
最基础的数据结构:数组。
【思考】有了数组,为什么还要学习其他的数据结构?
假定数组有10个空间,已经使⽤了5个,向数组中插⼊数据步骤:
求数组的⻓度,求数组的有效数据个数,向下标为数据有效个数的位置插⼊数据(注意:这⾥是否要判断数组是否满了,满了还能继续插⼊吗)…
假设数据量⾮常庞⼤,频繁的获取数组有效数据个数会影响程序执⾏效率。
结论:最基础的数据结构能够提供的操作已经不能完全满⾜复杂算法实现。
2、顺序表
2.1、顺序表的概念及结构
线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使 ⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,
线性表在物理上存储时,通常以数组和链式结构的形式存储。
案例:蔬菜分为绿叶类、⽠类、菌菇类。线性表指的是具有部分相同特性的⼀类数据结构的集合
2.2、顺序表分类
• 顺序表和数组的区别:
◦ 顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝
• 顺序表分类
◦ 静态顺序表
概念:使⽤定⻓数组存储元素
静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费
◦ 动态顺序表
2.3、动态顺序表的实现
#define INIT_CAPACITY 4
typedef int SLDataType;
// 动态顺序表 -- 按需申请
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);
3、ps:源码
SeqList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int type;
//定义顺序表
struct SeqList
{
type* arr;
type size;//有效个数
type room;//空间大小个数
};
typedef struct SeqList SL;
//顺序表的初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//顺序表的尾部插入
void SLPushBack(SL* ps, type x);
//扩容
void SLCheckCapacity(SL* ps);
//顺序表的头部插入
void SLPushFront(SL* ps,type x);
//顺序表的尾部删除
void SLPopBack(SL* ps);
//顺序表的头部删除
void SLPopFront(SL* ps);
//指定位置之前插⼊数据
void SLInsert(SL* ps, int pos,type x);
//指定位置之前删除数据
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps,type x);
//打印顺序表
void SLprint(SL s);
SeqList.c
# define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->room = 0;
}
void SLDestroy(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->room = 0;
}
void SLCheckCapacity(SL* ps)
{
assert(ps);
type newroom = ps->room == 0 ? 4 : ps->room * 2;
if (ps->size == ps->room)
{
type* tmp = (type*)realloc(ps->arr, newroom * sizeof(type));
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
ps->room = newroom;
}
}
void SLPushBack(SL* ps, type x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
void SLPushFront(SL* ps, type x)
{
assert(ps);
SLCheckCapacity(ps);
for (type 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 = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];//ps->arr[size - 2] = ps->arr[size - 1]
}
ps->size--;
}
void SLInsert(SL* ps, int pos, type 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];//[pos+1] = [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];// arr[size - 2] = arr[size - 1];
}
ps->size--;
}
int SLFind(SL* ps, type x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (x == ps->arr[i])
{
//找到了
return i;
}
}
return -1;
}
void SLprint(SL s)
{
for (int i = 0; i < s.arr[s.size]; i++)
{
printf("%d ", s.arr[i]);
}
printf("\n");
}
test.c
# define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"
void test01()
{
SL sl;
SLInit(&sl);
//测试扩容
//SLCheckCapacity(&sl);
//测试尾插
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
//测试头插
SLPushFront(&sl, 0);
SLPushFront(&sl, 5);
//测试尾删
// SLPopBack(&sl);
//测试头删
SLPopFront(&sl);
}
test02()
{
SL sl;
SLInit(&sl);
//测试指定位置之前插⼊数据
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
/*SLInsert(&sl,0,66);
SLInsert(&sl,4,66);
SLInsert(&sl,2,66);*/
//测试指定位置之前删除数据
/*SLErase(&sl,0);
SLErase(&sl,3);
SLErase(&sl,1);*/
//测试查找
int ret = SLFind(&sl,3);
if (ret < 0)
{
printf("没有找到\n");
}
else
{
printf("找到了,下标为%d\n", ret);
}
}
int main()
{
//test01();
test02();
return 0;
}
标签:ps,arr,少年,void,sl,梦该,SL,size
From: https://blog.csdn.net/f_2789889770/article/details/137276980