두 큐 구현 스택은 두 스택은 대기
1 개의 스택 큐 구현
두 개의 큐 구현 사항 : 스택 푸시 (), 팝업 ().
참고 단계 :
(1) 종래의 2 대기열 Q1 및 Q2는 분기 스택은 소자에인가 될
제 해석 스택이 비어 Q1, Q1인지 요소가 항상 후 LIFO 올 때문에 때를 제외하고, (2) 큐의 마지막 요소는 Q2에 다른 요소를 추가, 최종 소자 (Q1)의 디큐
스택 (3) 밖으로 때 판정 Q1 2가 Q2 다른 요소를 추가하고, 마지막으로 소자 (Q2)를 제외하고 비어 있다면 Q1, 팀의 2 분기 후 마지막 요소
/**
* 两个队列实现栈
*
* @author Linlin Zhao
*
*/
public class StackAndQueue02 {
public Queue<Integer> queue1;
public Queue<Integer> queue2;
public StackAndQueue02() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
/**
* 压栈(入队queue1)
*
* @param num
* @return
*/
public void push(int num) {
queue1.offer(num);
}
/**
* 弹栈
* @return
*/
public Integer pop() {
if (queue1.isEmpty() && queue2.isEmpty()) {
return null;
}
// 先判断 q1 是否为空
if (!queue1.isEmpty()) {
int size = queue1.size();
for (int i = 0; i < size - 1; i++) {//其他元素转移
queue2.offer(queue1.poll());
}
return queue1.poll();//最后一个元素出队
} else {
int size = queue2.size();
for (int i = 0; i < size - 1; i++) {
queue1.offer(queue2.poll());
}
return queue2.poll();
}
}
public static void main(String[] args) {
StackAndQueue02 myStack=new StackAndQueue02();
myStack.push(3);
myStack.push(6);
myStack.push(8);
myStack.push(3);
myStack.push(2);
myStack.push(0);
myStack.push(1);
System.out. println(myStack.pop());
myStack.pop();
System.out. println(myStack.pop());
}
}
2 개의 스택 큐
구현이 스택을 사용하여 추가 큐를 (), 여론 조사 (), PEEK ().
참고 단계 :
(1) 두 스택 stack1 stack2하고있다
라인에서만 stack1 첨가제 요소 큐 경우 (2)
제 1 대기열이 비어 (3) 결정된 stack2이고, 요소 stack2가 FIFO에 진보 . stack2가 비어 있지 않은 경우, 팝업 상위 요소 stack2을 지시. 당신이 stack2에 추가하는 경우 비어, 다음 stack1 요소는 다음 상단 요소 stack2 팝
/**
* 两个栈实现队列
*
*/
public class StackAndQueue {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
public StackAndQueue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
// 入队列
public boolean add(int num) {
stack1.push(num);
return true;
}
// 获取队首元素
public int peek() {
while (!stack2.isEmpty()) {
return stack2.pop();
}
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.peek();
}
// 获取队首元素,并移除
public int poll() {
while (!stack2.isEmpty()) {
return stack2.pop();
}
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
public static void main(String[] args) {
StackAndQueue myQueue =new StackAndQueue();
myQueue.add(9);
myQueue.add(5);
myQueue.add(8);
myQueue.add(1);
myQueue.add(3);
myQueue.add(2);
System.out.println(myQueue.peek());
System.out.println(myQueue.poll());
System.out.println(myQueue.poll());
}
}
3 Dueue 아날로그 구현 스택
/**
* 使用队列实现自定义栈
* 1、弹栈
* 2、压栈
* 3、获取头
* @author Linlin Zhao
*
*/
public class MyStack01<E> {
//容器
private Deque<E> container =new ArrayDeque<E>();
//容量
private int cap;
public MyStack01(int cap) {
super();
this.cap = cap;
}
//压栈
public boolean push(E e){
if(container.size()+1>cap){
return false;
}
return container.offerLast(e);//加到队尾
}
//弹栈
public E pop(){
return container.pollLast();//移除队尾
}
//获取
public E peek(){
return container.peekLast();//获取队尾
}
public int size(){
return this.container.size();//队列长度
}
public static void main(String[] args) {
}
}