abouttr3

ArrayQueue

Oct 12th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public class ArrayQueue{
  2.     private int count;
  3.     private int fIndex,rIndex;
  4.     private int maxSize;
  5.     private Object []data;
  6.    
  7.     public ArrayQueue(){        
  8.         data=new Object[10];
  9.         maxSize=10;
  10.         fIndex=0;
  11.         rIndex=maxSize-1;
  12.         count=0;
  13.     }
  14.    
  15.     public ArrayQueue(int _maxSize){        
  16.         data=new Object[_maxSize];
  17.         maxSize=_maxSize;
  18.         fIndex=0;
  19.         rIndex=maxSize-1;
  20.         count=0;
  21.     }
  22.    
  23.     public void add(Object _data){
  24.         if(count==maxSize){
  25.             System.out.println("Stack Over Flow");
  26.         }
  27.         else{
  28.             rIndex=(rIndex+1)%maxSize;
  29.             data[rIndex]=_data;            
  30.             count++;
  31.         }
  32.     }
  33.    
  34.     public void delete(){
  35.         if(count==0){
  36.             System.out.println("Stack Under Flow");
  37.         }
  38.         else{
  39.             count--;
  40.             data[fIndex]=null;
  41.             fIndex=(fIndex+1)%maxSize;
  42.         }
  43.     }
  44.    
  45.     public Object front(){
  46.         if(data[fIndex]==null){
  47.             return null;
  48.         }
  49.         else{
  50.             return data[fIndex];
  51.         }
  52.     }
  53.    
  54.     public Object rear(){
  55.         if(data[rIndex]==null){
  56.             return null;
  57.         }
  58.         else{
  59.             return data[rIndex];
  60.         }
  61.     }
  62.    
  63.     public int size(){
  64.         return count;
  65.     }
  66.    
  67.     public boolean isEmpty(){
  68.         return (count==0);
  69.     }
  70.    
  71.     public boolean isFull(){
  72.         return (count==maxSize);
  73.     }
  74.    
  75.     public String toString()
  76.     {
  77.         String output = "";
  78.         if(!isEmpty()){
  79.             int index=0;
  80.             for(int i=fIndex;i<count+fIndex;i++){
  81.                 index=i%maxSize;
  82.                 output += "[" + data[index] + "]";
  83.             }
  84.             return output;
  85.         }
  86.         else{
  87.             return null;
  88.         }
  89.        
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment