顺序表的初始化,销毁,添加数据(头插,尾插),删除数据(头删,尾删),查找数据,在指定位置插入删除,打印数据
SeqList.h
#pragma once
//在头文件里将要用到的头文件都包含进去
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//定义顺序表的结构(动态顺序表)
/*静态顺序表
#define N 100
struct SeqList
{
int arr[N];
int size;//有效数据个数
};*/
typedef int SLDataType;
typedef struct SepList
{
SLDataType* arr;
int size;//有效数据长度
int capacity;//空间大小
}SL;
//要对顺序表进行的操作--函数声明
//初始化
void SLInit(SL* ps);
//打印
void SLPrint(SL ps);
//头插
void SLPushFront(SL* ps, SLDataType x);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);
//尾删
void SLPopBack(SL* ps);
//在指定位置之前插入 即在指定位置插入
void SLInsert(SL* ps, int pos, SLDataType x);
//在指定位置之前删除 即删除指定位置
void SLErase(SL* ps, int pos);
//在指定位置之后插入 在指定位置之后删除-->思路同上
//销毁
void SLDestroy(SL* ps);
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
//包含头文件
#include"SeqList.h"
//函数实现
//初始化
void SLInit(SL* ps)
{
ps->arr = NULL;//要不然date就是野指针了
ps->size = ps->capacity = 0;
}
//打印
void SLPrint(SL ps)//不用改变顺序表里的值所以不传址SL* ps
{
for(int i=0;i<ps.size;i++)
{
printf("%d ", ps.arr[i]);//不是ps.arr
}
printf("\n");
}
//头插
//剩余空间检查函数
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)//没多余空间了则申请
{
int newCapacity = ps->capacity = 0 ? 4 : 2 * ps->capacity;//三目表达式 原空间为0为真则申请四个空间 原空间不为0则空间扩容两倍
SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//申请空间
if (temp == NULL)//申请失败
{
perror("realloc");
exit(1);//直接退出程序
}
//申请成功
ps->arr = temp;
ps->capacity = newCapacity;
}
}
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);//顺序表都没有就没办法增加数据 不能ps->arr(arr为空不代表ps为空)
SLCheckCapacity(ps);//插入数据要看有没有多余的空间用来存放要插入的数据
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i-1];//直到arr[1]=arr[0]为止
}
ps->arr[0] = x;
ps->size++;
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
//头删
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];//arr[size-2]=arr[size-1]
}
ps->size--;
}
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
//在指定位置之前插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size && pos >= 0);
SLCheckCapacity(ps);
for (int i = ps->size;i>pos; i--)
{
ps->arr[i] = ps->arr[i - 1];//arr[pos+1]=arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
//在指定位置之前删除
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(ps->size);
assert(pos < ps->size && pos >= 0);
for (int i = pos;i<=ps->size-2; i++)
{
ps->arr[i] = ps->arr[i + 1];//arr[size-2]=arr[size-1]
}
ps->size--;
}
//销毁
void SLDestroy(SL* ps)
{
if (ps->arr)//ps->arr!=NULL
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
text.c
#define _CRT_SECURE_NO_WARNINGS 1
//包含头文件
#include"SeqList.h"
void SLTest01()
{
//在进行所有操作之前需要创建一个顺序表
SL s1;
//初始化
SLInit(&s1);
//打印
SLPrint(s1);
//头插
SLPushFront(&s1, 5);
SLPushFront(&s1, 4);
SLPushFront(&s1, 3);
SLPushFront(&s1, 2);
SLPushFront(&s1, 1);
SLPrint(s1);
//尾插
SLPushBack(&s1, 6);
SLPushBack(&s1, 7);
SLPushBack(&s1, 8);
SLPushBack(&s1, 9);
SLPushBack(&s1, 10);
SLPrint(s1);
//头删
SLPopFront(&s1);
SLPopFront(&s1);
SLPopFront(&s1);
SLPopFront(&s1);
SLPrint(s1);
//尾删
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPrint(s1);
//在指定位置之前插入
SLInsert(&s1, 0, 4);
SLInsert(&s1, 0, 3);
SLInsert(&s1, 4, 7);
SLPrint(s1);
//在指定位置之前删除
SLErase(&s1, 0);
SLPrint(s1);
SLErase(&s1, 2);
SLPrint(s1);
//销毁
SLDestroy(&s1);
}
int main()
{
SLTest01();
return 0;
}