Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package cz.cvut.fel.pjv;
  2.  
  3. /**
  4.  * Implementation of the {@link Queue} backed by fixed size array.
  5.  */
  6. public class CircularArrayQueue implements Queue {
  7.     private int size;
  8.     private String[] buffer;
  9.     private int lastUse;
  10.     /**
  11.      * Creates the queue with capacity set to the value of 5.
  12.      */
  13.     public CircularArrayQueue() {
  14.         size = 0;
  15.         buffer = new String[5];
  16.         lastUse = 0;
  17.     }
  18.  
  19.  
  20.     /**
  21.      * Creates the queue with given {@code capacity}. The capacity represents maximal number of elements that the
  22.      * queue is able to store.
  23.      * @param capacity of the queue
  24.      */
  25.     public CircularArrayQueue(int capacity) {
  26.         size = 0;
  27.         buffer = new String[capacity];
  28.     }
  29.  
  30.     public int size() {
  31.         return lastUse;
  32.     }
  33.  
  34.  
  35.     public boolean isEmpty() {
  36.         if (lastUse == 0){
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41.  
  42.  
  43.     public boolean isFull() {
  44.         return lastUse == buffer.length;
  45.     }
  46.  
  47.  
  48.     public boolean enqueue(String obj) {
  49.         if (!isFull() && obj != ""){
  50.             buffer[lastUse] = obj;
  51.             lastUse++;
  52.         }
  53.         return false;
  54.     }
  55.  
  56.  
  57.     public String dequeue() {
  58.         if (!isEmpty()){
  59.             String head = buffer[0];
  60.             for (int i =0; i < buffer.length-1;i++){
  61.                 buffer[i] = buffer[i+1];
  62.             }
  63.             lastUse--;
  64.             return head;
  65.         }
  66.         return null;
  67.     }
  68.  
  69.  
  70.     public void printAllElements() {
  71.         for (int i = 0; i< buffer.length; i++){
  72.             if (buffer[i] != null){
  73.                 System.out.println(buffer[i]);
  74.             }
  75.             else {
  76.                 break;
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement