【数据结构】顺序表
👑个人主页:啊Q闻
🎇收录专栏:《C语言》
🎉道阻且长,行则将至
前言
顺序表是我们后续实现通讯录的一个关键技术,今天我们就来学习一下顺序表。
一.顺序表的概念及结构
顺序表是线性表的一种。什么是线性表呢?
线性表是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表有:顺序表,链表,栈,队列,字符串……
线性表在逻辑结构上是线性结构,在物理结构上不一定是连续的。
线性表在物理上存储时,通常以数组和链式结构的形式存储。
1.对于顺序表,顺序表在逻辑结构上是线性的,物理结构上是连续的。
2.顺序表的底层结构是数组,对数组进行封装,实现常用的增删查改等接口。有点类似与苍蝇馆子包装成五星级酒店。
二.顺序表的分类
顺序表分为静态顺序表和动态顺序表
1.静态顺序表
概念:使用定长数组存储元素
正是因为静态顺序表是使用定长数组存储元素,所以其空间给少了不够用,给多了又会造成空间浪费。
2.动态顺序表
动态顺序表可以自己开辟空间,相较于静态顺序表更加灵活。
显然,动态顺序表更加灵活好用,我们来完成一下动态顺序表的实现。
三.动态顺序表的实现
动态顺序表的实现要有的基本功能是增删查改,所以我们要实现的功能为顺序表的初始化和销毁,以及基本功能增删查改。
我们创建三个文件:
一个头文件SeqList.h用于定义顺序表的结构,顺序表要实现的接口。
一个源文件SeqList.c用于具体实现顺序表里定义的接口。
一个源文件test.c用于测试顺序表。
SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct Seqlist
{
SLDataType* arr;
int size;
int capacity;
}SL;
void Init(SL* ps);//初始化
void SLPlusCapacity(SL* ps);//扩容
void SLPushFront(SL* ps,SLDataType num);//前插
void SLPushBack(SL* ps, SLDataType num);//后插
void SLDeFront(SL* ps);//头删
void SLDeBack(SL* ps);//尾删
void SLInsert(SL* ps, SLDataType num, int pos);//指定位置插入
void SLDelete(SL* ps, int pos);//指定位置删除
int SLFind(SL* ps, SLDataType num);//查找
void SLChange(SL* ps, SLDataType num,int a);//修改
void SLPrint(SL*ps);//打印
void SLDestory(SL* ps);//销毁
详解:
![]()
SeqList.c
#include "SLlist.h"
void Init(SL* ps)
{
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}//对每个值初始化
void SLPlusCapacity(SL* ps)//扩容
{
assert(ps);
if (ps->size == ps->capacity)
{
int newcapacity = 0;
newcapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);//三目表达式
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity *sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail");
exit(1);
}
ps->arr = tmp;
ps->capacity = newcapacity;
}
}
void SLPushFront(SL* ps, SLDataType num)//前插
{
assert(ps);
SLPlusCapacity( ps);
int i = 0;
for (i=ps->size;i>0;i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = num;
ps->size++;
}
void SLPushBack(SL* ps, SLDataType num)//后插
{
assert(ps);
SLPlusCapacity(ps);
ps->arr[ps->size++] = num;
}
void SLDeFront(SL* ps)//前删
{
assert(ps);
assert(ps->size);
int i = 0;
for (i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
void SLDeBack(SL* ps)//后删
{
assert(ps);
assert(ps->size);
ps->size--;
}
void SLInsert(SL* ps, SLDataType num, int pos)//指定位置插入
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLPlusCapacity(ps);
int i = 0;
for (i=ps->size;i>pos;i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = num;
ps->size++;
}
void SLDelete(SL* ps, int pos)//指定位置删除
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
int i = 0;
for (i=pos;i<ps->size-1;i++)
{
ps->arr[i] = ps->arr[i+1];
}
ps->size--;
}
int SLFind(SL* ps, SLDataType num)//查找
{
assert(ps);
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (ps->arr[i] == num)
{
return i;
}
}
return -1;
}
void SLChange(SL* ps, SLDataType num,int a)//修改
{
assert(ps);
ps->arr[a] = num;
}
void SLPrint(SL* ps)//打印
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
void SLDestory(SL* ps)//销毁
{
assert(ps);
if (ps->arr)
{
free(ps->arr);
}
ps->arr=NULL;
ps->size = ps->capacity = 0;
}
详解:
![]()
![]()
![]()
test.c
#include "SLlist.h"
void SLTest01()
{
SL s1;//创建变量
Init(&s1);
SLPushFront(&s1, 1);
SLPushFront(&s1, 2);
SLPushFront(&s1, 3);
SLPushFront(&s1, 4);
//SLPrint(&s1);
SLPushBack(&s1, 5);
SLPushBack(&s1, 6);
SLPrint(&s1);
/*SLDeFront(&s1);
SLPrint(&s1);
SLDeBack(&s1);
SLPrint(&s1);*/
//SLInsert(&s1, 8, 3);
//SLPrint(&s1);
/*SLDelete(&s1, 3);
SLPrint(&s1);*/
/*int ret=SLFind(&s1, 2);
if (ret < 0)
{
printf("要查找的数字不存在\n");
}
else
{
printf("找到了,数字在%d位置\n", ret);
}*/
SLChange(&s1, 9, 1);
SLPrint(&s1);
SLDestory(&s1);
}
int main()
{
SLTest01();
return 0;
}
感谢大家阅读,下篇博客将会分享利用顺序表实现通讯录,如果对你有帮助的话,三连支持一下吧