当前位置:网站首页>Using two stacks to realize the function of one queue?
Using two stacks to realize the function of one queue?
2022-06-25 21:14:00 【Rabbit cloud program】
This question is often given when learning data structures for beginners , I never expected to enter the society , I found that many large factories like to use various data structures to enable you to implement another data structure . Just understand the characteristics of stacks and queues , To realize the function of queue , That is, the composite use of stack functions , There are two stacks that can be combined twice to implement queues .
features
Stack :FILO First in, then out .
queue :FIFO fifo
Ideas
Use two stacks s1 and s2 When simulating a queue ,s1 As input stack , Stack elements one by one , This simulates the queueing of queue elements .
When you need to get out of the team , Stack s1 Step back and push into the stack one by one s2 in ,s1 The first element in the stack , stay s2 At the top of the stack .
s2 Backstack , It is equivalent to leaving the queue , First in, first out .
obviously , Only stack s2 Empty and s1 Also empty , The queue is empty .
Code
Suppose two stacks A and B, And all are empty .
It can be thought of as a stack A To provide the function of queuing , Stack B Provide the function of queue .
Queue entry : Push A
Outgoing queue :
1 If the stack B Not empty , Pop up the stack directly B The data of .
2 If the stack B It's empty
2.1 if A Not empty , Then pop up the stack in turn A The data of , Put in the stack B in , Then pop up the stack B The data of .
2.1 if A It's empty , Then the queue is empty .
Critical code
边栏推荐
- 银河证券靠谱吗?开证券账户安全吗?
- Instant aesthetics of the Centennial Olympic Games: beauty in the air, condensed in minutes and seconds - Alibaba cloud video cloud AI editorial department "cloud smart scissors"
- [deep learning series] - visual interpretation of neural network
- Alicloud disk mounted locally
- Analysis and cleaning of kdevtmpfsi virus content
- Openocd compilation and installation
- COMP9024
- JS__ This, arguments, cloning, ternary operator__ Duyi
- Mysqldumpslow out slow statements in the database
- JS__ Inheritance mode, namespace, object enumeration__ Duyi
猜你喜欢
js(3)
The beginning of manjaro's journey
What is a server? (Powercert animated videos)
Unable to connect to the server remotely locally using the Jupiter notebook
Live broadcast preview | front line experts invite you to talk: the value of data science enabling multiple application scenarios
Beginner to embedded development
[buucry] sensor (Manchester code)
OLED driven learning based on ssd1306 (II): addressing mode of ssd1306
Alicloud disk mounted locally
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
随机推荐
Xshell mouse configuration
Illustration tcp/ip - Chapter 1 and 2 Notes
IPtables
What is an app circle of friends advertisement
OBD Position Overview
Win10 common software
Lantern Festival, learning at the right time! Novice training camp attacks again, learning buff continues to fill up
Get parameters in URL
2022 "gold, silver and four" is a must for job hopping. You must know 100 questions in 2022 intermediate and advanced Android interview to realize your big factory dream
IPv4 and IPv6 (powercert animated videos)
New generation engineers teach you how to play with alluxio + ml (Part 2)
Day 28/100 CI CD basic introductory concepts
Mysqldumpslow out slow statements in the database
Yolov4 reading notes (with mind map)! YOLOv4: Optimal Speed and Accuracy of Object Detection
Explain memcached principle in detail
Causes and solutions of unreliable JS timer execution
The user name and password will be automatically filled in when adding a form
couldn‘t be accessed by user ‘_ apt‘
Is it safe for qiniu school to open a securities account?
2022年启牛学堂证券开户安全嘛?