我的数据结构c(给自己用的)

目录

顺序表:

链表:

栈:

队列:


我想在之后的大学数据结构课上需要自己写来做题,但每次都自己写,那太麻烦了,所以我就将这个博客来把所有的C语言的数据结构弄上去,

问我为什么不用GitHub,虽说也托管上去了,哈哈机房访问的GitHub太慢了!

顺序表:

头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//  队列先进先出typedef int QUDataType;typedef struct QueueNode
{struct QueueNode* next;QUDataType x;
}QueueNode;typedef struct QUEUE
{QueueNode* head;QueueNode* tail;
}Queue;//初始化
void QueueInit(Queue* pq);//入队列
void QueuePush(Queue* pq,QUDataType x);//出队列
void QueuePop(Queue* pq);//取头数据
QUDataType QueueFront(Queue* pq);//取尾数据
QUDataType QueueBack(Queue* pq);//有几个数据int QueueSize(Queue* pq);//是否为空bool QueueEmpty(Queue* pq);//打印
void Print(Queue* pq);

函数文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"void Checkcapacity(SL* pc)
{if (pc->size == pc->capacity){int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;SLDataType* str = (SLDataType*)realloc(pc->a, newcapacity * sizeof(SLDataType));if (str == NULL){perror("realloc");exit(-1);}}
}void print(SL* pc)
{int i = 0;for (i = 0; i < pc->size; i++){printf("%d ", pc->a[i]);}
}//回收空间
void SeqlistDestory(SL* pc)
{free(pc->a);pc->a = NULL;pc->capacity = pc->size = 0;
}void SeqListInit(SL* pc)
{pc->a = NULL;pc->size = 0;pc->capacity = 0;
}void SeqListPushback(SL* pc, SLDataType x)
{//如果没有空间或根本没有就增容:if (pc->size == pc->capacity){int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;SLDataType* str = (SLDataType*)realloc(pc->a,newcapacity*sizeof(SLDataType));if (str == NULL){perror("realloc");exit(-1);}pc->a = str;str = NULL;pc->capacity = newcapacity;}pc->a[pc->size] = x;pc->size++;
}void SeqlistPopback(SL* pc)
{if (pc->size == 0){printf("没有了喵~\n");return;}pc->a[pc->size - 1] = 0;pc->size--;
}void SeqlistPushfront(SL* pc, SLDataType x)
{if (pc->size == pc->capacity){int newcapacity = pc->capacity == 0 ? 4 : pc->capacity * 2;SLDataType* str = (SLDataType*)realloc(pc->a, newcapacity * sizeof(SLDataType));if (str == NULL){perror("realloc");exit(-1);}pc->a = str;str = NULL;pc->capacity = newcapacity;}pc->size += 1;for (int i = pc->size; i>=0; i--){pc->a[i+1] = pc->a[i];}pc->a[0]=x;
}void SeqlistPopfront(SL* pc)
{if (pc->size == 0){printf("没u删除的元素喵~\n");}int i = 1;for (i = i; i <= pc->size+1; i++){pc->a[i - 1] = pc->a[i];}pc->size -= 1;
}//插入数字
void Seqlistinsert(SL* pc, int pos, SLDataType x)
{Checkcapacity(pc);pc->size += 1;int i=pos;for (i = pos; i < pc->size; i++){pc->a[i] = pc->a[i - 1];}pc->a[pos - 1] = x;
}//查找数字()
int  Seqlistfind_bydata(SL* pc, SLDataType x)
{int i = 0;for (i; i < pc->size; i++){if (pc->a[i] == x){printf("返回第一个下标\n");return  i;}}printf("找不到\n");return -1;
}//删除指定数字 
void Seqlistdelet(SL* pc, SLDataType x)
{int i = 0;for (i = 0; i < pc->size; i++){if (x == pc->a[i]){for (i; i < pc->size; i++){pc->a[i] = pc->a[i + 1];}pc->size--;break;}}printf("没这个数字哦\n");return;
}


链表:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>//单链表的英文  Single linked listtypedef int SLLdatatype;typedef struct SListNode
{SLLdatatype  data;struct SListNode*next;
}SLL;//打印
void SLLprint(SLL* phead);//尾部存储 
void SLLpushback(SLL**phead,SLLdatatype x);//头部存储
void SLLpushfront(SLL** phead, SLLdatatype x);void SLLpushfront2(SLL** phead, SLLdatatype x);//尾部删除
void SLLpopback(SLL** phead);//头部删除
void SLLpopfront(SLL** phead);//查找
void SLL_find_print(SLL* phead, SLLdatatype x);//查下标
SLL* SLL_findpos(SLL* phead, SLLdatatype x);//  指定位置插入
void SLL_inset(SLL** phead, SLL* pos, SLLdatatype x);//指定位置删除
void SLL_pos_del(SLL** phead, SLL* pos);//销毁链表
void SLL_destory(SLL* *phead);

函数文件:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"SLL* creatnode( SLLdatatype x)
{SLL* newnode = (SLL*)malloc(sizeof(SLL));if (newnode == NULL){perror("malloc");exit(-1);}newnode->data = x;newnode->next = NULL;return newnode;
}//打印
void SLLprint(SLL* phead)
{/*SLL* cur = phead;*/while (phead != NULL){printf("%d ", phead->data);phead = phead->next;}printf("->NULL\n");
}void SLLpushback(SLL**phead,SLLdatatype x)
{SLL* newnode = (SLL*)malloc(sizeof(SLL));newnode->data = x;newnode->next = NULL;if (*phead == NULL){*phead = newnode;}else{SLL* tail = *phead;while (tail->next != NULL){tail = tail->next;}tail->next = newnode;}
}void SLLpushfront(SLL** phead, SLLdatatype x)
{SLL* newnode = (SLL*)malloc(sizeof(SLL));newnode->data = x;newnode->next = NULL;if (phead == NULL){*phead = newnode;}else{newnode->next = *phead;*phead = newnode;}}void SLLpushfront2(SLL** phead, SLLdatatype x)
{SLL* newnode = creatnode(x);newnode->next = *phead;*phead = newnode;
}//尾部删除
void SLLpopback(SLL** phead){SLL*tail = *phead;SLL* str = NULL;if (*phead==NULL){return ;}if (tail->next ==NULL){free(tail);*phead = NULL;}else{while (tail->next!=NULL){str = tail;tail = tail->next;}free(tail);tail = NULL;str->next = NULL;}
}//头部删除
void SLLpopfront(SLL** phead)
{if (*phead == NULL){return;}if ((*phead)->next == NULL){*phead = NULL;}else{SLL* front = *phead;(*phead)=(*phead)->next;free(front);front = NULL;}
}//找加打印
void SLL_find_print(SLL* phead, SLLdatatype x)
{if (phead == NULL){return;}while (phead->data!=x){if (phead->next== NULL){break;}phead = phead->next;}if (phead->next == NULL){printf("找不到喵~\n");}else{printf("%d\n", phead->data);}
}//查下标
SLL* SLL_findpos(SLL* phead, SLLdatatype x)
{if (phead == NULL){return NULL;}else{while (phead){if (phead->data == x){return phead;}phead = phead->next;}}printf("找不到\n");return NULL;
}//  指定位置插入
void SLL_inset(SLL** phead, SLL* pos, SLLdatatype x)
{if (*phead == NULL){return;}else{SLL* find = *phead;while (find){if (find->next == pos){SLL* newnode = creatnode(x);newnode->next = find->next;find->next = newnode;return;}find = find->next;}}
}//指定位置删除
void SLL_pos_del(SLL** phead, SLL* pos)
{if (*phead == NULL){return;}else{SLL* find = *phead;SLL* findpos = NULL;while (find){if (find->next == pos){findpos = find->next;find->next = findpos->next;free(findpos);findpos = NULL;return;}find = find->next;}}}//销毁链表
void SLL_destory(SLL** phead)
{if (*phead == NULL){return;}else{   while ((*phead)->next!=NULL){SLL* tailpos = *phead;SLL* tail = NULL;while (tailpos->next != NULL){tail = tailpos;tailpos = tailpos->next;}free(tailpos);tail->next = NULL;tailpos = NULL;}free(*phead);*phead = NULL;}
}


栈:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int  STDataType;typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;//初始化栈
void StackIint(ST* ps);//销毁栈
void Stackdestory(ST* pc);//push
void StackPush(ST* ps, STDataType x);//pop
void StackPop(ST* ps);//取栈顶数据
STDataType Stacktop(ST* ps);//有多少数据
int StackSize(ST* ps);//判断是否为空
bool StackEmpty(ST* ps);int minStackGetMin(ST* obj);

函数:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"//初始化单链表
void StackIint(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = 0;ps->top = 0;
}//销毁栈
void Stackdestory(ST* pc)
{assert(pc);free(pc->a);pc->a = NULL;pc->capacity = pc->top = 0;
}//push
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity == ps->top){ps->capacity =ps->capacity== 0 ? 4 : 2 * ps->capacity;STDataType* temp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity);if (temp == NULL){printf("realloc fail\n");exit(-1);}ps->a = temp;}ps->a[ps->top] = x;ps->top += 1;}int minStackGetMin(ST* obj) {int i = 0;int min = obj->a[i];for (i = 1; i<obj->top; i++){if (obj->a[i] < min){min = obj->a[i];}}return min;
}//pop
void StackPop(ST* ps)
{assert(ps->top > 0);ps->top--;
}//取栈顶数据
STDataType Stacktop(ST* ps)
{assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}//有多少数据
int StackSize(ST* ps)
{assert(!StackEmpty(ps));return ps->top;
}
//判断是否为空
bool StackEmpty(ST* ps)
{return ps->top == 0;
}


队列:

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//  队列先进先出typedef int QUDataType;typedef struct QueueNode
{struct QueueNode* next;QUDataType x;
}QueueNode;typedef struct QUEUE
{QueueNode* head;QueueNode* tail;
}Queue;//初始化
void QueueInit(Queue* pq);//入队列
void QueuePush(Queue* pq,QUDataType x);//出队列
void QueuePop(Queue* pq);//取头数据
QUDataType QueueFront(Queue* pq);//取尾数据
QUDataType QueueBack(Queue* pq);//有几个数据int QueueSize(Queue* pq);//是否为空bool QueueEmpty(Queue* pq);//打印
void Print(Queue* pq);

函数:

#define _CRT_SECURE_NO_WARNINGS 1
#include"main.h"//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->head = NULL;pq->tail = NULL;
}void QueueDestory(Queue* pq)
{assert(pq);QueueNode* cur = pq->head;while (cur!=NULL){QueueNode* next = cur->next;free(cur);cur = next;}pq->head = NULL;pq->tail = NULL;}//入队列
void QueuePush(Queue* pq, QUDataType x)
{assert(pq);if (pq->head == NULL && pq->tail == NULL){QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));newnode->x = x;newnode->next = NULL;pq->head=newnode;pq->tail=newnode;}else{QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));newnode->x = x;newnode->next = NULL;QueueNode* tail1 = NULL;tail1 = pq->tail;tail1->next = newnode;pq->tail = newnode;}
}//出队列
void QueuePop(Queue* pq)
{assert(pq);if (pq->head ==NULL){exit(-1);}QueueNode* head1 = pq->head->next;free(pq->head);pq->head = head1;if (pq->head == NULL){pq->head = pq->tail = NULL;}
}//取头数据
QUDataType QueueFront(Queue* pq)
{assert(pq);assert(pq->head);return pq->head->x;
}//取尾数据
QUDataType QueueBack(Queue* pq)
{assert(pq);assert(pq->head);return pq->tail->x;
}//有几个数据int QueueSize(Queue* pq)
{assert(pq);int count = 0;QueueNode*pos = pq->head;while (pos != NULL){count += 1;pos = pos->next;}return count;
}//是否为空
bool QueueEmpty(Queue* pq)
{assert(pq);if (pq->head){return false;}return true;
}//打印
void Print(Queue* pq)
{assert(pq);assert(pq->head);QueueNode* pos = pq->head;while (pos != NULL){printf("%d ", pos->x);pos = pos->next;}printf("\n");
}                                        typedef struct {Queue* q1;Queue* q2;
} MyStack;MyStack* myStackCreate() {MyStack* st = (MyStack*)malloc(sizeof(MyStack));st->q1 = (QUEUE*)malloc(sizeof(QUEUE));st->q2 = (QUEUE*)malloc(sizeof(QUEUE));return st;
}void myStackPush(MyStack* obj, int x) {if (!QueueEmpty(obj->q1)){QueuePush(obj->q1, x);}else{QueuePush(obj->q2, x);}
}int myStackPop(MyStack* obj) {Queue* empty = obj->q1;Queue* noempty = obj->q2;if (!QueueEmpty(obj->q1)){empty = obj->q2;noempty = obj->q1;}int top = QueueBack(noempty);while (QueueSize(noempty) > 0){QueuePush(empty, QueueFront(noempty));QueuePop(noempty);}return top;}int myStackTop(MyStack* obj) {if (!QueueEmpty(obj->q1)){return QueueBack(obj->q1);}return QueueBack(obj->q2);
}bool myStackEmpty(MyStack* obj) {if (QueueEmpty(obj->q1) || QueueEmpty(obj->q2)){return false;}return true;}void myStackFree(MyStack* obj) {free(obj->q1);free(obj->q2);free(obj);
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/660119.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微服务中间件 RabbitMq学习

1、为什么需要Mq 例如在用户注册业务中&#xff0c;用户注册成功后 需要发注册邮件和注册短信&#xff0c;传统的做法有两种 1.串行的方式&#xff1b;2.并行的方式 &#xff1b; 假设三个业务节点分别使用50ms&#xff0c;串行方式使用时间150ms&#xff0c;并行使用时间10…

如何编写具有完备性的测试用例 ? 具体思路是什么 ? 全套解决方案打包呈现给你 。

设计测试用例应该算是测试人员最为主要的工作之一 &#xff0c;好的测试用例往往具有覆盖性强 &#xff0c;扩展性高以及复用性好等特点 。该如何设计出好的测试用例 &#xff1f;是我们每一位测试人员需要重点思考的问题 &#xff0c;下面是我对设计测试用例设计的思考 &#…

代码随想录 Leetcode40.组合总和 II

题目&#xff1a; 代码&#xff08;首刷看解析 2024年2月1日&#xff09;&#xff1a; class Solution { public:vector<vector<int>> res;vector<int> path;void backtracking(vector<int>& candidates, int target, int startIndex, vector<…

activemq 默认端口说明

支持的消息应用协议&#xff1a;OpenWire,Stomp REST,WS Notification,XMPP,AMQP,MQTT 默认使用了61616&#xff08;openwire/activemq服务监控端口&#xff09;、5672(amqp)、61613(stomp)、1883(mqtt)、61614(ws),8161(web管理页面端口&#xff09;等端口

opencv#41 轮廓检测

轮廓概念介绍 通常我们使用二值化的图像进行轮廓检测&#xff0c;对轮廓以外到内进行数字命名&#xff0c;如下图&#xff0c;最外面的轮廓命名为0&#xff0c;向内部进行扩展&#xff0c;遇到黑色白色相交区域&#xff0c;就是一个新的轮廓&#xff0c;然后依次对轮廓进行编号…

玛格全屋定制携手君子签,实现业务信息、流程、合同全面数字化

中国定制家居领导品牌——玛格全屋定制携手君子签&#xff0c;部署玛格业务系统&#xff0c;将电子签章系统与供应链上下游业务合同签署场景融合&#xff0c;通过无纸化、电子化的签署环境&#xff0c;打造业务“线上审批、签署、归档”闭环&#xff0c;助推业务减负提效。 电…

prometheus的alertmanager监控报警

监控告警&#xff1a; alert是一个单独的模块&#xff0c;需要我们单独的配置。 需要声明一个邮箱地址。配置是以configmap进行部署。 alert 实验&#xff1a; vim alert-cfg.yaml apiVersion: v1 kind: ConfigMap metadata:name: alertmanagernamespace: monitor-sa data…

跟着cherno手搓游戏引擎【16】Camera和Uniform变量的封装

相机封装&#xff1a; OrthographicCamera.h: #pragma once #include <glm/glm.hpp> namespace YOTO {class OrthographicCamera{public:OrthographicCamera(float left,float right , float bottom,float top);const glm::vec3& GetPosition()const { return m_Pos…

对同一文件多次mmap

abstract 问&#xff1a;对同一个文件多次mmap&#xff0c;返回的地址相同吗? 答&#xff1a;不相同 code #ifdef __linux__#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> …

山东省七五商贸有限公司缝纫设备采购项目(第一批次)

山东省七五商贸有限公司缝纫设备采购项目(第一批次) (招标编号:JCJS-2024-002) 项目所在地区:山东省 一、招标条件 本山东省七五商贸有限公司缝纫设备采购项目(第一批次)已由项目审批/核准/备案机关批准&#xff0c;项目资金来源为其他资金/&#xff0c;招标人为山东省七五商贸…

Vue-49、Vue技术实现动画效果

1、首先&#xff0c;在Vue项目中的src/components文件夹下创建一个名为AnimatedBox.vue的文件。 2、编辑AnimatedBox.vue文件&#xff0c;添加以下代码&#xff1a; <template><div class"animated-box" click"toggle"><transition name&q…

【C++】STL优先级队列(priority_queue)

priority_queue 基本介绍 priority_queue就是优先级队列。其头文件就是queue&#xff0c;但是队列和优先级队列关系不大&#xff0c;两个是不同的数据结构。但二者都是适配器&#xff0c;容器适配器。 优先级队列中存放的数据是有优先级的。 其内部有以下成员方法&#xff0c…

使用post-css实现移动端适配

介绍移动端适配以及适配方案 适配原因 移动端不像PC端,有足够大的屏幕展示较多的内容不同的移动端设备&#xff0c;有不同屏幕宽度同样大小的页面元素在不同屏幕宽度设备展示时&#xff0c;布局就会错乱有些元素没有展示在可视范围内有些元素不能撑满整个屏幕&#xf…

【Linux】初始进程地址空间

最近&#xff0c;我发现了一个超级强大的人工智能学习网站。它以通俗易懂的方式呈现复杂的概念&#xff0c;而且内容风趣幽默。我觉得它对大家可能会有所帮助&#xff0c;所以我在此分享。点击这里跳转到网站。 目录 一、再谈fork二、程序地址空间2.1代码验证 三、虚拟地址&am…

成熟的汽车制造供应商协同平台 要具备哪些功能特性?

汽车行业是一个产业链长且“重”的行业&#xff0c;整个业务流程包括了研发、设计、采购、库存、生产、销售、售后等一系列环节&#xff0c;在每一个环节都涉及到很多信息交换的需求。对内要保证研发、采购、营销等业务环节信息流通高效安全&#xff0c;对外要与上、下游合作伙…

Python编程实验一:流程控制结构

目录 一、实验目的与要求 二、实验内容 三、主要程序清单和程序运行结果 第1题 第2题 第3题 第4题 四、实验结果分析与体会 一、实验目的与要求 &#xff08;1&#xff09;通过本次实验&#xff0c;学生应掌握多分支语句 if …elif…else结构的用法&#xff1b; &…

vue3/vue2中自定义指令不可输入小数点.

import { directive } from vueconst noDecimal {mounted(el) {el.addEventListener(keypress, (e) > {if (e.key .) {e.preventDefault() }})} }// 使用自定义指令 directive(no-decimal, noDecimal)使用 标签上添加 v-no-decimal <el-input…

重磅!讯飞星火V3.5正式发布,3大核心能力超GPT-4 Turbo!

1月30日&#xff0c;科大讯飞召开星火认知大模型V3.5升级发布会&#xff0c;这是国内首个基于全国产算力训练的多模态认知大模型。科大讯飞董事长刘庆峰先生、研究院院长刘聪先生出席了大会&#xff0c;并对最新产品进行了多维度解读。 讯飞星火V3.5的7大核心能力实现全面大幅…

P3654 First Step (ファーストステップ)题解

题目 浦之星女子学院的篮球场是一个R行C列的矩阵&#xff0c;其中堆满了各种学校的杂物 (用#表示)&#xff0c;空地 (用.表示) Aqours 现在已经一共有K个队员了&#xff0c;要歌唱舞蹈起来的话&#xff0c;得排成一条1K的直线&#xff0c;一个接一个地站在篮球场的空地上呢 (横…

C++面试:堆排序、归并排序、二分查找等高阶算法

目录 堆排序 (Heap Sort) 步骤&#xff1a; 时间复杂度&#xff1a; 空间复杂度&#xff1a; 归并排序 (Merge Sort) 步骤&#xff1a; 时间复杂度&#xff1a; 空间复杂度&#xff1a; 二分查找 (Binary Search) 步骤&#xff1a; 时间复杂度&#xff1a; 空间复杂…