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

【C语言数据结构】模拟·顺序表·总项目实现

在这里插入图片描述

💐 🌸 🌷 🍀 🌹 🌻 🌺 🍁 🍃 🍂 🌿 🍄🍝 🍛 🍤
📃个人主页 :阿然成长日记 👈点击可跳转
📆 个人专栏: 🔹数据结构与算法🔹C语言进阶
🚩 不能则学,不知则问,耻于问人,决无长进
🍭 🍯 🍎 🍏 🍊 🍋 🍒 🍇 🍉 🍓 🍑 🍈 🍌 🍐 🍍

前言

我在上一篇博客中,详细讲解啦每一个函数的实现思路和代码展现,在这一篇博客中,我将像是做项目一样,去实现顺序表的总体实现。

一、项目源文件构成

该项目由三部分组成
1️⃣ 用来存放库函数,宏定义,函数申明等的一个头文件:SqList.h
2️⃣ 主函数的所在文件 test.c
3️⃣各个函数的实现,我们主要在此完成函数的代码编写:SqList.c

二、菜单

建立一个菜单是很重要的,菜单能够实现和用户的交互,以便于用户的操作。

代码如下:

void meun()
{printf("**************************************************\n");printf("*    1.顺序表尾插            2.顺序表尾删        *\n");printf("*    3.顺序表头插            4.顺序表头删        *\n");printf("*    5.顺序表的查找          6.在pos位置插入x    *\n");printf("*    7.删除pos位置的值       8.顺序表的打印      *\n");printf("*    0.退出                                      *\n");printf("**************************************************\n");
}

三、顺序表结构体

