Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Queue{
  2.  
  3.     private int capacity;
  4.     int queueArr[];
  5.     int front;
  6.     int rear;
  7.     int currentSize = 0;
  8.  
  9.     public Queue(int sizeOfQueue) {
  10.         this.capacity = sizeOfQueue;
  11.         front = 0;
  12.         rear = -1;
  13.         queueArr = new int[this.capacity];
  14.     }
  15.  
  16.     public void enqueue(int data) {
  17.         if (isFull()) {
  18.         } else {
  19.             rear++;
  20.             if (rear == capacity) {
  21.                 rear = 0;
  22.             }
  23.             queueArr[rear] = data;
  24.             currentSize++;
  25.         }
  26.     }
  27.  
  28.     public void dequeue() {
  29.         if (isEmpty()) {
  30.         } else {
  31.             front++;
  32.             if (front == capacity) {
  33.                 front = 0;
  34.             }
  35.             currentSize--;
  36.         }
  37.     }
  38.  
  39.     public boolean isFull() {
  40.         if (currentSize == capacity) {
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.  
  46.     public boolean isEmpty() {
  47.  
  48.         if (currentSize == 0) {
  49.             return true;
  50.         }
  51.         return false;
  52.     }
  53.    
  54.     public int size(){return currentSize;}
  55.    
  56.     public void show(){
  57.         if(isEmpty()){
  58.             System.out.print("Kosong");
  59.             return;
  60.         }
  61.         int i=front;
  62.         while(i<=rear){
  63.             System.out.print(queueArr[i]);
  64.             if(i!=rear) System.out.print(", ");
  65.             i++;
  66.         }
  67.     }
  68. }