java实现队列(数组方式)

QueueArray类

import java.util.Arrays;

public class QueueArray {
	T[] ars;
	private int default_capacity=10;
	//元素个数
	private int size=0;
	//队头
	private int font=-1;
	//队尾
	private int rear=-1;
	@SuppressWarnings("unchecked")
	public QueueArray()
	{
		ars=(T[])new Object[default_capacity];
		
	}
	@SuppressWarnings("unchecked")
	public QueueArray(int capacity)
	{
		ars=(T[])new Object[capacity];
		
	}
	 public  void push(T data)
	 {
		 if (size==ars.length)
		 {
			 //扩容20%
			 @SuppressWarnings("unchecked")
			T[] newars=(T[])new Object[(int)(ars.length*1.2)];
			 System.arraycopy(ars, 0, newars, 0, ars.length);
			 Arrays.fill(ars, null);
			 ars=newars;
			 push(data);
		 }
		 else
		 {   
			  if (font==-1)
				  font++;
			 rear=(rear+1)%ars.length;
			 ars[rear]=data;
			 size++;
			 
			 
		 }
		  
	 }
	 public T Pop()
	 {
		
		   if(font==-1)
		   {
			   return null;
		   }
		  else 
		  {

		 T fontdata=ars[font];
		 ars[font]=null;
		 font=(font+1)%ars.length;
		 size--;
		 return fontdata;
		  }
		 
		 
	 }
	 public int getSize()
	 {
		 return this.size;
	 }
	 public T getFontData()
	 {
		 return ars[font];
	 }
	 public int getFontIndex()
	 {
		 return this.font;
	 }
	 public T getRearData()
	 {
		 return ars[rear];
	 }
	 public int getRearIndex()
	 {
		 return this.rear;
	 }
}


 

你可能感兴趣的:(算法)