typedef int SLDataType;typedef struct SeqList
{SLDataType* array;int size;//当前存储个数int capacity;//空间大小}SL;

四、源文件展示

下面是整个项目的代码:

1.SqList.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>#define INT_SIZE 2typedef int SLDataType;typedef struct SeqList
{SLDataType* array;int size;//当前存储个数int capacity;//空间大小}SL;//顺序表的初始化
void SeqListInit(SL* ps);// 顺序表尾插
void SeqListPushBack(SL* ps, SLDataType x);// 顺序表尾删
void SeqListPopBack(SL* ps);// 顺序表头插
void SeqListPushFront(SL* p, SLDataType x);// 顺序表头删
void SeqListPopFront(SL* p);// 顺序表查找
int SeqListFind(SL* p, SLDataType x);// 顺序表在pos位置插入x
void SeqListInsert(SL* p, size_t pos, SLDataType x);// 顺序表删除pos位置的值
void SeqListErase(SL* p, size_t pos);// 顺序表销毁
void SeqListDestory(SL* p);// 顺序表打印
void SeqListPrint(SL* p);

2.SqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"
//顺序表的初始化
void SeqListInit(SL* ps)
{ps->array = (SLDataType*)malloc(sizeof(SLDataType)*4);if (ps->array == NULL){perror("malloc failed\n");exit(-1);}ps->size = 0;ps->capacity = 4;
}// 检查空间,如果满了,进行增容
int CheckCapacity(SL* ps)
{if (ps->size == ps->capacity){SLDataType* tmp = (SLDataType*)realloc(ps->array, (ps->capacity + INT_SIZE)*sizeof(SLDataType));if (tmp == NULL){perror("扩容失败!\n");return 0;}else{ps->array = tmp;ps->capacity += INT_SIZE;printf("扩容成功\n");return 1;}	}return 1;
}// 顺序表销毁
void SeqListDestory(SL* ps)
{free(ps->array);ps->array = NULL;ps->capacity = 0;ps->size = 0;
}// 顺序表尾插
void SeqListPushBack(SL* ps, SLDataType x)
{if (CheckCapacity(ps) == 0){printf("空间已满,插入失败!\n");}if (CheckCapacity(ps) == 1){ps->array[ps->size] = x;ps->size ++;}
}// 顺序表尾删
void SeqListPopBack(SL* ps)
{if (ps->size < 1){printf("已经为空,无元素可删除\n");return;}ps->array[ps->size - 1] = 0;ps->size--;
}// 顺序表头插
void SeqListPushFront(SL* ps, SLDataType x)
{//判断空间是否够。//先挪动if (CheckCapacity(ps) == 0){printf("空间已满,头插入失败!\n");}if (CheckCapacity(ps) == 1){int end = ps->size - 1;while (end>=0){ps->array[end + 1] = ps->array[end];end--;}ps->array[0] = x;ps->size++;}
}// 顺序表头删
void SeqListPopFront(SL* ps)
{int n = 1;while (n < ps->size){ps->array[n - 1] = ps->array[n];n++;}ps->size--;
}// 顺序表打印
void SeqListPrint(SL* ps)
{for (int i = 0; i < ps->size; i++){printf("%d ", ps->array[i]);}printf("\n");
}// 顺序表查找
int SeqListFind(SL* ps, SLDataType x)
{int i = 0,count=0;for (i = 0; i < ps->size - 1; i++){if (x == ps->array[i]){printf("找到啦!下标是%d\n",i);count++;return i;}}if (count == 0){printf("没找到!\n");return;}
}// 顺序表在pos位置插入x
void SeqListInsert(SL* ps, size_t pos, SLDataType x)
{assert(pos>=0&&pos<ps->size);if (CheckCapacity(ps) == 0){printf("空间已满,插入失败!\n"); }if (CheckCapacity(ps) == 1){int end = ps->size - 1 ;while (end >= pos){ps->array[end + 1] = ps->array[end];end--;}ps->array[pos] = x;ps->size++;}
}// 顺序表删除pos位置的值
void SeqListErase(SL* ps, size_t pos)
{assert(pos >= 0 && pos < ps->size);int n = pos + 1;while (n <= ps->size - 1){ps->array[n - 1] = ps->array[n];n++;}ps->size--;
}

3.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SqList.h"void meun()
{printf("**************************************************\n");printf("*    1.顺序表尾插            2.顺序表尾删        *\n");printf("*    3.顺序表头插            4.顺序表头删        *\n");printf("*    5.顺序表的查找          6.在pos位置插入x    *\n");printf("*    7.删除pos位置的值       8.顺序表的打印      *\n");printf("*    0.退出                                      *\n");printf("**************************************************\n");
}
int main()
{int x,pos;SL s;//初始化SeqListInit(&s);int input = 0;do{meun();printf("请输入你的选择:\n");scanf("%d", &input);switch (input){case 1:printf("请输入要尾插的值:");scanf("%d", &x);SeqListPushBack(&s, x);break;case 2:SeqListPopBack(&s);break;case 3:printf("请输入要头插的值:");scanf("%d", &x);SeqListPushFront(&s,x);break;case 4:SeqListPopFront(&s);break;case 5:printf("请输入要查找的值:");scanf("%d", &x);SeqListFind(&s,x); break;case 6:{printf("请输入要插入的位置和数据(空格分开):");scanf("%d %d", &pos,&x);SeqListInsert(&s, pos, x);break;}case 7:printf("请输入要删除的位置:");scanf("%d", &pos);SeqListErase(&s,pos); break;case 8:SeqListPrint(&s);break;case 0:SeqListDestory(&s);break;default:printf("输入有误,请重新输入:\n");}}while (input);
}

五、运行截图

1.顺序表尾插,头插展示

在这里插入图片描述

2.顺序表的头删

在这里插入图片描述

3.顺序表的尾删

在这里插入图片描述

4.顺序表的查找

在这里插入图片描述

5.在pos位置插入x

在这里插入图片描述

6.在pos位置删除元素

在这里插入图片描述

总结:

本次项目当中遇到许多之气没有注意到的问题,尤其是数组越界问题等等,在接下来学习数据结构预算法是非常重要的,🌈相信自己,踏踏实实走好每一步,梦想终会成为现实! ⛵️

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

相关文章:

  • 自然语言处理从入门到应用——LangChain:模型(Models)-[文本嵌入模型Ⅰ]
  • 使用Gradio构建生成式AI应用程序; Stability AI推出Stable Diffusion XL 1.0
  • Java 递归计算斐波那契数列指定位置上的数字
  • ai数字人透明屏的应用场景有哪些?
  • 一、1、Hadoop的安装与环境配置
  • 剑指YOLOv7改进最新MPDIoU损失函数(23年7月首发论文):论文实测YOLOv7模型涨点,超越现有多种G/D/C/EIoU,高效准确的边界框回归的损失
  • 前端JavaScript面试100问(上)
  • C语言第九课------------------数组----------------C中之将
  • MySQL的安装
  • 在Chrome(谷歌浏览器)中安装Vue.js devtools开发者工具及解决Vue.js not detected报错
  • 用Python实现概率矩阵分解(PMF)算法在MovieLens ml-100k数据集上构建精确的推荐系统:深入理解GroupLens数据的操作
  • WPF icon的设置
  • 使用frp中的xtcp映射穿透指定服务实现不依赖公网ip网速的内网穿透p2p
  • 2023-07-28 LeetCode每日一题(并行课程 III)
  • 8.11 PowerBI系列之DAX函数专题-TopN中实现N的动态
  • 后端性能测试的类型
  • 关闭Tomcat的日志输出
  • express 路由匹配和数据获取
  • 62 | Python 操作 PDF
  • [SQL挖掘机] - 左连接: left join
  • Android 之 使用 SoundPool 播放音效
  • 防火墙的ALG、NAT、双机热备知识点详解
  • 传染病模型
  • 一百三十七、Hive——HQL运行报错(持续更新中)
  • Spring Boot配置加密实践
  • SwiftUI-基础
  • vue。cli怎么使用自定义组件,会有哪些问题
  • linux----vim的使用
  • 95. Python基础教程:异常处理try...except语句
  • 详解rocketMq通信模块升级构想