Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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.     String [] pole;
  8.     int head =0;
  9.     int end =0;
  10.    
  11.     public CircularArrayQueue() {
  12.         pole = new String [5];
  13.     }
  14.  
  15.     public CircularArrayQueue(int capacity) {
  16.         pole = new String [capacity];
  17.     }
  18.  
  19.     @Override
  20.     public int size() {
  21.         int size=0;
  22.         for (String x : pole ){
  23.             if (x!=null){
  24.                 size++;
  25.             }
  26.         }
  27.         return size;
  28.     }
  29.  
  30.     @Override
  31.     public boolean isEmpty() {
  32.         if (pole[head]==null){
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37.  
  38.     @Override
  39.     public boolean isFull() {
  40.         if(head-end==0 && pole[head]!=null ){
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.  
  46.     @Override
  47.     public boolean enqueue(String obj) {
  48.         if(this.isFull() || obj==null){
  49.             return false;
  50.         }
  51.         if (this.isEmpty()){
  52.             pole[head]=obj;
  53.         }
  54.         pole[end]=obj;
  55.         if(end==pole.length-1){
  56.             end=0;
  57.         } else{
  58.             end++;
  59.         }
  60.         return true;
  61.     }
  62.  
  63.     @Override
  64.     public String dequeue() {
  65.         if (!this.isEmpty()){
  66.             String deq=pole[head];
  67.             pole[head]=null;
  68.             if(head==pole.length-1){
  69.                 head=0;
  70.             }else {
  71.                 head++;
  72.             }
  73.             return deq;
  74.         }
  75.         return null;
  76.     }
  77.  
  78.     @Override
  79.     public void printAllElements() {
  80.         for(String x : pole ){
  81.             if(x!=null){
  82.                 System.out.println(x);
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement