⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayqueue.java

📁 真的很麻烦也
💻 JAVA
字号:
interface Queue {  
 /** 
  * @return number of elements in the queue.
  */
  public int size();  
 /** 
  * @return true if the queue is empty, false otherwise.
  */
  public boolean isEmpty(); 
 /**
  * Inspect the element at the front of the queue.
  *
  * @return element at the front of the queue.
  * @exception QueueEmptyException if the queue is empty.
  */
  public Object front(); //throws QueueEmptyException; 
 /** 
  * Insert an element at the rear of the queue.
  *
  * @param element new element to be inserted.
  */
  public Object enqueue (Object element); 
 /** 
  * Remove the element at the front of the queue.
  *
  * @return element removed.
  * @exception QueueEmptyException if the queue is empty.
  */
  public Object dequeue(); //throws QueueEmptyException; 
}

public class ArrayQueue implements Queue
{
	public static final int CAPACITY = 1000;
	private int N, f, r;
	private Object Q[];
	 
	
	public ArrayQueue()
	{
		this(CAPACITY);
	}
	public ArrayQueue(int cap)
	{
		N = cap;
		Q = new Object[N];
		f= 0;
		r = 0;
			
	}
	
	public int size()
	{
		return ((N-f+r)%N);
	}
	
	public boolean isEmpty()
	{
		return (f==r);
	}
	
	public Object front() //throws StackFullException
	{
		if (isEmpty())
		//throw new StackFullException("Stack overflow.");
		{
			return ("Queue is empty.");
		}
		return Q[f];
	}
	public Object dequeue()// throws StackEmptyException
	{
		if (isEmpty())
		{
			return ("Queue is empty.");
		}
		//throw new StackEmptyException("Stack is empty.");
		Object temp = Q[f];
		Q[f]= null;
		f = (f+1)%N;
		return temp;
	}
	
	public Object enqueue(Object o)// throws StackEmptyException
	{
		if (size()==(N-1))
		{
			return ("Queue is full.");
		}
//		throw new StackEmptyException("Stack is Empty.");	
		Q[r] = o;
		r= (r+1)%N;
		return ("-");
	}
	
	public String getContent()
	{
		String cont= "";
		System.out.println("f="+f);
		System.out.println("r="+r);
		
		if (isEmpty())
			return cont;
		for (int i =f; i<(f+(N+r-f)%N); i++ )
		{
			cont += (Q[i%N].toString()+ ",");
		}
		return (cont.substring(0,cont.length()-1));		
	}	
}

⌨️ 快捷键说明

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