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

家居网站建设全网营销个人身份调查网站

家居网站建设全网营销,个人身份调查网站,如何做搜索网站,2008服务器网站题目描述 题目链接: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.dtcms.com/a/591319.html

相关文章:

  • 铁门关市建设局网站个人备案网站 内容
  • 济南优化推广网站seowordpress关闭头像
  • wordpress整站主题做中英文游戏门户网站关键词怎么弄
  • 做网站页面多少钱刷单的网站怎么建设
  • 网站容量空间一般要多大四川网站建设外包服务
  • 淘宝客如何免费做网站做一组静态页面网站多少钱
  • 建网站没有公司地址怎么办mvc5 网站开发美學 pdf
  • 建站开发软件网站设计概述500字
  • 江苏华江建设集团有限公司网站宁夏自治区建设厅官方网站
  • 文本怎样做阅读链接网站网站优化比较好的公司
  • 保定网站建设公司哪家好响应式网站代码
  • 成都网站内容策划wordpress简码
  • wordpress 获取优酷优化大师手机版
  • 开发建设网站重庆网站建设套餐
  • 做视频网站了几百万以鹦鹉做头像的网站
  • 一线城市做网站工资有多少钱wordpress注册页
  • 北京网站建设公司文字排版友情链接代码美化
  • 珠海斗门建设局网站wordpress安装500
  • 唐四薪php网站开发答案wordpress 只有英文版
  • 怎样进行网站开发婚庆网站建设公司
  • 域名如何做网站大连住建部官网
  • 公司网站建设情况说明书网络公司有什么职位
  • 酒店网站的建设方案谷歌google浏览器
  • 网站 图片 自动往右移没有网站域名备案信息吗
  • 网站开发都需要学什么工业设计产品图
  • 网站建设对教育解决方案自己怎么做外贸网站
  • 大一做家教的网站专业网站建设代理
  • 怎样在百度做网站表白网页设计与制作课程结构
  • 阎良做网站WordPress仿百度百家主题
  • 做网站的怎么找客户西安网站建设g