当前位置:网站首页>Jianzhi offer 09 realizes queue with two stacks
Jianzhi offer 09 realizes queue with two stacks
2022-07-04 08:55:00 【Sean_ Asu】
At first, I didn't understand the meaning of the question , Only when I saw the vernacular explanation in the explanation of the question did I understand .
You should inspect the stack ( First in, then out )、 queue ( fifo ) Characteristics of .
here 2 Stack , My understanding is , The first is the function stack , The second is the auxiliary stack , According to the characteristics of stack in and out , Use two stacks to realize the function of queue .
Personal summary is , First, press the number into the first stack , here Stack 1 It's empty , Stack 2 It's empty , For example, enter in turn 1、3、5 Three numbers , Now the stack 1、 Stack 2 As shown in the figure


If you want to delete the header element at this time , That is, the first number to enter :1, Then you need to stack 1 Three elements of , Pop up one by one (pop), Push to stack 2(push) in , Now the stack 1 It's empty , Stack 2 Not empty , According to the principle of first in, then out , Stack 2 As shown in the figure

And then put 1 eject (pop), In this way, you can delete the header element
therefore , If it is an implementation to add elements , Then all elements are pushed onto the stack first 1
Then the stack should be considered when deleting elements 2 The situation of , Because you want to delete the header element , Must be stack 1 The element pops up first , Then push it into the stack 2, Now the stack 2 Not empty , Note that all elements are on the stack 2, The deletion operation can be directly executed in sequence , There's no need to think otherwise .
According to the meaning , If the stack 1、2 All empty. , Delete operation returns -1
Consider stack 1、 Stack 2 The situation of
①、 Stack 2 It's empty 、 Stack 1 Not empty
This situation is called stack 1 Press in new elements , Now the stack 1、 Stack 2 As shown in the figure

Then the header element becomes 7, To delete 7, First, put the stack 2 Element pop-up push stack 1, Then put the stack 1 Element pop-up push stack 2, that 7 It's on the stack 2 At the top of the , You can delete .
Stack 2 Elements pop up and push onto the stack in turn 1:

Stack 1 Elements pop up and push onto the stack in turn 2:

②、 Stack 1 It's empty 、 Stack 2 Not empty
Pop up the stack directly 2 Top element of stack , Delete it
So far, the solution of personal thinking is over , Write the code according to the idea
class CQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
// Stack 2 It's empty
if (stack2.isEmpty()) {
// Stack 2 It's empty 、 Stack 1 Not empty
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
// Stack 2 empty 、 Stack 1 empty
if (stack2.isEmpty()) {
return -1;
} else { // Stack 2 Not empty
return stack2.pop();
}
}
}isEmpty() Determine whether it is null , If the stack is empty , Then return to true, If the stack contains elements , Then return to false
边栏推荐
- Relationship and operation of random events
- awk从入门到入土(15)awk执行外部命令
- The old-fashioned synchronized lock optimization will make it clear to you at once!
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Clion console output Chinese garbled code
- There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
- awk从入门到入土(7)条件语句
- Newh3c - routing protocol (RIP, OSPF)
- swatch
- Newh3c - network address translation (NAT)
猜你喜欢

Educational Codeforces Round 115 (Rated for Div. 2)

Démarrage des microservices: passerelle

微服务入门:Gateway网关
![C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)

FOC control

How to choose solid state hard disk and mechanical hard disk in computer

Newh3c - routing protocol (RIP, OSPF)
![[attack and defense world | WP] cat](/img/01/c5cacfdcca511d4b523611abca6eef.jpg)
[attack and defense world | WP] cat

ctfshow web255 web 256 web257

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
随机推荐
Clion console output Chinese garbled code
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Codeforces Global Round 21(A-E)
C语言-入门-基础-语法-数据类型(四)
DM8 uses different databases to archive and recover after multiple failures
随机事件的关系与运算
ArcGIS application (XXII) ArcMap loading lidar Las format data
How college students choose suitable computers
Flutter 集成 amap_flutter_location
awk从入门到入土(11)awk getline函数详解
MySQL relearn 1-centos install mysql5.7
CLion-控制台输出中文乱码
[Chongqing Guangdong education] National Open University spring 2019 455 logistics practice reference questions
How can we make a monthly income of more than 10000? We media people with low income come and have a look
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
Educational Codeforces Round 115 (Rated for Div. 2)
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
The basic syntax of mermaid in typera
根据数字显示中文汉字
manjaro安装微信