package com.data.struct; public class ArrayQueue { private int []data; private int head; private int tail; private boolean full; public ArrayQueue(int size){ data=new int[size]; head=0; tail=0; } public void enqueue(int d)throws Exception{ if(head-tail==0&&full||(head==0&&(tail==0)&&full)){ throw new Exception("full"); } data[tail]=d; tail=tail+1; if(tail==data.length){ tail=0; } if(head==tail){ full=true; } } public int dequeue()throws Exception{ if(head==tail&&!full||(head==data.length&&tail==0)){ throw new Exception("empty"); } full=false; head=head+1; if(head==data.length){ head=0; return data[data.length-1]; }else{ return data[head-1]; } } public static void main(String[] args)throws Exception { ArrayQueue queue=new ArrayQueue(10); for(int i=0;i<10;i++){ queue.enqueue(i); } for(int i=0;i<10;i++){ System.out.print(queue.dequeue()+" "); } System.out.println(); for(int i=0;i<5;i++){ queue.enqueue(i); } for(int i=0;i<3;i++){ System.out.print(queue.dequeue()+" "); } System.out.println(); for(int i=0;i<8;i++){ queue.enqueue(i); } for(int i=0;i<10;i++){ System.out.print(queue.dequeue()+" "); } System.out.println(); } }