3.9 10 12 数据结构 顺序表
直接代码
接口
点击查看代码
#pragma once
typedef int SLDataType;//直接修改 类型(也可以使用c++的模板)
typedef struct Sequence
{
SLDataType* array;
size_t size;//实际大小
size_t volume;//容易大小
}Sequence;
//增 删 查 改
//顺序表初始化
void qk(Sequence* ps);
//空间判断 空间增加
void SeqListInit(Sequence* ps);
//----------------------------------------------------
///顺序表尾插入
void SeqListPushBack(Sequence* ps, SLDataType x);
//从顺序表头部插入数据
void SeqListPushFront(Sequence* ps, SLDataType x);
//从顺序表中间插入数据
void SeqListInsert(Sequence* ps, size_t pos, SLDataType x);
//----------------------------------------------------
//----------------------------------------------------
//头删
void SeqListPopFront(Sequence* ps);
//尾删
void SeqListPopBack(Sequence* ps);
//随便删
void SeqListErase(Sequence* ps, size_t pos);
//-----------------------------------------------------
//查找
int SeqListFind(Sequence* ps, SLDataType x);
////改
void SeqListErase(Sequence* ps, size_t pos, SLDataType x);
代码实现
点击查看代码
#include"ctj.h"
#include <stdlib.h>
#include <assert.h>
#include<stdio.h>
void qk(Sequence* ps)
{
ps->array = NULL;
ps->size = 0;
ps->volume = 0;
}
//判断顺序表空间
void SeqListInit(Sequence* ps)
{
//1.应该使用三目操作符判断此时容量是否为空,为空容量则开辟成4,再使用realloc开辟动态内存
SLDataType* tmp =NULL;
int NewCapitcy;
assert(ps);
//空间满了直接给4
if (ps->size == ps->volume)
{
NewCapitcy = ps->volume == 0 ? 4 : ps->volume * 2;
tmp = (SLDataType*)realloc(ps->array, NewCapitcy * sizeof(SLDataType));
ps->array = tmp;
ps->volume = NewCapitcy;
printf("已增容空间------ 容量大小:%d\n", ps->volume);
}
}
//顺序表尾插
void SeqListPushBack(Sequence* ps, SLDataType x)
{
//判断是否需要扩大容量
assert(ps);
SeqListInit(ps);
ps->array[ps->size] = x;
ps->size++;
}
//顺序表头插
// 1 2 3 4 5 6
// 9 1 2 3 4 5 6
void SeqListPushFront(Sequence* ps, SLDataType x)
{
assert(ps);
SeqListInit(ps);
for (int i = ps->size; i > 0; i--)
{
ps->array[i] = ps->array[i - 1];
}
ps->array[0] = x;
ps->size++;
}
//随意插
// 1 2 3 4 5 6
// 1 9 2 3 4 5 6
void SeqListInsert(Sequence* ps, size_t pos, SLDataType x)
{
assert(ps);
SeqListInit(ps);
for (int i = ps->size; i >= pos; i--)
{
ps->array[i] = ps->array[i - 1];
}
ps->array[pos] = x;
ps->size++;
}
////---------------------------------///
// 1 2 3 4
// 头
void SeqListPopFront(Sequence* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
ps->array[i] = ps->array[i +1];
}
ps->size--;
}
//尾删
// 1 2 3 4
//
void SeqListPopBack(Sequence* ps)
{
ps->array[ps->size - 1] = 0X00;
ps->size--;
}
//随便删
// 1 2 3 4
// 1 3 4 0
void SeqListErase(Sequence* ps, size_t pos)
{
assert(ps);
for (int i = pos-1; i <= ps->size; i++)
{
ps->array[i] = ps->array[i + 1];
}
ps->size--;
}
//---------------------------
int SeqListFind(Sequence * ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->array[i] == x)
{
printf("下标:%d\n",i+1);
return i+1;
}
}
printf("未找到");
}
///-------------- 1 2 3 4
void SeqListErase(Sequence* ps, size_t pos, SLDataType x)
{
int i = pos - 1;
ps->array[i] = x;
}
本文来自博客园,作者:逆向狗,转载请注明原文链接:https://www.cnblogs.com/Agtw/p/17197817.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现