1.什么是线性表

2.什么是顺序表

3.静态顺序表结构的定义

4.静态顺序表的函数接口实现

5.静态顺序表的问题及思考

2.什么是顺序表

1.静态顺序表：使用定长数组存储元素。

2.动态顺序表：使用动态开辟的数组存储元素。

3.静态顺序表结构的定义

``````#define N 100
typedef int SLDataType;

//静态顺序表
typedef struct SeqList
{
SLDataType a[N];//定长数组
int size;//记录存储多少个有效数据
}SeqList;``````

4.静态顺序表的函数接口实现

``````//初始化顺序表
void SLInit(SeqList* ps);
//静态顺序表的尾插
void SLPushBack(SeqList* ps,SLDataType data);
//静态顺序表的尾删
void SLPopBack(SeqList* ps);
//静态顺序表的头插
void SLPushFront(SeqList* ps,SLDataType data);
//静态顺序表的头删
void SLPopFront(SeqList* ps);
//pos位置插入数据
void SLInsert(SeqList* ps, int pos, SLDataType data);
//pos位置删除数据
void SLErase(SeqList* ps,int pos);
//查找数据
int SLFind(SeqList* ps, SLDataType data, int begin);
//判断数组是否已满
bool IsFull(SeqList* ps);
//打印存储的数据
void SLPrint(SeqList* ps);``````

``````void SLInit(SeqList* ps)
{
assert(ps);
ps->size = 0;
}``````
``````bool IsFull(SeqList* ps)
{
assert(ps);
if (ps->size == N)
{
return true;
}
else
{
return false;
}
}``````
``````void SLPushBack(SeqList* ps,SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//插入数据
ps->a[ps->size] = data;
ps->size++;
}``````
``````void SLPopBack(SeqList* ps)
{
assert(ps);
ps->size--;
}``````
``````void SLPushFront(SeqList* ps, SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//挪动数据
for (int i = ps->size - 1; i >= 0; i--)
{
ps->a[i + 1] = ps->a[i];
}
//插入数据
ps->a[0] = data;
ps->size++;
}``````
``````void SLPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
//挪动数据
for (int i = 0; i < ps->size-1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}``````
``````void SLInsert(SeqList* ps, int pos, SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//挪动数据
for (int i = ps->size-1; i >= pos; i--)
{
ps->a[i + 1] = ps->a[i];
}
//插入数据
ps->a[pos] = data;
ps->size++;
}``````
``````void SLErase(SeqList* ps, int pos)
{
assert(ps);
assert(ps->size > 0);
//挪动数据
for (int i = pos; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}``````
``````int SLFind(SeqList* ps, SLDataType data, int begin)
{
assert(ps);
assert(begin < ps->size);
for (int i = begin; i < ps->size; i++)
{
if (ps->a[i] == data)
{
return i;
}
}
return -1;
}``````
``````void SLPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("n");
}``````

5.静态顺序表的问题及思考

1.静态顺序表的局限性

2.接口函数的参数为什么要用结构体指针？

3.顺序表增删查改的时间复杂度

①顺序表的优势

②顺序表的劣势

6.完整源码

SeqList.h文件

``````#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#define N 5
typedef int SLDataType;

//静态顺序表
typedef struct SeqList
{
SLDataType a[N];//定长数组
int size;//记录存储多少个有效数据
}SeqList;

//初始化顺序表
void SLInit(SeqList* ps);
//静态顺序表的尾插
void SLPushBack(SeqList* ps,SLDataType data);
//静态顺序表的尾删
void SLPopBack(SeqList* ps);
//静态顺序表的头插
void SLPushFront(SeqList* ps,SLDataType data);
//静态顺序表的头删
void SLPopFront(SeqList* ps);
//pos位置插入数据
void SLInsert(SeqList* ps, int pos, SLDataType data);
//pos位置插入数据
void SLErase(SeqList* ps,int pos);
//查找数据
int SLFind(SeqList* ps, SLDataType data, int begin);
//判断数组是否已满
bool IsFull(SeqList* ps);
//打印存储的数据
void SLPrint(SeqList* ps);``````

SeqList.c文件

``````#define _CRT_SECURE_NO_DEPRECATE 1
#include"SeqList.h"

void SLInit(SeqList* ps)
{
assert(ps);
ps->size = 0;
}

void SLPushBack(SeqList* ps,SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//插入数据
ps->a[ps->size] = data;
ps->size++;
}

void SLPopBack(SeqList* ps)
{
assert(ps);
ps->size--;
}

void SLPushFront(SeqList* ps, SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//挪动数据
for (int i = ps->size - 1; i >= 0; i--)
{
ps->a[i + 1] = ps->a[i];
}
//插入数据
ps->a[0] = data;
ps->size++;
}

void SLPopFront(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
//挪动数据
for (int i = 0; i < ps->size-1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}

void SLInsert(SeqList* ps, int pos, SLDataType data)
{
assert(ps);
assert(!IsFull(ps));
//挪动数据
for (int i = ps->size-1; i >= pos; i--)
{
ps->a[i + 1] = ps->a[i];
}
//插入数据
ps->a[pos] = data;
ps->size++;
}

void SLErase(SeqList* ps, int pos)
{
assert(ps);
assert(ps->size > 0);
//挪动数据
for (int i = pos; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}

int SLFind(SeqList* ps, SLDataType data, int begin)
{
assert(ps);
assert(begin < ps->size);
for (int i = begin; i < ps->size; i++)
{
if (ps->a[i] == data)
{
return i;
}
}
return -1;
}

void SLPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("n");
}

bool IsFull(SeqList* ps)
{
assert(ps);
if (ps->size == N)
{
return true;
}
else
{
return false;
}
}``````

THE END