【初阶数据结构】模拟顺序表的开辟
作者:互联网
1、首先定义结构体:
typedef int Seqdata;
typedef struct SeqList
{
Seqdata* data;
int sz;
int capacity;
}SeqList;
SeqList s; //定义结构体
2、初始化结构体:
SeqListInit(&s); //初始化结构体
注意:这里为清晰,分为2个源文件,1个头文件操作
//初始化结构体
void SeqListInit(SeqList* pa)
{
assert(pa);
pa->data = NULL;
pa->sz = pa->capacity = 0;
}
3、从结尾插入数值:
SeqListPushBack(&s, 1);
//尾插
void SeqListPushBack(SeqList* pa, Seqdata x)
{
assert(pa);
SeqListFounded(pa);
pa->data[pa->sz] = x;
pa->sz++;
}
这里我们需要检查扩容,所以设置一个扩容函数:SeqListFounded()
//动态扩容
void SeqListFounded(SeqList* pa)
{
if (pa->sz == pa->capacity)
{
int new_capacity = pa->capacity == 0 ? 4 : pa->capacity * 2; //三目运算符
Seqdata* new_data = (Seqdata*)realloc(pa->data, new_capacity * sizeof(Seqdata));
if (NULL == new_data) //判断空指针
{
perror("Founded");
exit(-1);
}
pa->data = new_data;
pa->capacity = new_capacity;
}
}
4、从头部插入数值:
void SeqListPushFront(SeqList* pa, Seqdata x)
{
assert(pa);
SeqListFounded(pa);
int ret = pa->sz - 1; //因为是下标所以要-1
while (ret >= 0)
{
pa->data[ret + 1] = pa->data[ret];
ret--;
}
pa->data[0] = x;
pa->sz++;
}
5、从尾部删除数值:
//尾删
void SeqListPopBack(SeqList* pa)
{
assert(pa);
//直接--sz,隐藏即可,不用变为0;
if (pa->sz != 0)
pa->sz--;
}
6、从头部删除数值:
//头删
void SeqListPopFront(SeqList* pa)
{
assert(pa);
int ret = 0;
if (pa->sz != 0) //没有数时不删
{
while (ret < pa->sz - 1) //从后往前移
{
pa->data[ret] = pa->data[ret + 1];
ret++;
}
pa->sz--; //覆盖掉最后一个数
}
}
7、结尾释放空间:
//释放空间
void SeqListDistory(SeqList* pa)
{
assert(pa);
free(pa->data);
pa->data = NULL;
pa->capacity = 0;
pa->sz = 0;
}
下面是全部代码:test.c
#include "seqlist.h"
void test()
{
SeqList s; //定义结构体
SeqListInit(&s); //初始化结构体
//尾插
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
//头插
SeqListPushFront(&s, 5);
SeqListPushFront(&s, 4);
SeqListPushFront(&s, 3);
//尾删
SeqListPopBack(&s);
SeqListPopBack(&s);
//头删
SeqListPopFront(&s);
SeqListPopFront(&s);
PrintSeqList(&s);
SeqListDistory(&s); //释放空间
}
int main()
{
test();
return 0;
}
seqlist.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int Seqdata;
typedef struct SeqList
{
Seqdata* data;
int sz;
int capacity;
}SeqList;
//头插尾插
void SeqListPushBack(SeqList* pa, Seqdata x);
void SeqListPushFront(SeqList* pa, Seqdata x);
//头删尾删
void SeqListPopBack(SeqList* pa);
void SeqListPopFront(SeqList* pa);
//创建、初始化、销毁空间
void SeqListFounded(SeqList* pa);
void SeqListInit(SeqList* pa);
void SeqListDistory(SeqList* pa);
//打印
void PrintSeqList(SeqList* pa);
seqlist.c
#include "seqlist.h"
//初始化结构体
void SeqListInit(SeqList* pa)
{
assert(pa);
pa->data = NULL;
pa->sz = pa->capacity = 0;
}
//释放空间
void SeqListDistory(SeqList* pa)
{
assert(pa);
free(pa->data);
pa->data = NULL;
pa->capacity = 0;
pa->sz = 0;
}
//动态扩容
void SeqListFounded(SeqList* pa)
{
if (pa->sz == pa->capacity)
{
int new_capacity = pa->capacity == 0 ? 4 : pa->capacity * 2; //三目运算符
Seqdata* new_data = (Seqdata*)realloc(pa->data, new_capacity * sizeof(Seqdata));
if (NULL == new_data) //判断空指针
{
perror("Founded");
exit(-1);
}
pa->data = new_data;
pa->capacity = new_capacity;
}
}
//尾插
void SeqListPushBack(SeqList* pa, Seqdata x)
{
assert(pa);
SeqListFounded(pa);
pa->data[pa->sz] = x;
pa->sz++;
}
//头插
void SeqListPushFront(SeqList* pa, Seqdata x)
{
assert(pa);
SeqListFounded(pa);
int ret = pa->sz - 1; //因为是下标所以要-1
while (ret >= 0)
{
pa->data[ret + 1] = pa->data[ret];
ret--;
}
pa->data[0] = x;
pa->sz++;
}
//打印顺序表
void PrintSeqList(SeqList* pa)
{
int i = 0;
for (i = 0; i < pa->sz; i++)
{
printf("%d ", pa->data[i]);
}
}
//尾删
void SeqListPopBack(SeqList* pa)
{
assert(pa);
//直接--sz,隐藏即可,不用变为0;
if (pa->sz != 0)
pa->sz--;
}
//头删
void SeqListPopFront(SeqList* pa)
{
assert(pa);
int ret = 0;
if (pa->sz != 0) //没有数时不删
{
while (ret < pa->sz - 1) //从后往前移
{
pa->data[ret] = pa->data[ret + 1];
ret++;
}
pa->sz--; //覆盖掉最后一个数
}
}
标签:sz,初阶,capacity,SeqList,pa,数据结构,data,void,模拟 来源: https://blog.csdn.net/m0_64645696/article/details/122625794