棧:先進后出 隊列:先進先出
兩個棧實現一個隊列:
思路:先將數據存到第一個棧里,再將第一個棧里的元素全部出棧到第二個棧,第二個棧出棧,即可達到先進先出
源碼:
class Queue<E>{ //用的jdk自帶的棧
private Stack<E> s1=new Stack<>();
private Stack<E> s2=new Stack<>();
public void offer(E val){ //入隊
s1.push(val);
}
public E poll() { //出隊
while (s2.empty()){
while (!s1.empty()){
s2.push(s1.peek());
s1.pop();
}
}
E val=s2.peek();
s2.pop();
//獲取出隊元素后,再將s2里面的元素放入s1里面。
while (!s2.empty()){
s1.push(s2.pop());
}
return val;
}
public E peek(){//查看對頭元素
while (s2.empty()){
while (!s1.empty()){
s2.push(s1.peek());
s1.pop();
}
}
E val=s2.peek();
//獲取出隊元素后,再將s2里面的元素放入s1里面。
while (!s2.empty()){
s1.push(s2.pop());
}
return val;
}
public boolean empty(){ //判斷隊是否為空
return s1.empty();
}
}
測試:
public static void main(String[] args) {
Queue<Integer> queue = new Queue<>();
Random rand = new Random();
for (int i = 0; i < 5; i++) {
int data = rand.nextInt(100);
System.out.print(data + " ");
queue.offer(data);
}
System.out.println();
System.out.println("出隊:");
while (!queue.empty()) {
System.out.print(queue.poll()+" ");
}
}
運行結果:
79 67 45 73 59
出隊: 79 67 45 73 59
兩個隊列實現一個棧:
思路:先將數據存到第一個隊列里面,然后數據出隊一直出隊到地二個隊列里面,直到第一個隊列里面剩余一個數據,這個時候出隊 即可達到先進后出的特性
源碼:
自定義的一個循環隊列:
class Queue<E>{ // 存儲隊列元素的數組 private E[] que; // 表示隊頭的位置 private int front; // 表示隊尾的位置 private int rear; public E[] getQue() { return que; } public int getFront() { return front; } public int getRear() { return rear; } /** * 默認構造隊列,初始大小是10 */ public Queue(){ this(10); } /** * 用戶可以指定隊列的大小size * @param size */ public Queue(int size){ this.que = (E[])new Object[size]; this.front = this.rear = 0; } /** * 入隊操作 * @param val */ public void offer(E val){ if(full()){ // 擴容 E[] newQue = Arrays.copyOf(this.que, this.que.length*2); int index = 0; for(int i=this.front; i != this.rear; i=(i+1)%this.que.length){ newQue[index++] = this.que[i]; } this.front = 0; this.rear = index; this.que = newQue; } this.que[this.rear] = val; this.rear = (this.rear+1)%this.que.length; } /** * 出隊操作,並把出隊的元素的值返回 */ public E poll(){ if(empty()){ return null; } E front = this.que[this.front]; this.front = (this.front+1)%this.que.length; return front; } /** * 查看隊頭元素 * @return */ public E peek(){ if(empty()){ return null; } return this.que[this.front]; } /** * 判斷隊滿 * @return */ public boolean full(){ return (this.rear+1)%this.que.length == this.front; } /** * 判斷隊空 * @return */ public boolean empty(){ return this.rear == this.front; } }
兩個隊列實現一個棧:
class SeqStack<E>{ private Queue<E> que1; // 存放棧的元素 private Queue<E> que2; // 做一個輔助操作 public SeqStack(){ this.que1 = new Queue<>(); this.que2 = new Queue<>(); } public SeqStack(int size){ this.que1 = new Queue<>(size); this.que2 = new Queue<>(size); } public void push(E val){ this.que1.offer(val); } public E pop(){ // 從que1出隊,把最后一個出隊的元素返回 E data = null; /** * 把que1里面的所有元素出隊,放入que2里面, * 然后把que1最后一個出隊的元素直接返回,不用放入que2 */ while(!this.que1.empty()){ data = this.que1.poll(); if(this.que1.empty()){ break; } this.que2.offer(data); } // 獲取該出棧的元素以后,再把que2的元素再放入que1里面 while(!this.que2.empty()){ this.que1.offer(this.que2.poll()); } return data; } public E top(){ // 從que1出隊,把最后一個出隊的元素返回 E data = null; while(!this.que1.empty()){ data = this.que1.poll(); this.que2.offer(data); } // 獲取該出棧的元素以后,再把que2的元素再放入que1里面 while(!this.que2.empty()){ this.que1.offer(this.que2.poll()); } return data; } public boolean full(){ return this.que1.full(); } public boolean empty(){ return this.que1.empty(); } }
測試:
public static void main(String[] args) { SeqStack<Integer> seqStack=new SeqStack<>(); Random rand = new Random(); for (int i = 0; i < 5; i++) { int data = rand.nextInt(100); System.out.print(data + " "); seqStack.push(data); } System.out.println(); System.out.println("出棧:"); while (!seqStack.empty()) { System.out.print(seqStack.pop()+" "); } }
運行結果:
9 3 7 83 32
出棧:
32 83 7 3 9