当前位置:网站首页>Implement a queue with two stacks.
Implement a queue with two stacks.
2022-07-24 07:40:00 【yn20000227】
The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Input :
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
Output :[null,null,3,-1]
Input :
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
class CQueue {
private:
stack<int> instack;
stack<int> outstack;/* An input stack , One as an output stack */
public:
CQueue() {
}
void appendTail(int value) {
instack.push(value);
}
int deleteHead() {
if(!outstack.empty()){
int value = outstack.top();
outstack.pop();
return value;
}
else{
while(!instack.empty()){
int top = instack.top();
instack.pop();
outstack.push(top);
}
if(outstack.empty()){
return -1;
}else{
int output = outstack.top();
outstack.pop();
return output;
}
}
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
边栏推荐
- Arduino's super power-saving sleep mode has worked with one 18650 battery for 17 years
- 【FreeRTOS】11 软件定时器
- Arduino 超级省电之休眠模式用1节18650电池工作17年
- FlinkSQL-UDF自定义数据源
- php 转义字符串
- JS_ Realize the separation of multiple lines of text into an array according to the newline
- [introduction to C language] zzulioj 1011-1015
- Notes on the basics of using parameters in libsvm (1)
- Buffer overflow vulnerability of network security module B in national vocational college skills competition
- HCIP第十天笔记
猜你喜欢

mysql查询当前节点的所有父级

Advanced part of C language I. data storage

Selenium basic knowledge automatic search

php 转义字符串

全国职业院校技能大赛网络安全B模块 Windows操作系统渗透测试

C language to achieve mine sweeping game

Mitre att & CK ultra detailed learning notes-02 (a large number of cases)

Using depth and normal textures in unity

Influxdb未授权访问&CouchDB权限绕过

Vulnhub DC1
随机推荐
R语言手写数字识别
Requests crawler implements a simple web page collector
从零开始C语言精讲篇3:函数
【sklearn】PCA
Selenium basics controls the scroll bar of the browser
requests-爬虫实现一个简易网页采集器
Amber tutorial A17 learning - concept
requests-爬虫多页爬取肯德基餐厅位置
PHP link log scheme
【信息系统项目管理师】第七章 复盘成本管理知识架构
Introduction to C language v First understanding pointer VI. first understanding structure
Arduino在不同主频下稳定支持的TTL串口速率
Log in to the server using the fortress machine (springboard machine)
无法自动装配,未找到“RedisTemplate类型的Bean
Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
爬虫学习-概述
numpy.concatenate
Buddy: core function entry
【Pytorch】Dataset_ DataLoader
Jenkins detailed deployment