目录
顺序表的简单介绍
顺序表的底层结构是数组,对数组的封装,实现了常用的增删查改等操作,并且顺序表物理结构和逻辑结构都是连续的。
静态顺序表
typtdef int SLdatatype;
#define n 3
typedef srtuct Seqlist
{
SLdatatype a[n];
int size;
};
动态数据表
ypedef struct Seqlist
{
int* arr;
int size;//有效个数
int capacity;//空间个数
}SL;
顺序表的初始化
void SLinit(SL* ps)
{
ps->arr = NULL;
ps->capacity =ps->size = 0;
//上面下面都可以实现
//ps->arr=malloc(10);
//ps->capacity =ps->size=10;
}
顺序表的销毁
void SLdestory(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
顺序表的插入
插入之前都需要判断空间,如果空间不够则需要申请空间
void SLcheckcapacity(SL*ps)
{
if (ps->capacity == ps->size)
{
//申请空间
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLdatatype* p = (SLdatatype*)realloc(ps->arr, newcapacity * sizeof(SLdatatype));
if (p == NULL)
{
perror("realloc fail!");
return 1;
}
else
{
ps->arr = p;
ps->capacity = newcapacity;
}
}
}
如果空间不够,要申请多少空间?
一般申请的是2倍或者3倍。
头部插入
void pushfront(SL* ps, SLdatatype x)
{
assert(ps);
//插入数据之前先看空间够不够
SLcheckcapacity(ps);
for (int i = ps->size; i>0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
尾部插入
void pushback(SL* ps, SLdatatype x)
{
assert(ps);
//插入数据之前先看空间够不够
SLcheckcapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
顺序表的删除
头部删除
void POPfront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i<ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
尾部删除
void POPback(SL* ps)
{
assert(ps);
assert(ps->size);
//ps->arr[ps->size - 1] = -1;
ps->size--;
}
总代码
头文件
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLdatatype;
//动态数据表结构
typedef struct Seqlist
{
int* arr;
int size;//有效数据个数
int capacity;//当前空间大小
}SL;
//初始化
void SLinit(SL* ps);
void pushback(SL* ps, SLdatatype x);
void pushfront(SL* ps, SLdatatype x);
void SLprint();
void POPback(SL* ps);
void POPfront(SL* ps);
//顺序表的销毁
void SLdestory(SL* ps);
源文件
#include "test.h"
//初始化
void SLinit(SL* ps)
{
ps->arr = NULL;
ps->capacity =ps->size = 0;
//ps->arr=malloc(10);
//ps->capacity =ps->size=10;
}
//销毁
void SLdestory(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
//封装申请空间函数
void SLcheckcapacity(SL*ps)
{
if (ps->capacity == ps->size)
{
//申请空间
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLdatatype* p = (SLdatatype*)realloc(ps->arr, newcapacity * sizeof(SLdatatype));
if (p == NULL)
{
perror("realloc fail!");
return 1;
}
else
{
ps->arr = p;
ps->capacity = newcapacity;
}
}
}
//尾部插入
void pushback(SL* ps, SLdatatype x)
{
assert(ps);
//插入数据之前先看空间够不够
SLcheckcapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
//头部插入
void pushfront(SL* ps, SLdatatype x)
{
assert(ps);
//插入数据之前先看空间够不够
SLcheckcapacity(ps);
for (int i = ps->size; i>0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
//不需要修改可以直接传值打印
void SLprint(SL s)
{
for (int i = 0; i < s.size; i++)
{
printf("%d ", s.arr[i]);
}
printf("\n");
}
//尾部删除
void POPback(SL* ps)
{
assert(ps);
assert(ps->size);
//ps->arr[ps->size - 1] = -1;
ps->size--;
}
//头部删除
void POPfront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i<ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
测试代码
#include "test.h"
void test01()
{
SL sl;
SLinit(&sl);
//增删查改
pushback(&sl, 1);
pushback(&sl, 2);
pushback(&sl, 3);
pushback(&sl, 4);
pushback(&sl, 5);
SLprint(sl);
pushfront(&sl, 7);
POPfront(&sl);
SLprint(sl);//1 2 3 4 5
POPfront(&sl);
SLprint(sl);//2 3 4 5
POPfront(&sl);
SLprint(sl);//3 4 5
POPback(&sl);
SLprint(sl);
SLdestory(&sl);
}
int main()
{
test01();
return 0;
}