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

Source: Internet
Author: User
"225-implement stack using queues (stack operations with queues)" " leetcode-interview algorithm classic-java Implementation" "All topic Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original title

Implement the following operations of a stack using queues.
Push (x) –push element x onto stack. The
Pop () –removes the element on top of the stack. The
Top () –get is the top element. The
Empty () –return whether the stack is empty.
Notes:
You must use only standard operations of a queue–which means only push to back, peek/pop from front , size, and is empty operations are valid.
Depending on your language, the queue may isn't be supported natively. You could simulate a queue by using a list or deque (double-ended queue) as long as your use only standard operations of a Q Ueue.
You may assume this all operations are valid (for example, no POPs or top operations'll be called on a empty Stac k).
title effect

Using queues to implement stack operations
Push (x) – element into stack
Pop () – element out of stack
Top () – The value of the topmost element of the stack
Empty () – Determines whether the stack is empty
Attention:
Can only use the standard operation of the queue, FIFO, the number of elements to determine the queue is empty
Because of programming language, some languages do not touch the queue and can be replaced with a linked list or a two-way list, but only standard queue operations can be used
You can assume that all operations are legal, that is, when the queue is empty, there is no element out of the stack and the top element of the stack is asked to operate
ideas for solving problems

Use two queues to simulate a stack
Code Implementation

Algorithm implementation class

Import java.util.LinkedList;

Import java.util.List; public class Mystack {//Maintain two queues, where a queue is always empty, ready for pop and top operations private list<integer> alist = new LINKEDLIST&LT;&G
    t; ();


    Private list<integer> blist = new linkedlist<> ();
    Push element x onto stack.
        public void push (int x) {//If alist is not NULL, add X to Alist if (!alist.isempty ()) {alist.add (x);
        //Otherwise add total to blist else {blist.add (x);
    }//Removes the element on top of the stack.  At least one of the public void Pop () {//Two queues is empty, alist is set to non-null if (Alist.isempty ()) {list<integer> tmp
            = Blist;
            Blist = alist;
        Alist = tmp;
        ///Except the last element is transferred to Blist while (Alist.size () > 1) {blist.add (alist.remove (0));
    //Delete the last element (corresponding to the stack top element of the stack) alist.clear ();
    }//Get the top element. public int Top () {//Two queues have at least oneNull to set the Alist if (Alist.isempty ()) {list<integer> tmp = blist;
            Blist = alist;
        Alist = tmp;
        ///Except the last element is transferred to Blist while (Alist.size () > 1) {blist.add (alist.remove (0));
        } blist.add (Alist.get (0));
    Return Alist.remove (0);
    }//Return whether the stack is empty.
    public Boolean empty () {return alist.isempty () && blist.isempty (); }
}
Evaluation Results

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

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

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.