文章目录
头文件
c
复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define INIT_CAPACITY 4
typedef int SLDataType;
// 动态顺序表 -- 按需申请
typedef struct SeqList {
SLDataType* a;
int size;
int capacity;
}SL;
//打印
void SLPrint(SL* ps);
//初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);
//扩容
void SLCheckCapacity(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 SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
实现文件
c
复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//打印
void SLPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
//初始化
void SLInit(SL* ps)
{
ps->size = 0;
ps->capacity = INIT_CAPACITY;
SLDataType* arr = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
if (arr == NULL) {
perror("malloc fail!\n");
exit(1);
}
else {
ps->a = arr;
arr = NULL;
}
}
//销毁
void SLDestory(SL* ps)
{
assert(ps);
//如果顺序表为非空链表,就将ps->a的空间释放
if(ps->a)
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
//判断是否需要扩容,如果需要则进行扩容
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
ps->capacity *= 2;
SLDataType* arr = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity));
if (arr == NULL)
{
perror("realloc fail!\n");
exit(1);
}
ps->a = arr;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size - 1; i >= 0; i--)
{
ps->a[i + 1] = ps->a[i];
}
ps->a[0] = x;
ps->size++;
}
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size > 0);
ps->size--;
}
//头删
void SLPopFront(SL* 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--;
}
//找到某元素第一次出现的位置
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return i;
}
return -1;
}
//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size && ps >= 0);
SLCheckCapacity(ps);
for (int i = ps->size - 1; i >= pos; i--)
{
ps->a[i + 1] = ps->a[i];
}
ps->a[pos] = x;
ps->size++;
}
//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos <= ps->size && ps >= 0);
for (int i = pos; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
测试文件
c
复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void Test1()
{
//创建顺序表
SL* ps = (SL*)malloc(sizeof(SL*));
//初始化
SLInit(ps);
//尾插
SLPushBack(ps, 1);
SLPushBack(ps, 2);
SLPushBack(ps, 3);
SLPushBack(ps, 4);
//头插
//SLPushFront(ps, 5);
//SLPushFront(ps, 6);
//SLPushFront(ps, 7);
//SLPushFront(ps, 8);
//尾删
//SLPopBack(ps);
//SLPopBack(ps);
//SLPopBack(ps);
//SLPopBack(ps);
//头删
//SLPopFront(ps);
//SLPopFront(ps);
//SLPopFront(ps);
//SLPopFront(ps);
//指定位置之前插入数据
SLInsert(ps, 3, 0);
SLErase(ps, 3);
//打印
SLPrint(ps);
//查找
//int find = SLFind(ps, 2);
//printf("%d", find);
//销毁
SLDestory(ps);
}
int main()
{
Test1();
return 0;
}