Advertisement
FahimFaisal

ArrayQueue

Mar 15th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package QueueClassPractice;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class ArrayQueue {
  13.     // The number of items in the queue
  14.     int f;
  15.     int r;
  16.     int size;
  17.     Object [] cir;
  18.    
  19.     public ArrayQueue(){
  20.         f=-1;
  21.         r=-1;
  22.         size=0;
  23.         cir=new Object [10];
  24.     }
  25.    
  26.     public int size(){
  27.         return size;
  28.     }
  29. // Returns true if the queue is empty
  30.     public boolean isEmpty(){
  31.         if(size==0 || f==-1)
  32.             return true;
  33.         return false;
  34.     }
  35. // Adds the new item on the back of the queue, throwing the
  36. // QueueOverflowException if the queue is at maximum capacity. It
  37. // does not throw an exception for an "unbounded" queue, which
  38. // dynamically adjusts capacity as needed.
  39.     public void enqueue(Object obj) throws QueueOverflowException{
  40.         if(size==cir.length)
  41.             throw new QueueOverflowException();
  42.         else if(size==0){
  43.             f=0;
  44.             r=0;
  45.             cir[r]=obj;
  46.         }
  47.         else{
  48.             r=(r+1)%cir.length;
  49.             cir[r]=obj;
  50.            
  51.         }
  52.         size++;
  53.     }
  54. // Removes the item in the front of the queue, throwing the
  55. // QueueUnderflowException if the queue is empty.
  56.     public Object dequeue() throws QueueUnderflowException{
  57.         Object val=null;
  58.         if(size==0)
  59.             throw new QueueUnderflowException();
  60.         else if(f==r){
  61.             val=cir[f];
  62.             cir[f]=null;
  63.             f=0;
  64.             r=0;
  65.         }
  66.         else{
  67.             val=cir[f];
  68.             cir[f]=null;
  69.             f=(f+1)%cir.length;
  70.         }
  71.         size--;
  72.         return val;
  73.     }
  74. // Peeks at the item in the front of the queue, throwing
  75. // QueueUnderflowException if the queue is empty.
  76.     public Object peek() throws QueueUnderflowException{
  77.         if(size==0)
  78.             throw new QueueUnderflowException();
  79.         return cir[f];
  80.     }
  81. // Returns a textual representation of items in the queue, in the
  82. // format "[ x y z ]", where x and z are items in the front and
  83. // back of the queue respectively.
  84.     public String toString(){
  85.     String str="";
  86.     int k=f;
  87.     for(int i=0;i<size();i++){
  88.         str+=cir[k]+" ";
  89.         k=(k+1)%cir.length;
  90.     }
  91.     return "[ "+str+" ]";
  92.     }
  93. // Returns an array with items in the queue, with the item in the
  94. // front of the queue in the first slot, and back in the last slot.
  95.     public Object[] toArray(){
  96.         Object [] temp=new Object[size()];
  97.         int k=f;
  98.         for(int i=0;i<size();i++){
  99.             temp[i]=cir[k];
  100.             k=(k+1)%cir.length;
  101.         }
  102.         return temp;
  103.     }
  104. // Searches for the given item in the queue, returning the
  105. // offset from the front of the queue if item is found, or -1
  106. // otherwise.
  107.     public int search(Object obj){
  108.         int k=f;
  109.         for(int i=0;i<size();i++){
  110.             if(obj.equals(cir[k])){
  111.                 return i;
  112.             }
  113.             k=(k+1)%cir.length;
  114.         }
  115.         return -1;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement