1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
public class MyQueue { private int[] arr; private int maxSize; private int elems; private int font; private int end;
public MyQueue(int size) { this.maxSize = size; this.arr = new int[maxSize]; this.elems = 0; this.font = 0; this.end = -1; }
public void insert(int value) { if(end == maxSize - 1) { end = -1; } arr[++end] = value; if(elems < maxSize) { elems++; }else { font++; } }
public int remove() { if(font == maxSize) { font = 0; } elems--; return arr[font++]; }
public boolean isEmpty() { return this.elems == 0; }
public boolean isFull() { return this.elems == maxSize; }
public void printQueue() { int index = font; for(int i = 0; i < elems; i++) { System.out.print(arr[index++]+"\t"); if(index == maxSize) { index = 0; } } System.out.println(); } }
|