当前位置: 首页 > news >正文

数据结构顺序表(C语言版)

目录

  • 1.实现的接口及其功能
  • 2.代码块

1.实现的接口及其功能

  //初始化顺序表

void initSL(SL* p);
//销毁顺序表
void DestorySL(SL* p);
//头插
void PushFont(SL* p, SeqListType x);
//尾插
void PushBack(SL* p, SeqListType x);
//头删
void PopFont(SL* p);
//尾删
void PopBack(SL* p);
//显示存的数据
void Show(SL* p);
//检查扩容
void CheckSL(SL* p);
//指定位置插入
void InsertSL(SL* p, int x, SeqListType y);
//指定位置删除
void EraseSL(SL* p,int x);
//查找某一位置
int FindSlL(SL* p, SeqListType x);
//把某一位置存的数改掉
void ModifySL(SL* p, int pos, SeqListType x);

2.代码块

测试顺序表功能代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
int main()
{SL sl;initSL(&sl);PushBack(&sl, 1);PushBack(&sl, 2);PushFont(&sl, 3);PushFont(&sl, 4);PushFont(&sl, 5);Show(&sl);PopFont(&sl);Show(&sl);PopBack(&sl);Show(&sl);InsertSL(&sl, 3, 6);EraseSL(&sl, 0);Show(&sl);DestorySL(&sl);
}

顺序表声明代码

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SeqListType;
typedef struct SeqList
{SeqListType* a;int size;int capacity;
}SL;
//初始化顺序表
void initSL(SL* p);
//销毁顺序表
void DestorySL(SL* p);
//头插
void PushFont(SL* p, SeqListType x);
//尾插
void PushBack(SL* p, SeqListType x);
//头删
void PopFont(SL* p);
//尾删
void PopBack(SL* p);
//显示存的数据
void Show(SL* p);
//检查扩容
void CheckSL(SL* p);
//指定位置插入
void InsertSL(SL* p, int x, SeqListType y);
//指定位置删除
void EraseSL(SL* p,int x);
//查找某一位置
int FindSlL(SL* p, SeqListType x);
//把某一位置存的数改掉
void ModifySL(SL* p, int pos, SeqListType x);

顺序表实现代码

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void CheckSL(SL* p)
{if (p->capacity == p->size){   SeqListType* tmp = (SeqListType*)realloc(p->a, sizeof(SeqListType) * 2 * p->capacity);if (tmp == NULL){perror("realloc fail\n");return;}p->capacity *= 2;p->a = tmp;tmp = NULL;}
}
void initSL(SL* p)
{p->a = (SeqListType*)malloc(sizeof(SeqListType)*4);if (p->a == NULL){perror("malloc fail\n");return;}p->capacity = 4;p->size = 0;
}
void DestorySL(SL* p)
{free(p->a);p->a = NULL;p->capacity = 0;p->size = 0;
}
void PushBack(SL* p, SeqListType x)
{CheckSL(p);p->a[p->size++] = x;
}
void PopBack(SL* p)
{assert(p->size > 0);p->size--;
}
void PushFont(SL* p, SeqListType x)
{CheckSL(p);for (int i = p->size; i > 0; i--){p->a[i] = p->a[i - 1];}p->a[0] = x;p->size++;
}
void PopFont(SL* p)
{assert(p->size > 0);for (int i = 0; i < p->size - 1; i++){p->a[i] = p->a[i + 1];}p->size--;
}
void Show(SL* p)
{for (int i = 0; i < p->size; i++){printf("%d ", p->a[i]);}printf("\n");
}
void InsertSL(SL* p, int x, SeqListType y)
{assert(x<=p->size);CheckSL(p);for (int i = p->size-1; i >= x; i--){p->a[i + 1] = p->a[i];}p->a[x] = y;p->size++;
}
void EraseSL(SL* p, int x)
{assert(x <= p->size - 1 && x >= 0);for (int i = x; i < p->size - 1; i++){p->a[i] = p->a[i + 1];}p->size--;
}
int FindSlL(SL* p, SeqListType x)
{   int pos = -1;for (int i = 0; i < p->size; i++){if (p->a[i] == x){pos = i;break;}}return pos;
}
void ModifySL(SL* p, int pos, SeqListType x)
{assert(pos <= p->size - 1 && pos >= 0);p->a[pos] = x;
}

结尾:今天的分享到此结束,喜欢的朋友如果感觉有帮助可以点赞三连支持,咱们共同进步!

http://www.lryc.cn/news/205618.html

相关文章:

  • 新手如何备考学习PMP?
  • [卷积神经网络]FasterNet论文解析
  • 知识图谱+推荐系统 文献阅读
  • shell_39.Linux参数测试
  • 3D模型格式转换工具HOOPS Exchange助力SIMCON搭建注塑项目
  • Linux_虚拟内存机制
  • 淘宝官方开放平台API接口获得店铺的所有商品、商品id、商品标题、销量参数调用示例
  • Java Spring 通过 AOP 实现方法参数的重新赋值、修改方法参数的取值
  • Real3D FlipBook jQuery Plugin 3.41 Crack
  • Pytorch:model.train()和model.eval()用法和区别,以及model.eval()和torch.no_grad()的区别
  • Linux CentOS 8(firewalld的配置与管理)
  • C复习-指针
  • Runnable和Thread的区别,以及如何调用start()方法
  • 云音乐Android Cronet接入实践
  • Linux dup和dup2
  • Spring Boot实战 | 如何整合高性能数据库连接池HikariCP
  • Spring依赖注入
  • Linux下Jenkins自动化部署SpringBoot应用
  • 【git 学习】--- ubuntu18.04 搭建本地git服务器
  • JAVA电商平台免费搭建 B2B2C商城系统 多用户商城系统 直播带货 新零售商城 o2o商城 电子商务 拼团商城 分销商城
  • Android 13 Framework 裁剪
  • 【Axios封装示例Vue2】
  • k8s-----20、持久化存储--PV/PVC
  • python matplotlib 生成矢量图
  • 机器学习中常见的特征工程处理
  • Spring IOC 和 AOP
  • echarts插件-liquidFill(水球图)
  • c++ vscode cmake debug for mac
  • 17 结构型模式-享元模式
  • 创建Secret(手动)