seqqueue.java

来自「堆栈类 用java编写的。。。dasfhdakdkjadjakdasds-lev」· Java 代码 · 共 57 行

JAVA
57
字号
public class SeqQueue implements Queue{
	final int defaultSize = 10;
	int front;
	int rear;
	int count;
	int maxSize;
	Object[] data;
	
	public SeqQueue(){
		this.initiate(defaultSize);
	}
	
	public SeqQueue(int sz){
		this.initiate(sz);
	}
	
	private void initiate(int sz){
		maxSize = sz;
		front = rear = 0;
		count = 0;
		data = new Object[sz];
	}
	
	public void append(Object obj) throws Exception{
		if(count > 0 && front == rear){
			throw new Exception("队列已满!");
		}
		
		data[rear] = obj;
		rear = (rear + 1) % maxSize;
		count ++;
	}
	
	public Object delete() throws Exception{
		if(count == 0){
			throw new Exception("队列已空!");
		}
		
		Object temp = data[front];
		front = (front + 1) % maxSize;
		count --;
		return temp;
	}
	
	public Object getFront() throws Exception{
		if(count == 0){
			throw new Exception("队列已空!");
		}
		
		return data[front];
	}
	
	public boolean notEmpty(){
		return count != 0;
	}
		
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?