目录
一、线性表
- 什么是线性表,从名字上你就能感觉到,是具有像线一样的性质的表。在广场上,有很多人分散在各处,当中有些是小朋友,可也有很多大人,甚至还有不少宠物,这些小朋友的数据对于整个广场人群来说,不能算是线性表的结构。但像现在说的这样,一个班级的小朋友,一个跟着一个排着队,有一个打头,有一个收尾,当中的小朋友每一个都知道TA前面一个是谁,TA后面一个是谁,这样就如同一根线把它们串联起来了, 就可以称之为线性表。————以上的文段出自于《大话数据结构》这本书,我觉得讲的挺生动形象的,于是就搬过来与大家分享。
- 线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
以上便是对线性表的介绍
二、顺序表
1、顺序表的定义
顺序表(线性表的顺序存储结构)是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储(一维数组)。在数组上完成数据的增删查改。
2、顺序表中不同的存储结构
- 静态存储:使用定长数组存储元素,这种存储结构只适用于知道存储多少数据。
缺点:由于不知道具体存储的数据,无法及时地修改N的值。
2.动态存储:使用动态开辟的数组存储,根据需要动态的分配空间大小。
3、顺序表动态存储的一些功能实现
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
// 打印表
void SeqListPrint(SeqList* ps);
//头插头删 尾插尾删
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
//检查是否需要增容
void SLCheckCapacity(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
// 顺序表修改值
void SeqListModify(SeqList* ps, int pos, SLDateType x);
初始化顺序表
//初始化顺序表表
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a =(SLDateType*) malloc(sizeof(SLDateType) * 4);
if (ps->a == NULL)
{
perror("malloc failed");
exit(-1);//让程序以异常的方式退出(终止程序)
//return -1; //以异常的方式返回上一级函数并继续执行余下程序
}
ps->size = 0;
ps->capacity = 4;
}
释放所开辟的空间
//释放所开辟的空间
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
打印顺序表
//打印
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
检查是否需要增容
//检查是否需要增容
void SLCheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDateType* tmp = (SLDateType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDateType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
头插头删,尾插尾删
头插
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
//SLCheckCapacity(ps);
挪动数据
//int end = ps->size - 1;
//while (end >= 0)
//{
// ps->a[end + 1] = ps->a[end];
// --end;
//}
//ps->a[0] = x;
//ps->size++;
SeqListInsert(ps, 0, x);
}
头删
//头删
void SeqListPopFront(SeqList* ps)
{
assert(ps);
/*assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;*/
SeqListErase(ps, 0);
}
尾插
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
//将原空间的值放入新空间
/*ps->a[ps->size] = x;
ps->size++;*/
SeqListInsert(ps, ps->size, x);
}
尾删
//尾删
void SeqListPopBack(SeqList* ps)
{
assert(ps);
//两种检查的目的:为了防止增加或者删除后会造成越界的后果
//温柔的检查
/*if (ps->size == 0)
{
return;
}*/
//暴力的检查
//assert(ps->size > 0);//如果是真的就继续执行程序,如果是假的就终止程序
//ps->a[ps->size - 1] = 0;
//ps->size--;
SeqListErase(ps, ps->size);
}
顺序表查找
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return i;
}
return - 1;
}
顺序表在pos位置插入x
插入算法的思路:
- 如果插入位置不合理,抛出异常;
- 如果线性表长度大于等于数组长度,则抛出异常或者动态增加容量;
- 从最后一个元素开始向前遍历到第i个位置,分别将它们都向后移动一个位置;
- 将要插入的元素填入位置i处;
- 表长加1。
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType 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位置的值
删除算法的思路:
- 如果删除位置不合理,抛出异常;
- 取出删除元素;
- 从删除元素位置开始遍历到最后一个元素位置,分别将它们都向前移动一个位置;
- 表长减1。
// 顺序表删除pos位置的值
void SeqListErase(SeqList* 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 SeqListModify(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
4、线性表顺序存储结构的优缺点
优点
- 无须为表示表中元素之间的逻辑关系而增加额外的存储空间可以快速地存取表中任一位置的元素;
- 可以快速地存取表中任意位置的元素;
缺点
- 插入和删除操作需要移动大量元素 ;
- 当线性表长度变化较大时,难以确定存储空间的容量 ;
- 造成存储空间的“碎片";
总代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
//初始化顺序表表
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a =(SLDateType*) malloc(sizeof(SLDateType) * 4);
if (ps->a == NULL)
{
perror("malloc failed");
exit(-1);//让程序以异常的方式退出(终止程序)
//return -1; //以异常的方式返回上一级函数并继续执行余下程序
}
ps->size = 0;
ps->capacity = 4;
}
//释放所开辟的空间
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
//打印
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
//检查是否需要增容
void SLCheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SLDateType* tmp = (SLDateType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDateType)));
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
//将原空间的值放入新空间
/*ps->a[ps->size] = x;
ps->size++;*/
SeqListInsert(ps, ps->size, x);
}
//尾删
void SeqListPopBack(SeqList* ps)
{
assert(ps);
//两种检查的目的:为了防止增加或者删除后会造成越界的后果
//温柔的检查
/*if (ps->size == 0)
{
return;
}*/
//暴力的检查
//assert(ps->size > 0);//如果是真的就继续执行程序,如果是假的就终止程序
//ps->a[ps->size - 1] = 0;
//ps->size--;
SeqListErase(ps, ps->size);
}
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
//SLCheckCapacity(ps);
挪动数据
//int end = ps->size - 1;
//while (end >= 0)
//{
// ps->a[end + 1] = ps->a[end];
// --end;
//}
//ps->a[0] = x;
//ps->size++;
SeqListInsert(ps, 0, x);
}
//头删
void SeqListPopFront(SeqList* ps)
{
assert(ps);
/*assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
++begin;
}
ps->size--;*/
SeqListErase(ps, 0);
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return i;
}
return - 1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType 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 SeqListErase(SeqList* 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 SeqListModify(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}