1、线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
2、顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般分为:
1.静态顺序表(使用定长数组存储元素。)
如下:
#define K 4
typedef int SLDataType;
typedef struct Seqlist {
SLDataType arr[K];//数组长度
size_t size;//有效数据的个数
}Seqlist;
而我们如果想在顺序表中把数据插入删除,那么就必须能用动态的内存来管理它.
2. 动态顺序表:使用动态开辟的数组存储。
typedef int SLDataType;
typedef struct Seqlist
{
SLDataType* a;//指向动态开辟的数组
size_t size;//有效数据个数
size_t capacity;//空间大小
}SL;
3、接下来实现动态顺序表的增删查改的功能
我们把程序分为Seqlist.h Seqlist.c test.c
分别在其中声明函数,实现函数以及测试功能;
先来看Seqlist.h
#pragma once
typedef int SLDataType;
#include<stdio.h>
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);
// 返回下标,没有找打返回-1
int SLFind(SL* ps, SLDataType x);
// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);
// 删除pos位置的值
void SLErase(SL* ps, int pos);
void SLModify(SL* ps, int pos, SLDataType x);
接下里我们把这些函数一个一个实现:
void SLInit(SL* ps)
{
ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
if (ps-> == NULL)
{
perror("malloc failed");
exit(-1);
}
ps->size=0;
ps->capacity = 4;
}//初始化
void SLDestroy(SL* ps)
{
assert(ps->a);
free(ps);
ps ->a= NULL;
ps->size = 0;
ps->capacity = 0;
}
```//销毁
```c
void SLPrint(SL* ps)
{
assert(ps);
int i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
}//打印
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size > ps->capacity)//满了扩容
{
SLDataType *tmp=(SLDataType*)realloc(ps->a, sizeof(SLDataType) * 2 * ps->capacity);
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;//尾插入
}
void SLPopBack(SL* ps)//尾删除
{
assert(ps);
assert(ps->size > 0);
ps->a[ps->size - 1] = 0;
ps->size--;
}
void SLPushFront(SL* ps, SLDataType x)//头插入
{
assert(ps);
SLCheckCapacity(ps);
int end = ps->size;
while (end--)
{
ps->a[end+1] = ps->a[end];
}
ps->a[0] = x;
ps->size++;
}
void SLPopFront(SL* ps)//头删除
{
assert(ps);
int begin = 1;
while (begin<ps->size)
{
ps->a[begin-1] = ps->a[begin];
begin++;
}
ps->size--;
}
// 在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
// 删除pos位置的值
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
int begin = pos + 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;
}
void SLModify(SL* ps, int pos, SLDataType x)//修改pos处的值
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
最后关于测试的内容大家可以自己尝试这些功能的实现
——————————————————————————
以上就是顺序表的功能实现,如有错误欢迎指正,谢谢大家!