public class StackToQueue {
private Stack<Integer> stack1 = new Stack<>();
private Stack<Integer> stack2 = new Stack<>();
public void add(Integer value) {
//向stack1中添加元素
stack1.push(value);
System.out.println("添加元素成功");
}
public void remove() {
//先判断stack2是否为空,不为空直接pop
if (!stack2.isEmpty()) {
stack2.pop();
System.out.println("删除元素成功");
} else if (!stack1.isEmpty() && stack2.isEmpty()) {//如果stack2为空,stack1不为了,将stack1的元素逐个压入stack2,然后再pop stack2
while (!stack1.isEmpty()) {
int val = stack1.pop();
stack2.push(val);
}
stack2.pop();
System.out.println("删除元素成功");
} else {
System.out.println("队列为空");
}
}
public void printQueue(){
if (stack1.isEmpty()&&stack2.isEmpty()){
System.out.println("队列为空");
return;
}
System.out.println("按存入顺序打印:");
while (!stack2.isEmpty()){
System.out.println(stack2.pop());
}
while (!stack1.isEmpty()){
int val=stack1.pop();
stack2.push(val);
}
while (!stack2.isEmpty()){
System.out.println(stack2.pop());
}
}
public static void main(String[] args){
StackToQueue stackToQueue=new StackToQueue();
stackToQueue.add(10);
stackToQueue.add(6);
stackToQueue.add(9);
// stackToQueue.printQueue();
stackToQueue.remove();
// stackToQueue.printQueue();
stackToQueue.add(4);
stackToQueue.add(7);
stackToQueue.printQueue();
}
}
github代码地址:https://github.com/iot-wangshuyu/offer/blob/master/code/src/StackToQueue.java
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/15810.html