Advertisement
Samuel_Berkat_Hulu

Queue

Apr 27th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Queue here.
  4.  *
  5.  * @author Samuel Berkat Hulu
  6.  * @version 5.0 21-April-2021
  7.  */
  8. public class Queue
  9. {
  10.     private int capacity;
  11.     int front;
  12.     int rear;
  13.     char queueArr[];
  14.     int currentSize = 0;
  15.  
  16.     public Queue(int sizeOfQueue) {
  17.         this.capacity = sizeOfQueue;
  18.         front = 0;
  19.         rear = -1;
  20.         queueArr = new char[this.capacity];
  21.     }
  22.  
  23.     public void enqueue(char data) {
  24.         if (!isFull()){
  25.             rear++;
  26.             if (rear == capacity) {
  27.                 rear = 0;
  28.             }
  29.             queueArr[rear] = data;
  30.             currentSize++;
  31.         }
  32.     }
  33.  
  34.     public char dequeue() {
  35.         char fr = 0;
  36.         if (!isEmpty()){
  37.             fr = queueArr[front];
  38.             front++;
  39.             if (front == capacity) {
  40.                 front = 0;
  41.             }
  42.             currentSize--;
  43.             return fr;
  44.         }
  45.         return fr;
  46.     }
  47.  
  48.     public boolean isFull() {
  49.         if (currentSize == capacity) {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     public boolean isEmpty() {
  56.  
  57.         if (currentSize == 0) {
  58.             return true;
  59.         }
  60.         return false;
  61.     }
  62.    
  63.     public String getString(){
  64.         String str = "";
  65.         while(!isEmpty()){
  66.             str = str + dequeue();
  67.         }
  68.         return str;
  69.     }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement