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

四川城乡住房城乡建设厅网站首页银川网站建设网络

四川城乡住房城乡建设厅网站首页,银川网站建设网络,免费学软件的自学网站,wordpress网站换字体题目描述 题目链接:232. 用栈实现队列 - 力扣(LeetCode) 题目分析 我们先把之前写的数组栈的实现代码搬过来 用栈实现队列最主要的是实现队列先进先出的特点,而栈的特点是后进先出,那么我们可以用两个栈来实现&…

题目描述

题目链接:232. 用栈实现队列 - 力扣(LeetCode)

题目分析

我们先把之前写的数组栈的实现代码搬过来

用栈实现队列最主要的是实现队列先进先出的特点,而栈的特点是后进先出,那么我们可以用两个栈来实现:

  • 一个pushst用来入队列
  • 一个popst用来出队列

具体的接口有下面几个:

初始化

malloc一块空间来存两个栈,同时初始化这两个栈

入队列

入数据都入到pushst

出队列

出数据前先需要导数据:当popst为空且pushst不为空的时候,pushst的top数据依次push到popst中,然后返回pop的top数据,然后pop掉top数据;如果pop不为空,则直接返回poptop并pop

返回队头数据

判空

两个栈同时为空则为空

销毁 

销毁还是依次销毁

代码示例

#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 STInit(ST* pst);
//销毁
void STDestroy(ST* pst);
//入栈
void STPush(ST* pst, STDataType x);
//出栈
void STPop(ST* pst);
//返回栈顶元素
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//栈的元素个数
int STSize(ST* pst);//初始化
void STInit(ST* pst)
{assert(pst);pst->a = NULL;pst->capacity = 0;pst->top = 0;
}
//销毁
void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = pst->capacity = 0;
}
//入栈
void STPush(ST* pst, STDataType x)
{assert(pst);if (pst->top == pst->capacity){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType * )realloc(pst->a, sizeof(STDataType) * newcapacity);if (tmp == NULL){perror("realloc fail");return;}pst->a = tmp;pst->capacity = newcapacity;}pst->a[pst->top] = x;pst->top++;
}
//出栈
void STPop(ST* pst)
{assert(pst);assert(pst->top > 0);pst->top--;
}
//返回栈顶元素
STDataType STTop(ST* pst)
{assert(pst);assert(pst->top > 0);return pst -> a[pst->top - 1];
}
//判空
bool STEmpty(ST* pst)
{assert(pst);/*if (pst->top == 0){return true;}else{return false;}*/return pst->top == 0;
}
//栈的元素个数
int STSize(ST* pst)
{assert(pst);return pst->top;
}typedef struct {ST pushst;ST popst;
} MyQueue;MyQueue* myQueueCreate() {MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));STInit(&(obj->pushst));STInit(&(obj->popst));return obj;
}void myQueuePush(MyQueue* obj, int x) {STPush(&(obj->pushst),x);
}int myQueuePop(MyQueue* obj) {int front=myQueuePeek(obj);STPop(&(obj->popst));return front;
}int myQueuePeek(MyQueue* obj) {if(STEmpty(&(obj->popst))){while(!STEmpty(&(obj->pushst))){STPush(&(obj->popst),STTop(&(obj->pushst)));        STPop(&(obj->pushst));}}return STTop(&(obj->popst));
}bool myQueueEmpty(MyQueue* obj) {return STEmpty(&(obj->pushst))&&STEmpty(&(obj->popst));
}void myQueueFree(MyQueue* obj) {STDestroy(&(obj->pushst));STDestroy(&(obj->popst));free(obj);
}/*** Your MyQueue struct will be instantiated and called as such:* MyQueue* obj = myQueueCreate();* myQueuePush(obj, x);* int param_2 = myQueuePop(obj);* int param_3 = myQueuePeek(obj);* bool param_4 = myQueueEmpty(obj);* myQueueFree(obj);
*/

http://www.yayakq.cn/news/907072/

相关文章:

  • 广州木马网站建设公司怎么样搜索引擎和浏览器
  • 网站开发界面wordpress图文直播插件
  • 常州外贸集团 网站建设企业邮箱格式是什么样的
  • 申请了域名先做网站还是先备案网页设计 网站
  • 设计海报网站移动网站开发语言
  • wordpress怎么修改网站标题网站建设管理自查报告
  • 佛山网站建设及优化公司汽车销售公司的网站怎么做
  • 企业建设网站需要注意什么手续东莞网络科技有限公司
  • 网页设计工作室网站甘肃网站怎样备案
  • 设计网站都有哪些用asp做网站需要什么软件
  • 菏泽建设公司网站做网站 图片格式
  • 诸城哪有做公司网站和的黄石做网站多少钱
  • 做视频网站 视频放在哪佛山全网营销型网站建设
  • 相亲网站排名前十名玉树营销网站建设多少钱
  • zencart网站地图生成合肥培训网站建设
  • 网站建站系统程序网站设计怎么做明信片
  • 营口手机网站建设外贸公司有哪些工作岗位
  • 怎么做招聘网站手机版的网站用什么开发
  • 哪家公司做网站便宜南京网站网站建设公司
  • 免费的好看图片如何做营销型手机网站优化
  • 做纺织机械的网站域名自己做网站引用别人的电影
  • 建材网站的模板网站建设可行性分析报告范文
  • 常州市做网站的公司win2003建设网站
  • 张家港专业做网站WordPress查看主题源代码
  • 手机移动网站设计怎样给公司做一个网站做推广
  • 郑州专业公司网站制作公司在线生成头像
  • 什么类型的网站流量高伊春住房和城乡建设网站
  • 何做百度推广网站wordpress怎么调用外部主题网页
  • 网站排名效果好品牌网站建设价格实惠
  • 广州黄浦区建设局网站教你学做窗帘的网站