题目:
题1:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
题2:用两个队列来实现一个栈,完成栈的add和remove操作。 栈中的元素为int类型
知识点:
- 队列的基本知识,创建空队列、add、remove、isempty等;
- 栈的基本知识,创建空栈、pop、empty、push等; 参考:https://blog.csdn.net/qq_42124842/article/details/91420306
- 静态static:参考:https://baijiahao.baidu.com/s?id=1636927461989417537&wfr=spider&for=pc
- 静态方法只能访问静态成员(非静态即可访问静态也可非静态)
- 静态方法中不能用this,super(静态方法是类变量,不是实例变量)
- 主函数是静态的;
注意:
- 创建空队列:Queue<Integer> q1= new LinkedList<Integer>();
- 创建空栈:Stack<Integer> stack1 = new Stack<>();
代码实现:
- 题1:使用两个栈来互相倒,从而实现队列的先进先出,及删除队头元素,主要看stack2
- 题2:将除要删除的元素都移至q2,将删除元素从q1中弹出后,再将q2元素移至q1,主要看q1
package jzoffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import javax.management.RuntimeErrorException;
import org.omg.CORBA.PUBLIC_MEMBER;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;
import jdk.internal.dynalink.beans.StaticClass;
public class Num09 {
public static void main(String[] args) {
//测试1
StackImpQueue queue = new StackImpQueue();
//添加元素
queue.appendTail(1);
queue.appendTail(2);
queue.appendTail(5);
queue.appendTail(7);
queue.appendTail(9);
//删除队头元素
queue.deleteHead();
//打印队列元素
queue.showQueue();
//测试2
QueueImpStack stack = new QueueImpStack();
//添加元素
stack.appendTail(1);
stack.appendTail(2);
stack.appendTail(3);
stack.appendTail(4);
stack.appendTail(5);
//删除队头元素
stack.deleteHead();
//打印队列元素
stack.showStack();
}
//栈实现队列
public static class StackImpQueue{
public Stack<Integer> stack1 = new Stack<>();
public Stack<Integer> stack2 = new Stack<>();
public void appendTail(Integer a) {
stack1.push(a);
}
/**
* 打印队列元素
*/
public void showQueue() {
while(!stack1.empty()) {
Integer output1 = stack1.pop();
stack2.push(output1);
}
while(!stack2.empty()) {
System.out.print(stack2.pop()+ " ");
}
}
/**
* 删除队头元素
* @return
*/
public int deleteHead() {
//若stack1、2均空
if(stack1.empty() && stack2.empty()) {
throw new RuntimeException("队列为空");
//若stack1不空、2空
}else if(stack2.empty()) {
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
//队列实现栈
public static class QueueImpStack{
private Queue<Integer> q1= new LinkedList<Integer>();
private Queue<Integer> q2= new LinkedList<Integer>();
/**
* 添加元素
* @param data
*/
public void appendTail(int data) {
q1.add(data);
}
/**
* 删除元素
*/
public void deleteHead() {
if(q1.isEmpty() && q2.isEmpty()) {
throw new RuntimeException("栈 为空");
}
while(q1.size()>1) {
Integer output1 = q1.remove();
q2.add(output1);
}
q1.remove();
while(!q2.isEmpty()) {
q1.add(q2.remove());
}
}
/**
* 打印
*/
public void showStack() {
ArrayList<Integer> ls = new ArrayList<Integer>();
while(!q1.isEmpty()) {
ls.add(q1.remove());
}
for(int i=ls.size()-1; i>=0; i--) {
System.out.println(ls.get(i));
}
}
}
}
代码实现(C++):
stack<int> stack1;
stack<int> stack2;
void push(int node) {
stack1.push(node);
}
int pop() {
int res;
//当stack2有值先弹
if (!stack2.empty())
{
res = stack2.top();
stack2.pop();
}
//stack2为空,stack1不为空
else if(!stack1.empty())
{
//将stack1的值压入stack2
while (!stack1.empty())
{
int num = stack1.top();
stack2.push(num);
stack1.pop();
}
res = stack2.top();
stack2.pop();
}
return res;
}