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

网站换空间上怎么办啊沈阳德泰诺网站建设

网站换空间上怎么办啊,沈阳德泰诺网站建设,鞍山网站开发,网站关键词快排名题目 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现 MyQueue 类: void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开…

题目

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

题解

根据栈后进先出的性质,可将两个栈分别设置为只压入元素的栈和只弹出元素的栈,以此来满足队列先进先出的性质。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.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->top = 0; //指向栈顶元素的下一个位置pst->capacity = 0;
}void STDestroy(ST* pst)
{assert(pst);free(pst->a);pst->a = NULL;pst->top = 0;pst->capacity = 0;
}void STPush(ST* pst, STDataType x)
{if (pst->top == pst->capacity){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));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(!STEmpty(pst));pst->top--;
}STDataType STTop(ST* pst)
{assert(pst);assert(!STEmpty(pst));return pst->a[pst->top - 1];
}bool STEmpty(ST* pst)
{assert(pst);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 myQueuePeek(MyQueue* obj) {if(STEmpty(&obj->popst)){while(!STEmpty(&obj->pushst)){STPush(&obj->popst,STTop(&obj->pushst));STPop(&obj->pushst);}}return STTop(&obj->popst);
}int myQueuePop(MyQueue* obj) {int front =  myQueuePeek(obj);STPop(&obj->popst);return front;
}bool myQueueEmpty(MyQueue* obj) {return STEmpty(&obj->pushst) && STEmpty(&obj->popst);
}void myQueueFree(MyQueue* obj) {STDestroy(&obj->pushst);STDestroy(&obj->popst);free(obj);
}

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

相关文章:

  • 南京工程建设招聘信息网站优秀seo平台
  • 罗田网站建设石家庄网站建设方案咨询
  • 提交网站给百度国外优秀的html5网站
  • 网站建设唐山赣州章贡区天气预报
  • 微信营销网站wordpress 5 开发
  • 对网站建设在电子商务中的看法企业百度推广怎么收费
  • 英文网站网站建设wordpress外贸网站源码
  • 自己开发网站怎么开发漯河企业网站开发
  • 哪些人可以做网站游戏分销代理平台
  • 个人站长还有什么类型的网站可以做驻马店网站建设价格
  • 物流网站建设哪个好上海十大黑心装修公司
  • 买国外空间哪个网站好可以发广告的网站
  • 站长工具樱花网站开发有哪几个阶段
  • 做网站找合作伙伴Wordpress怎么改成中文
  • 制作网站公司图片建设银行信用卡网站多少
  • 中国建设银行个人登陆网站每年网站备案抽查
  • wp如何做引擎网站wordpress 多备份
  • 营销型网站开发公司电话WordPress 卡密购买插件
  • 做黎川旅游网站的目的常州外贸网站
  • 网站设计原型图关于加强网站建设与管理的通知
  • 网站排名下降的原因哈尔滨高端网页设计
  • 我自己的网站 怎样做防火墙企业公示信息年报
  • 网站建设redu网站建设的构思
  • 站长工具网站推广域名备案网站服务内容
  • 做服装搭配图的网站有哪些房地产销售计划
  • 数据显示网站模板wordpress 短信
  • com网站域名羊毛网站建设视频
  • 专门做h5的网站做网站开发需要学那些东西
  • 做名片去哪个网站池州专业网站建设哪家好
  • 一级a做爰片免费网站迅雷下载aliyun oss wordpress