1.线性表(linear list)
线性表是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串……
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
本节围绕顺序表进行代码实现
2.顺序表
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
①静态顺序表:使用定长数组存储元素
②动态顺序表:使用动态开辟的数组存储
3.接口实现
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。
<SeqList.h>
#pragma once
//顺序表数据类型定义
typedef int SLDataType;
//动态存储
typedef struct SeqList
{
SLDataType* a; // 指向动态开辟的数组
int size; // 有效数据个数
int capacity; // 空间容量大小
}SL;
void SLInit(SL* ps); // 初始化
void SLDestroy(SL* ps); // 销毁
void SLPushBack(SL* psl, SLDataType x); // 尾插入
void SLPushFront(SL* psl, SLDataType x); // 头插入
void SLPopBack(SL* psl); // 尾删除
void SLPopFront(SL* psl); // 头删除
void SLPrint(SL* psl); //顺序表的打印
void SLCheckCapacity(SL* psl); // 扩容前进行空间查询,看是否够用
void SLInsert(SL* psl, int pos, SLDataType x); // 任意位置根据下标插入
void SLErase(SL* psl, int pos); // 任意位置根据下标删除
int SLFind(SL* psl, SLDataType x);// 查找,找到返回下标,没有找到返回-1
4.函数实现
<SeqList.c>
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"SeqList.h"
#include <assert.h>
void SLInit(SL* psl) // 初始化
{
assert(psl);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
void SLDestroy(SL* psl) // 释放
{
if (psl->a != NULL)
{
free(psl->a);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
}
void SLPrint(SL* psl) // 打印
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
void SLCheckCapacity(SL* psl) // 检查空间
{
assert(psl);
if (psl->size == psl->capacity)
{
int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;// 检测capacity是否为0;是则赋值为4,不是则扩容至二倍
SLDataType* tmp = realloc(psl->a, sizeof(SLDataType) * newCapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
psl->a = tmp;
psl->capacity = newCapacity;
}
}
void SLPushBack(SL* psl, SLDataType x) // 尾插入
{
assert(psl);
SLCheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
void SLPushFront(SL* psl, SLDataType x) // 头插入
{
assert(psl);
SLCheckCapacity(psl);
// 挪动数据
int end = psl->size - 1;
while (end >= 0)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[0] = x;
psl->size++;
}
void SLPopBack(SL* psl) // 尾删除
{
assert(psl);
// 空
if (psl->size == 0)
{
return;
}
//OR 暴力检查,断言
//assert(psl->size > 0);
psl->size--;
}
void SLPopFront(SL* psl) // 头删除
{
assert(psl);
//OR 暴力检查,断言
assert(psl->size > 0);
int begin = 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
//pos是下标,也就是插入位置,x是插入的数值
void SLInsert(SL* psl, int pos, SLDataType x)// 任意位置根据下标插入
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
SLCheckCapacity(psl);
// 挪动数据
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
--end;
}
psl->a[pos] = x;
psl->size++;
}
void SLErase(SL* psl, int pos)// 任意位置根据下标删除
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
int begin = pos + 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
int SLFind(SL* psl, SLDataType x)// 找到返回下标,没有找到返回-1
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}
5.调试
<Test.c>
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"SeqList.h"
void menu()
{
printf("*****************************\n");
printf("****1.尾插数据 2.尾删数据****\n");
printf("****3.头插数据 4.头删数据****\n");
printf("****5.打印数据 0.退出程序****\n");
printf("*****************************\n");
}
int main()
{
SL s;
SLInit(&s);
int option = 0;
do
{
menu();
printf("请输入你的选择>");
scanf("%d", &option);
if (option == 1)
{
printf("请输入你要尾插的数据个数和数据:>\n");
int n = 0;//个数
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int x = 0;//数据
scanf("%d", &x);
SLPushBack(&s, x);
}
printf("\n");
}
else if(option == 2)
{
SLPopBack(&s);
printf("删除成功\n");
printf("\n");
}
else if (option == 3)
{
printf("请输入你要头插的数据,以-1结束:>\n");
int x = 0;
scanf("%d", &x);
while (x != -1)
{
SLPushFront(&s, x);
scanf("%d", &x);
}
printf("\n");
}
else if (option == 4)
{
SLPopFront(&s);
printf("删除成功\n");
printf("\n");
}
else if (option == 5)
{
SLPrint(&s);
printf("打印成功\n");
printf("\n");
}
else if (option == 0)
{
printf("退出程序\n");
break;
}
else
{
printf("无此操作\n");
printf("\n");
}
} while (option != 0);
SLDestroy(&s);
return 0;
}
6.顺序表的问题
①尾部插入效率还不错,头部或这中间插入删除,需要挪动数据,效率低下
②满了以后只能扩容,扩容是有一定的消耗的,扩容一般是存在一定的空间浪费(假设100为满,需要放入110个数据,扩容到200,浪费了90个;一次扩得多可能浪费多,扩的少可能会频繁扩容)