publicinterfaceQueue{publicvoidappend(Objectobj)throwsException;publicObjectdelete()throwsException;publicObjectgetFront()throwsException;publicbooleanisEmpty();}publiccl...
public interface Queue {
public void append(Object obj) throws Exception;
public Object delete() throws Exception;
public Object getFront () throws Exception;
public boolean isEmpty();
}
public class SeqQueue {
/**
* @param args
*/
static final int defaultSize = 10;
int front, rear;
int tag;
int maxSize;
Object data[];
public SeqQueue() {
initiate(defaultSize);
}
public SeqQueue(int sz) {
initiate(sz);
}
public void initiate(int sz) {
maxSize = sz;
front = rear = 0;
tag = 0;
data = new Object[sz];
}
public void append(Object obj) throws Exception{
if(rear == front && tag == 1){
throw new Exception("队列已满,无法进行添加操作!");
}
data[rear] = obj;
rear = (rear + 1) % maxSize;
tag = 1;
}
public Object delete() throws Exception {
if(tag == 0){
throw new Exception("队列已空,无元素可删!");
}
Object temp = data[front];
front = (front + 1) % maxSize;
tag = 0;
return temp;
}
public Object getFront() throws Exception{
if(tag == 0){
throw new Exception("队列已空,无元素可取!");
}
return data[front];
}
public boolean isEmpty() throws Exception{
return tag == 0;
}
}
import java.util.Scanner;
public class Test {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入你要构造的队列的大小:number = ");
int number = new Scanner(System.in).nextInt();
SeqQueue seq = new SeqQueue(number);
System.out.println("请设置队列的内容:");
for(int i=0; i
System.out.println("请输入第"+ (i + 1)+"个元素");
try {
seq.append(new Scanner(System.in).nextInt());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
print(seq.maxSize , seq);
try {
Object obj = seq.delete();
System.out.println("\n"+obj);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void print( int number, SeqQueue seq){
for(int j =0; j
try {
System.out.print(" " + seq.getFront());
seq.front ++;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
展开