当前位置:网站首页>Daily question 1: force deduction: 225: realize stack with queue
Daily question 1: force deduction: 225: realize stack with queue
2022-07-06 22:21:00 【Flying cats don't eat fish】
subject

** analysis : Use two queues to realize the structure of stack , We need to q1 and q2,q1 Keep consistent with the stack at all times ,q2 Used to play an auxiliary role . The only difference between the two is the difference of adding elements , The order of queue and stack should be opposite , therefore ,q2 The role of , Whenever an element is added to the stack ,q2 Add the element , And then q1 The elements in are queued out in turn , Enter again q2. Let's talk about it again q2 and q1 In exchange for , In order to make sure q1 Keep consistent with the elements in the stack at all times .**
class MyStack {
public MyStack() {
}
Queue <Integer> q1 = new LinkedList<>();
Queue <Integer> q2 = new LinkedList<>();
public void push(int x) {
q2.offer(x);
while(!q1.isEmpty()){
q2.offer(q1.poll());
}
Queue <Integer> temp;
temp = q2;
q2 = q1;
q1 = temp;
}
public int pop() {
return q1.poll();
}
public int top() {
return q1.peek();
}
public boolean empty() {
return q1.isEmpty();
}
}
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
边栏推荐
猜你喜欢

2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据

GPS从入门到放弃(十二)、 多普勒定速

GPS从入门到放弃(十九)、精密星历(sp3格式)

Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices

GPS from entry to abandonment (XIV), ionospheric delay

2500 common Chinese characters + 130 common Chinese and English characters

Oracle control file and log file management
![[10:00 public class]: basis and practice of video quality evaluation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[10:00 public class]: basis and practice of video quality evaluation

0 basic learning C language - interrupt
随机推荐
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
Chapter 4: talk about class loader again
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
ZABBIX proxy server and ZABBIX SNMP monitoring
Powerful domestic API management tool
Common sense: what is "preservation" in insurance?
MongoDB(三)——CRUD
Attack and defense world ditf Misc
GPS从入门到放弃(十二)、 多普勒定速
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
GPS from getting started to giving up (XX), antenna offset
Barcodex (ActiveX print control) v5.3.0.80 free version
第4章:再谈类的加载器
3DMAX assign face map
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
OpenCV VideoCapture. Get() parameter details
Memorabilia of domestic database in June 2022 - ink Sky Wheel
十二、启动流程
Four data streams of grpc
OpenCV300 CMake生成project在项目过程中的问题