1 2 24 25 package com.lutris.util; 26 27 35 36 public class CircularQueue { 37 38 39 protected Object queue[] = null; 40 41 42 protected int sIndex; 43 44 45 protected int rIndex; 46 47 48 protected int count; 49 50 51 protected int qSize; 52 53 58 public CircularQueue(int s) { 59 qSize = s + 1; 60 sIndex = 0; 61 rIndex = qSize; 62 count = 0; 63 queue = new Object [qSize]; 64 } 65 66 73 public boolean put(Object x) throws ArrayIndexOutOfBoundsException { 74 75 if ((sIndex + 1 == rIndex) || 76 ((sIndex + 1 == qSize ) && (rIndex == 0))) { 77 return false; 79 } else { 80 queue[sIndex++] = x; 82 count++; 83 if (sIndex == qSize) { 84 sIndex = 0; 86 } 87 } 88 return true; 89 } 90 91 97 public Object get() throws ArrayIndexOutOfBoundsException { 98 99 if (rIndex == qSize) { 100 rIndex = 0; 102 } 103 if (rIndex == sIndex) { 104 return null; 106 } else { 107 count--; 109 Object obj = queue[rIndex]; 110 queue[rIndex] = null; 111 rIndex++; 112 return obj; 113 } 114 } 115 116 121 public int getCount() { 122 return count; 123 } 124 125 130 public boolean isEmpty() { 131 return (count == 0 ? true : false); 132 } 133 } 134 135 | Popular Tags |