One , Two stacks implement a queue .

        Thinking of problem solving : The characteristic of the stack is that it comes in first and then comes out , The characteristics of the queue are first in and then out , So we can put the queue 1 Elements in pop To queue 2 in , queue 2 When you take it out again .
/** * Two stacks implement a queue : * thinking : Save the data to the first stack , Then all the elements in the first stack are put out of the stack to the second stack , The second stack comes out , First in, first out *
extend : The stack is Vector A subclass of , It implements a standard LIFO stack . * The stack only defines the default constructor , Used to create an empty stack .
The stack consists of Vector All methods defined , It also defines some of its own methods . * common method : * boolean empty(), Test whether the stack is empty . * Object
peek(), Look at the objects at the top of the stack , But it is not removed from the stack . * Object pop(), Remove the object at the top of the stack , And returns the object as the value of this function . * Object
push(Object element), Push an item to the top of the stack . * int search(Object element), Returns the position of the object in the stack , with 1
Is the base number , The position of the top of the stack is 1. */ public class StackToQueen<T> { private Stack<T> stack1 = new
Stack<>(); private Stack<T> stack2 = new Stack<>(); /** * Add data * @param num */
public void add(T num){ stack1.push(num); } /** * pop Output data * @param */ public
void poll(){ if(stack1.isEmpty() && stack2.isEmpty()){ return; }
// take stack1 Put the stack2 while (stack1.size() > 0){ stack2.push(stack1.pop()); }
return; } public T get(){ if(stack2.isEmpty()){ return null; } return
stack2.pop(); } public static void main(String[] args) { StackToQueen<Integer>
stackToQueen = new StackToQueen(); stackToQueen.add(1); stackToQueen.add(2);
stackToQueen.add(3); stackToQueen.add(4); stackToQueen.add(5);
stackToQueen.poll(); System.out.print(stackToQueen.get());
System.out.print(stackToQueen.get()); System.out.print(stackToQueen.get());
System.out.print(stackToQueen.get()); System.out.print(stackToQueen.get()); } }
Two , Two queues implement one stack .

        Thinking of problem solving : Except for the last number in queue 1, all the others are imported into queue 2 and returned to the elements in queue 1 , And then do the same for queue two .
/** * Two queues implement one stack * thinking : All the numbers except the last one in queue 1 are imported into queue 2 , It then returns the elements in the queue , And then do the same for queue two . *
Quenu Common methods of : * poll()    Gets and removes the header of this queue , If this queue is empty , Return to null *
remove()   Gets and removes the header of this queue , If this queue is empty , Then throw out NoSuchElementException abnormal * *
peek()   Gets the head of the queue without removing the header of the queue . If this queue is empty , Return to null *
element()   Gets the head of the queue without removing the header of the queue . If this queue is empty , Will be thrown NoSuchElementException abnormal * *
offer()   This element inserts the specified queue ( If it is immediately feasible and does not violate the capacity limit ), Insert successful return true; Otherwise return false. *
When using a capacity constrained queue ,offer Methods are usually better than add method ——add Method may not be able to insert elements , And just throw one IllegalStateException abnormal
* add()   Inserts the specified element into this queue */ public class QueenToStack<T> { Queue<T> queue1 = new
LinkedList<>(); Queue<T> queue2 = new LinkedList<>(); // add to public void push(T
num){ queue1.offer(num); } // take out public T pop(){ if (queue1.isEmpty() &&
queue2.isEmpty()) { throw new RuntimeException("queue is empty"); } if
(!queue1.isEmpty() && queue2.isEmpty()) { while (queue1.size() > 1) {
queue2.offer(queue1.poll()); } return queue1.poll(); } if (queue1.isEmpty() &&
!queue2.isEmpty()) { while (queue2.size() > 1) { queue1.offer(queue2.poll()); }
return queue2.poll(); } return null; } public static void main(String[] args) {
QueenToStack<Integer> queenToStack = new QueenToStack(); queenToStack.push(1);
queenToStack.push(2); queenToStack.push(3); queenToStack.push(4);
queenToStack.push(5); System.out.print(queenToStack.pop());
System.out.print(queenToStack.pop()); System.out.print(queenToStack.pop());
System.out.print(queenToStack.pop()); System.out.print(queenToStack.pop()); } }
 

Technology
©2019-2020 Toolsou All rights reserved,
Python Garbage collection and memory leak hive Summary of processing methods for a large number of small files The difference between memory overflow and memory leak , Causes and Solutions Create data mysql Library process You don't know ——HarmonyOS stay Vue Use in Web WorkerSparkSQL Achieve partition overlay write msf Generate Trojan horse attack android mobile phone Linux Page replacement algorithm C Language implementation Django Personal blog building tutorial --- Time classified archiving