"Leetcode-Interview algorithm classic-java Implementation" "225-implement stack using Queues (stack operation with queues)"

Source: Internet
Author: User

"225-implement stack using Queues (stack operation with queues)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original Question

Implement the following operations of a stack using queues.
Push (x) –push element x onto stack.
Pop () –removes the element on top of the stack.
Top () –get the top element.
Empty () –return whether the stack is empty.
Notes:
You must the only standard operations of a queue–which means only push-to-back, peek/pop from front, size, and is empty Operations is valid.
Depending on your language, queue May is not supported natively. You could simulate a queue by using a list or deque (double-ended queue), as long as if you have standard operations of a Q Ueue.
You may assume this all operations is valid (for example, no pop or top operations would be called on an empty stack).

Main Topic

Using queues to implement stack operations
Push (x) – element into the stack
Pop () – element out of stack
Top () – Take the top element value of the stack
Empty () – Determine if the stack is empty
Attention:
Can only use the standard operation of the queue, FIFO, the number of queue elements, determine whether the queue is empty
Some languages do not caress queues for programming language reasons, and can be replaced with linked lists or doubly linked lists, but only with standard queue operations
You can assume that all operations are legal, that is, when the queue is empty, there will be no element out of the stack and the operation of the top element of the stack

Thinking of solving problems

Use two queues to simulate a stack

Code Implementation

Algorithm implementation class

ImportJava.util.LinkedList;ImportJava.util.List; Public  class mystack {    //Maintain two queues, where there is always one queue empty, ready for pop and top operations    PrivateList<integer> alist =NewLinkedlist<> ();Privatelist<integer> blist =NewLinkedlist<> ();//Push element x onto stack.     Public void Push(intx) {//If alist is not empty, add X to Alist        if(!alist.isempty ())        {Alist.add (x); }//Otherwise always added to Blist        Else{Blist.add (x); }    }//Removes the element on top of the stack.     Public void Pop() {//Two at least one of the queues is empty, set alist to non-empty        if(Alist.isempty ())            {list<integer> tmp = blist;            Blist = alist;        Alist = tmp; }//Transfer to blist except for the last element         while(Alist.size () >1) {Blist.add (Alist.remove (0)); }//Delete the last element (corresponding to the stack top element)Alist.clear (); }//Get the top element.     Public int Top() {//Two at least one of the queues is empty, set alist to non-empty        if(Alist.isempty ())            {list<integer> tmp = blist;            Blist = alist;        Alist = tmp; }//Transfer to blist except for the last element         while(Alist.size () >1) {Blist.add (Alist.remove (0)); } Blist.add (Alist.get (0));returnAlist.remove (0); }//Return whether the stack is empty.     Public Boolean Empty() {returnAlist.isempty () && blist.isempty (); }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48084069"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "225-implement stack using Queues (stack operation with queues)"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.