In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the method of realizing queue with stack". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "the method of realizing queue with stack".
Use the stack to implement the following actions of the queue:
Push (x)-places an element at the end of the queue.
Pop ()-removes the element from the header of the queue.
Peek ()-- returns the element at the head of the queue.
Empty ()-returns whether the queue is empty.
Example:
MyQueue queue = new MyQueue ()
Queue.push (1)
Queue.push (2)
Queue.peek (); / / returns 1
Queue.pop (); / / returns 1
Queue.empty (); / / returns false
Description:
You can only use standard stack operations-that is, only push to top, peek/pop from top, size, and is empty operations are legal.
The language you are using may not support stacks. You can use list or deque (double-ended queue) to simulate a stack, as long as it is a standard stack operation.
Assume that all operations are valid (for example, an empty queue does not invoke pop or peek operations).
Try to solve the problem by yourself, ok
Public class MyQueue {Stack S1 = new Stack (); Stack S2 = new Stack (); / * Initialize your data structure here. * / public MyQueue () {} / * * Push element x to the back of queue. * / public void push (int x) {s1.push (x);} / * * Removes the element from in front of queue and returns that element. * / public int pop () {while (! s1.isEmpty ()) {s2.push (s1.pop ());} int I = s2.pop (); while (! s2.isEmpty ()) {s1.push (s2.pop ());} return I;} / * * Get the front element. * / public int peek () {while (! s1.isEmpty ()) {s2.push (s1.pop ());} int I = s2.peek (); while (! s2.isEmpty ()) {s1.push (s2.pop ());} return I;} / * * Returns whether the queue is empty. * / public boolean empty () {return s1.isEmpty ();}} Thank you for your reading. The above is the content of "how to implement queues with stacks". After the study of this article, I believe you have a deeper understanding of the method of implementing queues with stacks, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.