Advertisement
Guest User

CircularArrayQueue

a guest
Nov 20th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package cz.cvut.fel.pr1;
  6.  
  7. /**
  8.  *
  9.  * @author
  10.  */
  11. public class CircularArrayQueue implements Queue {
  12.  
  13.     String[] array;
  14.     int headIndex = 0;
  15.     int lowIndex = 0;
  16.  
  17.     public CircularArrayQueue() {
  18.         array = new String[5];
  19.  
  20.     }
  21.  
  22.     public CircularArrayQueue(int capacity) {
  23.         array = new String[capacity];
  24.  
  25.     }
  26.  
  27.     /**
  28.      * It will count number of elements in CircularArrayQueue, not a size of the
  29.      * structure
  30.      *
  31.      * @return the number of elements in CircularArrayQueue (int)
  32.      */
  33.     public int size() {
  34.         int size;
  35.         size = headIndex-lowIndex;
  36.         return size;
  37.     }
  38.  
  39.     /**
  40.      * It will check if CircularArrayQueue is empty
  41.      *
  42.      * @return <code>true</code> if the CircularArrayQueue is empty and contains
  43.      * no elements <code>false</code> otherwise (CircularArrayQueue contains
  44.      * atleast one element).
  45.      */
  46.     public boolean isEmpty() {
  47.         if (size() == 0) {
  48.             return true;
  49.         } else {
  50.             return false;
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * It will check if CircularArrayQueue is full
  56.      *
  57.      * @return <code>true</code> if the CircularArrayQueue is full
  58.      * <code>false</code> otherwise.
  59.      */
  60.     public boolean isFull() {
  61.         if (size() == array.length) {
  62.             return true;
  63.         } else {
  64.             return false;
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * This method will enqueue the String to CircularArrayQueue
  70.      *
  71.      * @param obj the String object that we want add to CircularArrayQueue
  72.      * @return <code>true</code> if the obj param is added to CircularArrayQueue
  73.      * <code>false</code> otherwise (if CircularArrayQueue is full or if we obj
  74.      * param is null).
  75.      */
  76.     public boolean enqueue(String obj) {
  77.         if (isFull() == true || obj == null) {
  78.             return false;
  79.         } else {
  80.             array[headIndex] = obj;
  81.             headIndex++;
  82.             return true;
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * This method will dequeue the first String from CircularArrayQueue
  88.      *
  89.      * @return <code>String value</code> if the CircularArrayQueue is not empty
  90.      * <code>null</code> otherwise (if CircularArrayQueue is empty).
  91.      */
  92.     public String dequeue() {
  93.         if (isEmpty() == true){
  94.             return null;
  95.         } else {
  96.             String halp = array[lowIndex];
  97.             array[lowIndex] = null;
  98.             return halp;
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * It will print all elements from CircularArrayQueue
  104.      */
  105.     public void printAllElements() {
  106.         for (int i = lowIndex; i <= headIndex; i++){
  107.             System.out.println(array[i]);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement