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

湘潭网站seo磐石网络佛山做网站的公司有哪些

湘潭网站seo磐石网络,佛山做网站的公司有哪些,大连模板网站制作哪家专业,现代化的中国风网站目录 栈 概念与结构 栈的结构 初始化与销毁 入栈 出栈 取栈顶元素 取元素个数 整体实现 Stack.h Stack.c 队列 概念与结构 队列的结构 初始化与销毁 入队 出队 取队头和队尾元素 取元素个数 整体实现 Queue.h Queue.c 栈 概念与结构 栈:一种…

目录

概念与结构

栈的结构

初始化与销毁

入栈

出栈

取栈顶元素

取元素个数

整体实现

Stack.h

Stack.c

队列

概念与结构

队列的结构

初始化与销毁

入队

出队

取队头和队尾元素

取元素个数

整体实现

Queue.h

Queue.c


概念与结构

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO的原则。

压栈:栈的插入操作叫做进栈 / 压栈 / 入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

这里使用数组来实现栈,入栈出栈的时间复杂度都是 O(1) 。

栈的结构

栈和顺序表很像,把顺序表的size改为top。

typedef int STDataType;
typedef struct Stack
{STDataType* arr;int top;             //栈顶int capacity;
}ST;

初始化与销毁

void STInit(ST* ps)
{ps->arr = NULL;ps->top = ps->capacity = 0;
}void STDestroy(ST* ps)
{if (ps->arr)free(ps->arr);ps->arr = NULL;ps->top = ps->capacity = 0;
}

入栈

就像顺序表的尾插,入栈也一样

void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity == ps->top){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* newarr = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));if (newarr == NULL){perror("realloc fail!");exit(1);}ps->arr = newarr;}ps->arr[ps->top] = x;ps->top++;
}

出栈

bool StackEmpty(ST* ps)        //返回bool型判断
{assert(ps);return ps->top == 0;
}void StackPop(ST* ps)          //出栈
{assert(!StackEmpty(ps));--ps->top;
}

取栈顶元素

STDataType StackTop(ST* ps)
{assert(!StackEmpty(ps));return ps->arr[ps->top - 1];   // ps->top - 1
}

取元素个数

int STSize(ST* ps)
{assert(ps);return ps->top;
}

整体实现

Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int STDataType;
typedef struct Stack
{STDataType* arr;int top;int capacity;
}ST;void STInit(ST* ps);
void STDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
int STSize(ST* ps);
bool StackEmpty(ST* ps);

Stack.c

#include"Stack.h"void STInit(ST* ps)
{ps->arr = NULL;ps->top = ps->capacity = 0;
}void STDestroy(ST* ps)
{if (ps->arr)free(ps->arr);ps->arr = NULL;ps->top = ps->capacity = 0;
}void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->capacity == ps->top){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* newarr = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));if (newarr == NULL){perror("realloc fail!");exit(1);}ps->arr = newarr;}ps->arr[ps->top] = x;ps->top++;
}bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}void StackPop(ST* ps)
{assert(!StackEmpty(ps));--ps->top;
}STDataType StackTop(ST* ps)
{assert(!StackEmpty(ps));return ps->arr[ps->top - 1];
}int STSize(ST* ps)
{assert(ps);return ps->top;
}

队列

概念与结构

概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO。

入队列:进行插入操作的一端称为队尾。

出队列:进行删除操作的一端称为队头。

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

队列的结构

为了追求更高的效率,这里创建两个结构体,一个为队列本体,一个为队列的结点。

typedef int QDataType;typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;typedef struct Queue
{QueueNode* phead;QueueNode* ptail;int size;             //在数据多时,为了更好的知道队列内有多少元素,多
}Queue;                   //创建一个size来记录队列内元素的数量。

初始化与销毁

void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}

入队

void QueuePush(Queue* pq,QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;if (pq->phead == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}

出队

bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == 0;
}void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}

取队头和队尾元素

QDataType QueueFront(Queue* pq)
{assert(!QueueEmpty(pq));return pq->phead->data;
}QDataType QueueBack(Queue* pq)
{assert(!QueueEmpty(pq));return pq->ptail->data;
}

取元素个数

int QueueSize(Queue* pq)
{//int size = 0;//QueueNode* pcur = pq->phead;//while (pcur)//{//	++size;//	pcur = pcur->next;//}//return size;return pq->size;
}

整体实现

Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int QDataType;typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;typedef struct Queue
{QueueNode* phead;QueueNode* ptail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);

Queue.c

#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}void QueuePush(Queue* pq,QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;if (pq->phead == NULL){pq->phead = pq->ptail = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == 0;
}void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(!QueueEmpty(pq));return pq->phead->data;
}QDataType QueueBack(Queue* pq)
{assert(!QueueEmpty(pq));return pq->ptail->data;
}int QueueSize(Queue* pq)
{//int size = 0;//QueueNode* pcur = pq->phead;//while (pcur)//{//	++size;//	pcur = pcur->next;//}//return size;return pq->size;
}

http://www.dtcms.com/a/518504.html

相关文章:

  • 做网站用什么开发工具管理员网站后台上传本地视频
  • 惠东网站开发免费自动取名100个
  • wordpress引用百度seo排名优化价格
  • 域名申请了怎么做网站桂林漓江竹筏
  • 国外游戏网站欣赏wordpress在线编辑器
  • 东阿做网站多少钱阿里云wordpress建站教程
  • 个人网站如何搭建网易企业邮箱入口官网
  • wordpress仿站视频网络营销的基础与前提是什么理论
  • 徐州建站推广手机屏网站开发
  • 深圳专业网站开发广东省水利工程建设信息网站
  • 营销网站建设推广上海企炬做的网站
  • 怎么用壳域名做网站可以做彩字的网站
  • 做企业网站备案都需要什么资料网站做直链下载存储解决方案
  • 企业网站开发背景及意义网站服务公司名称
  • win7 网站系统怎么做外链数是网站反向链接码
  • 天进机械东莞网站建设北京专业网站翻译影音字幕翻译速记速记速记速而高效
  • 公司网站 开源网站的后台管理员系统建设教程
  • 阅读转发网站那些做的比较好张家界旅游
  • 有没有网站可以做发虚拟币iis安装好了 网站该怎么做
  • 华宁县住房和城乡建设局网站微信公众号微网站怎么做的
  • 建设网站e护航下载台州做网站需要多少钱
  • dede关闭网站新网站备案查询
  • 食品网站开发的背景树莓派 wordpress mysql
  • 重庆网站建设公司销售网站建设运营成本
  • 端口扫描站长工具天津市建设网官网
  • 三网合一网站建设程序评估企业网站建设
  • 电子商务网站建设需要注意什么个人做电影网站
  • 亦庄网站设计小程序开发工具怎么用
  • 369网站建设合格的网站设计师需要会什么软件
  • asp门户网站源码国防教育网站建设方案