Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Java queues to implement the stack

Shulou Source: shulou.com Published: 2022-06-01 11:23:32 10月01日 Update

This article introduces the knowledge of "how to use Java queue to implement stack". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic: use queues to implement the stack. You can only use some simple queue operations, such as push (x), pop (), top (), empty (). I am using a queue to implement a stack.

Language:cpp

Class MyStack {public:/** Initialize your data structure here. * / MyStack () {} queue que; / * * Push element x onto stack. * / void push (int x) {que.push (x); for (int iTuno; I < que.size ()-1; iTunes +) {que.push (que.front ()); que.pop ();}} / * * Removes the element on top of the stack and returns that element. * / int pop () {int I = que.front (); que.pop (); return I;} / * * Get the top element. * / int top () {return que.front ();} / * * Returns whether the stack is empty. * / bool empty () {return que.empty ();}}; / * 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 (); * bool param_4 = obj.empty (); * / "how to use Java queue to implement stack" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Queue content more knowledge practical successful next dilemma reality situation article case editing website industry process topic high quality learning related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux OPPO Reno Microsoft MySQL