隊列的堆棧的元素移動java
import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class stackQueue {
public static void main(String[] args) {
System.out.println("--------------------堆棧--------------------");
MyStack<Integer> stack = new MyStack<Integer>();
System.out.println("剛建立堆棧時,stack.isEmpty():" + stack.isEmpty() +",stack.size():" + stack.size());
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("push 3個元素時,棧內元素個數爲" + stack.size() + ",元素轉化成字符串後爲:" + stack.toString());
stack.pop();
stack.pop();
System.out.println("彈出2個元素後,棧內剩餘的元素個數爲" + stack.size() + ",元素轉化成字符串後爲:" + stack.toString());
System.out.println("--------------------隊列開始--------------------");
MyQueue<Integer> queue = new MyQueue<Integer>(5);
System.out.println("剛建立堆棧時,queue.isEmpty():" + queue.isEmpty() +",queue.isFull():"+queue.isFull()+ ",queue.size():" + queue.size());
for (int i = 0; i < 3; i++) {
queue.add(i);
}
System.out.println("添加3個元素:");
System.out.println("隊列頭元素爲:"+queue.getHead()+",隊尾元素爲:"+queue.getTail()+",隊列長度爲:"+queue.size());
System.out.println(queue);
System.out.println("去掉2個元素:");//去掉元素,元素只是不能訪問到了,被去掉的元素能夠被添加的覆蓋
queue.remove();
queue.remove();
System.out.println("隊列頭元素爲:"+queue.getHead()+",隊尾元素爲:"+queue.getTail()+",隊列長度爲:"+queue.size());
queue.add(7);
System.out.println(queue);
for (int i = 3; i < 6; i++) {
queue.add(i);
}
System.out.println("添加4個元素:");//此時尾指又要從開始處添加元素,remove掉的被覆蓋
System.out.println("隊列頭元素爲:"+queue.getHead()+",隊尾元素爲:"+queue.getTail()+",隊列長度爲:"+queue.size());
System.out.println(queue);
System.out.println("去掉4個元素:");//此時尾指又要從開始處添加元素
queue.remove();queue.remove();queue.remove();queue.remove();
System.out.println("queue.getHead():"+queue.getHead()+",queue.getTail():"+queue.getTail()+",queue.size():"+queue.size());
System.out.println(queue);
}
}
class MyStack<E> {
private ArrayList<E> list;
public MyStack() {
list = new ArrayList<E>();
}
/**
* 棧是否爲空
*
* @return
*/
public boolean isEmpty() {
return list.size() == 0;
}
/**
* 棧內容長度
*
* @return
*/
public int size() {
return list.size();
}
/**
* 添加元素
*
* @param e
*/
public void push(E e) {
list.add(e);
}
/**
* 彈出元素
*
* @return
*/
public E pop() {
if (list.size() > 0) {
return list.remove(list.size() - 1);
}
return null;
}
@Override
public String toString() {
return Arrays.toString(list.toArray());
}
}
class MyQueue<E> {
private int maxSize;// 隊列容量
private E queue[];// 隊列
private int head;// 頭指針
private int tail;// 尾指針
private int nItems;// 元素個數
@SuppressWarnings("unchecked")
public MyQueue(int maxSize) {
this.maxSize = maxSize;
this.queue = (E[]) new Object[maxSize];
this.head = 0;// 移除元素通常從下標0開始,頭指針指向待移除的元素(也就是移除元素的下標)
this.tail = -1;// 通常設爲-1,當添加元素後,尾指針數值爲當前已經添加的元素的下標位置
this.nItems = 0;
}
/**
* 隊列是否爲空
*
* @return
*/
public boolean isEmpty() {
return nItems == 0;
}
/**
* 隊列是否已滿
*
* @return
*/
public boolean isFull() {
return nItems == queue.length;
}
/**
* 添加從隊尾開始
*
* @param e
*/
public void add(E e) {
if (isFull()) {
throw new RuntimeException("隊列已滿");
}
// 當隊尾指針已經到達數組的末尾,但數組卻未填滿(數組前面有空缺),此時又從起始位置添加元素
if (tail == maxSize - 1) {
tail = -1;
}
queue[++tail] = e;
nItems++;
}
/**
* 刪除從對頭開始
*
* @return
*/
public E remove() {
if (isEmpty()) {
throw new RuntimeException("隊列已空");
}
// 當對頭指針到達數組末尾,但數組個數卻不爲空(說明數組前面還有元素),此時又從起始位置刪除元素
if (head == maxSize) {
head = 0;
}
nItems--;
return queue[head++];
}
/**
* 獲取對頭元素
*
* @return
*/
public E getHead() {
return queue[head];
}
/**
* 獲取隊尾元素
*
* @return
*/
public E getTail() {
return queue[tail];
}
/**
* 隊列元素個數
*
* @return
*/
public int size() {
return nItems;
}
@Override
public String toString() {
return Arrays.toString(queue);
}
}
數組
運行結果:ide