如何获取网站建设培训资料和报价单?
摘要:网站建设培训资料,网站建设报价单 文库,商业软文怎么写,网站测速工具请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty)&
网站建设培训资料,网站建设报价单 文库,商业软文怎么写,网站测速工具请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作#xff08;push、pop、peek、empty#xff09;#xff1a;实现 MyQueue 类#xff1a;void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元…请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作push、pop、peek、empty实现 MyQueue 类void push(int x) 将元素 x 推到队列的末尾int pop() 从队列的开头移除并返回元素int peek() 返回队列开头的元素boolean empty() 如果队列为空返回 true 否则返回 false说明1、你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。2、你所使用的语言也许不支持栈。你可以使用 list 或者 deque双端队列来模拟一个栈只要是标准的栈操作即可。 示例 1输入[MyQueue, push, push, peek, pop, empty][[], [1], [2], [], [], []]输出[null, null, null, 1, 1, false]解释MyQueue myQueue new MyQueue();myQueue.push(1); // queue is: [1]myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)myQueue.peek(); // return 1myQueue.pop(); // return 1, queue is [2]myQueue.empty(); // return false 提示1、1 x 92、最多调用 100 次 push、pop、peek 和 empty3、假设所有操作都是有效的 例如一个空的队列不会调用 pop 或者 peek 操作思路empty方法如果两个栈都为空则队列为空push方法均向栈1压栈pop方法将栈1的所有元素出栈然后入栈2栈2pop的元素就是要出的元素peek方法pop方法不出栈peek代码class MyQueue {private StackInteger stack1;private StackInteger stack2;public MyQueue() {stack1new Stack();stack2new Stack();}public void push(int x) {stack1.push(x);}public int pop() {if(stack2.isEmpty()){while(!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.pop();public int peek() {if(stack2.isEmpty()){while(!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.peek();}public boolean empty() {return stack1.empty()stack2.empty();}
}
