1.线性表的介绍
1.1线性表
线性表是数据结构中的一个基本概念,它是一种线性序列的数据集合,其中元素之间存在一对一的关系。换句话说,除了第一个和最后一个元素之外,每个元素都有一个前驱和一个后继。线性表可以用顺序存储或链式存储的方式实现。
该图为一个学校的学生健康情况统计,就是一个线性表。
1.2顺序存储
顺序存顺序存储的线性表,也称为顺序表,是通过数组实现的。在这种存储方式中,元素按照它们在表中的逻辑顺序连续地存储在内存中。这意味着可以通过下标直接访问任何元素,具有随机访问的特点。顺序表的主要优点是访问速度快,但缺点是在插入和删除操作时可能需要移动大量的元素。
线性表的操作
线性表常见的操作包括:
1.初始化:创建一个空的线性表。
2.插入:在线性表的指定位置添加一个新的元素。
3.删除:从线性表中移除指定位置的元素。
线性表的应用非常广泛,例如在计算机科学中,它们常用于实现栈、队列、哈希表等更复杂的数据结构。理解线性表的概念和操作对于学习数据结构和算法至关重要。
2.代码展示
2.1静态顺序表
//静态顺序表
#define N 100
typedef int SLDataType;
struct SeqList
{
SLDataType a[100];
SLDataType size;
};
2.2动态顺序表
//动态顺序表
#define N 100
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int capacity;
int size;
}SL;
2.3顺序表的初始化和销毁
//.h头文件
void SLInit(SL* ps);
void SLDestory(SL* ps);
//初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
//销毁
void SLDestory(SL* ps)
{
free(ps->arr);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
2.4打印顺序表
void SLPrintf(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
2.5顺序表的头部插入和尾部插入
//检查并扩容
void SLCheckCapacity(SL*ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity = 0 ? 4 : 2 * ps->capacity;
//申请空间
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
//扩容成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
//断言,粗暴地断言方式
assert(ps != NULL);
//判断是否扩容
SLCheckCapacity(ps);
//空间足够,直接插入
ps->arr[ps->size++] = x;
}
//头部插入
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
//判断是否扩容
SLCheckCapacity(ps);
//空间够之后,旧数字往后挪
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
2.6顺序表的头部删除和尾部删除
//顺序表尾部删除
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
//顺序表不为空
ps->arr[ps->size--];
}
顺序表头部删除
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i <ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
2.7指定位置插入删除数据
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i=ps->size;i>pos;i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
//指定位置删除数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//pos以后的数据挪动一位
for (int i = pos; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
3.全代码展示
头文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//静态顺序表
//#define N 100
//typedef int SLDataType;
//struct SeqList
//{
// SLDataType a[100];
// SLDataType size;
//};
//动态顺序表
#define N 100
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int capacity;
int size;
}SL;
//顺序表的初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);
//打印顺序表
void SLPrintf(SL* ps);//保持接口一致性
//顺序表的头部插入和尾部插入
void SLPushBack(SL* ps ,SLDataType x);
void SLPushFront(SL* ps ,SLDataType x);
//顺序表的头部删除和尾部删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
//指定位置删除数据
void SLErase(SL* ps, int pos);
源代码
#include "SeqList.h"
//初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
//检查并扩容
void SLCheckCapacity(SL*ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity = 0 ? 4 : 2 * ps->capacity;
//申请空间
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
//扩容成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
//断言,粗暴地断言方式
assert(ps != NULL);
//判断是否扩容
SLCheckCapacity(ps);
//空间足够,直接插入
ps->arr[ps->size++] = x;
}
//头部插入
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
//判断是否扩容
SLCheckCapacity(ps);
//空间够之后,旧数字往后挪
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
//顺序表尾部删除
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
//顺序表不为空
ps->arr[ps->size--];
}
顺序表头部删除
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i <ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//打印
void SLPrintf(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i=ps->size;i>pos;i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
//指定位置删除数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//pos以后的数据挪动一位
for (int i = pos; i < ps->size; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//销毁
void SLDestory(SL* ps)
{
free(ps->arr);
ps->arr = ps->capacity = 0;
}
测试代码
#include "SeqList.h"
void slTest01()
{
SL sl;
SLInit(&sl);
//测试尾插
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
SLPrintf(&sl);
//测试头插
SLPushFront(&sl, 5);
SLPushFront(&sl, 6);
SLPushFront(&sl, 7);
SLPrintf(&sl);
//测试尾删
SLPopBack(&sl);
SLPopBack(&sl);
SLPopBack(&sl);
SLPrintf(&sl);
SLPopFront(&sl);
SLPrintf(&sl);
//测试指定位置插入
SLInsert(&sl, 0, 100);
SLPrintf(&sl);
//测试指定位置删除
SLErase(&sl, 1);
SLErase(&sl, sl.size-1);
SLPrintf(&sl);
}
int main()
{
slTest01();
return 0;
}