概念及结构
顺序表是一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
1.静态顺序表,使用定长数组存储元素。
2.动态顺序表,使用动态开辟的数组存储。
接口实现:
静态顺序表只适用于确定知道需要多少数据的场景。静态顺序表的定长数组导致N(数组长度)定大了,空间开多了浪费,开小了不够用。所以现实基本都是使用动态的顺序表,根据需要动态开辟空间大小,我们在下面以工程的方式实现。
SeqList.h :函数声明
#pragma once
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
typedef int SLDataType;
//顺序表的动态存储
typedef struct SeqList
{
SLDataType* a; //指向动态开辟的数组
int size; //有效数据个数
int capacity; //空间容量的大小
}SL; //重命名
//顺序表初始化
void SLInit(SL*ps1); //传递指针进行初始化
//顺序表销毁
//void SLDestroy(SL* ps1); //传递地址为空
//
//
//顺序表尾插
void SLPushBack(SL* ps1, SLDataType x);
//顺序表头插
void SLPushFront(SL* ps1, SLDataType x);
//顺序表尾删
void SLPopBack(SL* ps1);
//
//顺序表头删
void SLPopFront(SL* ps1);
//空间扩容
void SLChechCapacity(SL* ps1);
//顺序表在pos下标位置插入x
void SLInsert(SL* ps1, int pos, SLDataType x);
//顺序表删除pos小标位置的值
void SLErase(SL* ps1, int pos);
//顺序表查找
int SLFind(SL* ps1, SLDataType x);
//顺序表打印
void SLPrint(SL* ps1);
SeaList.c :函数定义
#pragma once
#include"SeqList.h"
//顺序表初始化
void SLInit(SL* ps1)
{
assert(ps1);
ps1->a = NULL;
ps1->size = 0;
ps1->capacity = 0;
}
//顺序表销毁
void SLDestroy(SL* ps1)
{
if (ps1->a != NULL)
{
free(ps1->a);
ps1->a = NULL;
ps1->size = 0;
ps1->capacity = 0;
}
}
//空间扩容
void SLChechCapacity(SL* ps1)
{
if (ps1->size == ps1->capacity) //realloc
{
//判定初始空间是否为NULL。
int newCapacity = ps1->capacity == 0 ? 4 : ps1->capacity * 2;
//对空间进行扩容
SLDataType* tmp = (SLDataType*)realloc(ps1->a, sizeof(SLDataType) * newCapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps1->a = tmp;
ps1->capacity = newCapacity;
}
}
//顺序表尾插
void SLPushBack(SL* ps1, SLDataType x)
{
SLChechCapacity(ps1);
ps1->a[ps1->size] = x;
ps1->size++;
}
//打印
void SLPrint(SL* psl)
{
for (int i = 0; i <psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
//顺序表头插
void SLPushFront(SL* ps1, SLDataType x)
{
SLChechCapacity(ps1);
int end = ps1->size - 1;
while(end >= 0)
{
ps1->a[end + 1] = ps1->a[end];
--end;
}
ps1->a[0] = x;
ps1->size++;
}
//顺序表尾删
void SLPopBack(SL* ps1)
{
assert(ps1->size);
//ps1->a[ps1->size-1]=-1;
ps1->size--;
}
//顺序表头删
void SLPopFront(SL* ps1)
{
assert(ps1->size);
int begin = 1;
while (begin < ps1 -> size)
{
//从后往前覆盖
ps1->a[begin - 1] = ps1->a[begin];
++begin;
}
ps1->size--;
}
//顺序表在pos下标位置插入x
//size是数组的个数,是数组最后一个的下一个
//数组从开始,本质是指针,逻辑自恰,偏移量是*(a+0)
void SLInsert(SL* ps1, int pos, SLDataType x)
{
assert(ps1);
assert(pos >= 0 && pos <= ps1->size);//pos可以等于size,相当于尾插
SLChechCapacity(ps1); //扩容
//挪动数据
int end = ps1->size - 1;
while (end >= pos)
{
ps1->a[end + 1] = ps1->a[end];
--end;
}
ps1->a[pos] = x;
ps1->size++;
}
//顺序表删除pos位置的值
void SLErase(SL* ps1, int pos)
{
assert(ps1);
assert(pos >= 0 && pos < ps1->size);
int begin = pos + 1;
while (begin < ps1->size)
{
ps1->a[begin - 1] = ps1->a[begin];
++begin;
}
ps1->size--;
}
//顺序表查找
int SLFind(SL* ps1, SLDataType x)
{
assert(ps1);
for (int i = 0; i < ps1->size; i++)
{
if (ps1->a[i] == x)
{
return i;
}
}
return -1;
}
test.c:测试
#include<stdio.h>
#include"SeqList.h"
void TestSL1()
{
SL s1;
//顺序表初始化
SLInit(&s1);
//顺序表尾插
SLPushBack(&s1,1);
SLPushBack(&s1, 2);
SLPushBack(&s1, 3);
SLPushBack(&s1, 4);
SLPushBack(&s1, 5);
SLPushBack(&s1, 6);
SLPushBack(&s1, 7);
SLPushBack(&s1, 8);
SLPushBack(&s1, 9);
SLPrint(&s1);
//顺序表头插
SLPushFront(&s1, 10);
SLPushFront(&s1, 9);
SLPushFront(&s1, 8);
SLPushFront(&s1, 7);
SLPushFront(&s1, 6);
SLPushFront(&s1, 11);
SLPrint(&s1);
//顺序表尾删
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPrint(&s1);
//顺序表头删
SLPopFront(&s1);
SLPopFront(&s1);
SLPopFront(&s1);
SLPrint(&s1);
//顺序表在pos位置插入x
//pos是下标
SLInsert(&s1, 2, 22);
/*SLInsert(&s1, 10, 22);*/
SLPrint(&s1);
//顺序表删除pos位置的值
SLErase(&s1, 3);
SLPrint(&s1);
//顺序表查找 下标
int ret = SLFind(&s1, 9);
printf("%d", ret);
//顺序表销毁
SLDestroy(&s1);
}
int main()
{
TestSL1();
return 0;
}
在这里扩展一个问题:
//越界一定会报错么?
int a[10];
越界读通常不会报错
printf("%d\n", a[10]);
printf("%d\n", a[11]);
越界写可能会报错
a[15] = 1;