线性表是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、栈、队列、字符串……
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常是以数组和链式结构的形式存储。
相同特性分为:
概念:顺序表是一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|
顺序表:逻辑结构:线性;物理结构:线性 。
顺序表和数组的关系? 顺序表的底层结构是数组,顺序表使用数组来实现的。这段话是啥意思?让我们通过画图来分析一下。

数组就好比如是苍蝇馆子,顺序表就比如是米其林餐厅,苍蝇馆子里面有的,米其林餐厅里面也有,但米其林餐厅有的,苍蝇馆子里面可没有哦。在顺序表里面,我们可以对数组进行增加数据,删除数据,修改数据,查找数据……
可以概括为增删改查四个功能:
静态顺序表的创建:
typedef int SLDataType;
#define N 7
typedef struct SeqList {
SLDataType a[N]; //定长数组
int size; //有效数据个数
}SL;由于静态顺序表是使用定长数组存储元素,所以可能会造成空间上的浪费。
动态顺序表的创建:
//动态顺序表--按需申请
typedef int SLDataType;
typedef struct SeqList {
SLDataType* arr;
int size; //有效数据个数
int capacity; //空间容量
}SL;创建一个动态顺序表,我们首先要开辟一片连续的空间用来存储顺序表中的数据,并且要保证这片空间的大小可以调节,那么我们可以创建一个指针来存储,因此,我们还要创建变量来存储顺序表的大小和这片空间的空间容量
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;//存储数据
int size;//有效数据个数
int capcity;//空间大小
}SL;//初始化
void SLInt(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capcity = 0;
}首先,我们要先创建一个函数来检查空间容量,当空间容量不够的时候,可以增加容量:
void SLcheckcapcity(SL* ps)
{
if (ps->size == ps->capcity)
{
int newcapcity = ps->capcity == 0 ? 4 : 2 * ps->capcity;
ps->capcity = newcapcity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, ps->capcity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
}
}检查空间容量,当空间容量足够的时候,直接在尾部增加数据,如果容量不够,需要扩容,添加完之后,size++。
//尾插法
void SLpushBack(SL* ps, SLDataType num)
{
//空间不够,需要扩容
SLcheckcapcity(ps);
//空间足够
ps->arr[ps->size++] = num;
}检查空间容量,当空间容量足够的时候,将所有元素向后移动一位,再把要添加的数据添加到开头,添加完之后,size++。
//头插法
void SLpushFront(SL* ps, SLDataType num)
{
//空间不够
SLcheckcapcity(ps);
//空间足够
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = num;
ps->size++;
}首先要检查空间容量,在顺序表的指定位置前增加数据,需要将从该位置开始所有元素向后移动一位,将元素添加到指定位置,添加完之后,size++。
//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType num)
{
//判断是否为空
assert(ps->arr&&pos>=0&&pos<ps->size);
//空间不够,扩容
SLcheckcapcity(ps);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = num;
ps->size++;
}在指定位置后添加元素:从该位置后一位元素开始,所有元素向后移动一位,再将指定元素添加到指定位置,添加完之后,size++。
//在指定位置之后插入数据
void SLInsertAfter(SL* ps, int pos, SLDataType num)
{
//判段是否为空
assert(ps->arr && pos >= 0 && pos < ps->size);
//空间够不够,扩容
SLcheckcapcity(ps);
for (int i = ps->size; i > pos + 1; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos + 1] = num;
ps->size++;
}尾部删除在顺序表中式最容易实现的一种删除方式,我们仅仅需要将size自减就可以了,但是在删除之前,我们需要先判断此顺序表中是否存在元素
//尾删
void SLpopback(SL* ps)
{
//顺序表是否为空
assert(ps->arr && ps->size);
ps->size--;
}从下标为1的位置开始的所有元素向前移动一位,然后size--。
//头删
void SLpopFront(SL* ps)
{
//顺序表是否为空
assert(ps->arr && ps->size);
for (int i = 0; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}该位置以后的所有元素都向前移动一位,移动完之后,size--。
//删除pos位置上的数据
void SLdelete(SL* ps, int pos)
{
//顺序表是否为空
assert(ps->arr && ps->size);
for (int i = pos; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}在代码中,如果我们自己主动输入位置是不是显得有那么一点多余,所以我们可以让代码自己查找数据的坐标,然后我们通过下标就可以完成相应的操作,接下来让我们看看如何得到我们想要的数据的位置:
//查找位置
int SLfindmember(SL* ps, SLDataType num)
{
//判断是否为空
assert(ps->arr);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == num)
{
return i;
}
}
return -1;
}//修改指定位置的数据
void SLChange(SL* ps, int pos, SLDataType num)
{
//判段顺序表是否为空以及位置是否在限度内
assert(ps->arr && pos >= 0 && pos <= ps->size);
ps->arr[pos] = num;
}//顺序表打印
void SLprint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
}//结束时释放空间
void SLfree(SL* ps)
{
ps->arr = NULL;
ps->capcity = ps->size = 0;
}SeqList.h(头文件、函数声明):
#pragma once
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;//存储数据
int size;//有效数据个数
int capcity;//空间大小
}SL;
//结束时释放空间
void SLfree(SL* ps);
//打印
void SLprint(SL* ps);
//初始化顺序表
void SLInt(SL* ps);
//尾插法
void SLpushBack(SL* ps, SLDataType num);
//头插法
void SLpushFront(SL* ps, SLDataType num);
//尾删
void SLpopback(SL* ps);
//头删
void SLpopFront(SL* ps);
//查找位置
int SLfindmember(SL* ps, SLDataType num);
//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType num);
//在指定位置之后插入数据
void SLInsertAfter(SL* ps, int pos, SLDataType num);
//修改指定位置的数据
void SLChange(SL* ps, int pos, SLDataType num);
//删除pos位置上的数据
void SLdelete(SL* ps, int pos);SeqList.c(具体操作代码):
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
//结束时释放空间
void SLfree(SL* ps)
{
ps->arr = NULL;
ps->capcity = ps->size = 0;
}
//顺序表打印
void SLprint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
}
//初始化
void SLInt(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capcity = 0;
}
void SLcheckcapcity(SL* ps)
{
if (ps->size == ps->capcity)
{
int newcapcity = ps->capcity == 0 ? 4 : 2 * ps->capcity;
ps->capcity = newcapcity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, ps->capcity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
}
}
//尾插法
void SLpushBack(SL* ps, SLDataType num)
{
//空间不够,需要扩容
SLcheckcapcity(ps);
//空间足够
ps->arr[ps->size++] = num;
}
//头插法
void SLpushFront(SL* ps, SLDataType num)
{
//空间不够
SLcheckcapcity(ps);
//空间足够
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = num;
ps->size++;
}
//尾删
void SLpopback(SL* ps)
{
//顺序表是否为空
assert(ps->arr && ps->size);
ps->size--;
}
//头删
void SLpopFront(SL* ps)
{
//顺序表是否为空
assert(ps->arr && ps->size);
for (int i = 0; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//查找位置
int SLfindmember(SL* ps, SLDataType num)
{
//判断是否为空
assert(ps->arr);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == num)
{
return i;
}
}
return -1;
}
//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType num)
{
//判断是否为空
assert(ps->arr&&pos>=0&&pos<ps->size);
//空间不够,扩容
SLcheckcapcity(ps);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = num;
ps->size++;
}
//在指定位置之后插入数据
void SLInsertAfter(SL* ps, int pos, SLDataType num)
{
//判段是否为空
assert(ps->arr && pos >= 0 && pos < ps->size);
//空间够不够,扩容
SLcheckcapcity(ps);
for (int i = ps->size; i > pos + 1; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos + 1] = num;
ps->size++;
}
//删除pos位置上的数据
void SLdelete(SL* ps, int pos)
{
//顺序表是否为空
assert(ps->arr && ps->size);
for (int i = pos; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//修改指定位置的数据
void SLChange(SL* ps, int pos, SLDataType num)
{
//判段顺序表是否为空以及位置是否在限度内
assert(ps->arr && pos >= 0 && pos <= ps->size);
ps->arr[pos] = num;
}test.c(测试代码):
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
int main()
{
SL sl;
//初始化
SLInt(&sl);
SLpushBack(&sl, 1);
SLpushBack(&sl, 2);
SLpushBack(&sl, 3);
SLpushBack(&sl, 4);
SLpushBack(&sl, 5);
SLpushFront(&sl, 6);
int pos = SLfindmember(&sl, 2);
//SLInsertAfter(&sl, pos, 0);
SLdelete(&sl, pos);
SLChange(&sl, pos, 8);
SLprint(&sl);
SLfree(&sl);
return 0;
}