日期:2014-05-20 浏览次数:21054 次
package sun.way; class QueueT { private int maxSize; private int[] queArr; private int front; private int rear; private int nItems; public QueueT(int s) { maxSize = s; queArr = new int[maxSize]; front = 0; rear = -1; nItems = 0; } public void insert(int j) { if (isFull()) return; if (rear == maxSize - 1) rear = -1; queArr[++rear] = j; nItems++; } public int remove() { int temp = queArr[front++]; if (front == maxSize) front = 0; nItems--; return temp; } public int peekFront(){ return queArr[front]; } public boolean isEmpty() { return (nItems == 0); } public boolean isFull() { return (nItems == maxSize); } public int size() { return nItems; } public String toString() { StringBuffer sb = new StringBuffer(); for (int i = rear; i >=0; i--) { sb.append("" + queArr[i] + " "); } return sb.toString(); } } public class QueueApp { public static void main(String[] args) { QueueT theQueue = new QueueT(10); theQueue.insert(39); theQueue.insert(23); theQueue.insert(12); theQueue.insert(54); theQueue.insert(63); theQueue.insert(36); System.out.print(theQueue); System.out.println(); if (!theQueue.isEmpty()) System.out.println("删除" + theQueue.remove()); System.out.println("------------------"); System.out.print(theQueue); } }