当前位置: 首页 > 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/965970/

相关文章:

  • 新网站seo技术网站改版多久恢复
  • 做瞹瞹网站wordpress api 中文文档
  • 邯郸做网站就找安联网络超酷个人网站欣赏
  • 网站设计的含义wordpress自动推送给百度
  • 如何创建网站服务器地址单仁牛商
  • 珠海市住房和城乡建设局网站威海外贸网站建设怎么样
  • 企业网站建设基本步骤网站 测速度
  • 高端定制网站设计公司深圳有哪些软件开发公司
  • 烟台建站模板源码精品课程网站开发平台
  • 百度商桥 手机网站网站建设备案要哪些
  • 网站空间一年多少钱开源众包
  • 江宁建设局网站域名去掉wordpress
  • 主页值得是网站的主要内容所在页广州的房地产网站建设
  • 好用的在线设计网站抄袭网站
  • 平顶山高端网站建设移动互联网的应用论文
  • 成都手工活外发加工网深圳搜索引擎优化推广
  • win10运行wordpress网站优化要怎么做
  • 广州沙河一起做网站wordpress the_content() 截取
  • 排名轻松seo 网站推广极速建站
  • 用文本文档做网站健身网站开发过程中遇到的麻烦
  • 商城微网站如何做邢台123信息最新招聘信息
  • 宁波做网站的大公司网站推广是做什么的
  • 仿牌网站服务器微信公众号怎么创建免费注册
  • 国外自助建站免费建站平台编程培训机构出来的能找到工作吗
  • 单位网站建设维护论文私有云笔记 wordpress
  • 崇左seo泰安seo排名
  • 课件ppt模板免费下载网站企业网站建设应具备的功能
  • 嘉兴做网站软件关于集团网站建设申请
  • 网站建设的售后access 做网站 出现问题
  • 帝国cms生成网站地图网站建设合同 法律声明