Advertisement
ralphdc09

its101.md.wk.07.QueueImplementation

Nov 2nd, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package its101.mdwk07;
  2.  
  3. class Queue {
  4.  
  5.     private static int front, rear, capacity;
  6.     private static int[] queue;
  7.  
  8.     Queue(int size) {
  9.         front = rear = 0;
  10.         capacity = size;
  11.         queue = new int[capacity];
  12.     }
  13.  
  14.     // Create Enqueue() method, this will be use to insert an element into the queue
  15.     static void enqueue(int item) {
  16.         // Check if queue is full
  17.         if (capacity == rear) {
  18.             System.out.println("Error! Queue is overflow.");
  19.         }
  20.         // Insert element at the rear
  21.         else {
  22.             queue[rear] = item;
  23.             rear++;
  24.         }
  25.     }
  26.  
  27.     // Create Dequeue() method, this will be use to remove an element from the queue
  28.     static void dequeue() {
  29.         // Check if queue is empty
  30.         if (front == rear) {
  31.             System.out.println("Error! Queue is underflow.");
  32.         }
  33.         // Shift elements to the right by one place until rear
  34.         else {
  35.             if (rear - 1 >= 0) System.arraycopy(queue, 1, queue, 0, rear - 1);
  36.             // Set queue[rear] to 0
  37.             if (rear < capacity)
  38.                 queue[rear] = 0;
  39.             // Decrement rear
  40.             rear--;
  41.         }
  42.     }
  43.  
  44.     // Create queuePrint() method, this will be use to print queue elements
  45.     static void queuePrint() {
  46.         int i;
  47.         if (front == rear) {
  48.             System.out.println("Queue is Empty");
  49.             return;
  50.         }
  51.         // Traverse from front to rear and print elements
  52.         for (i = front; i < rear; i++) {
  53.             System.out.printf(" %d ", queue[i]);
  54.         }
  55.     }
  56.  
  57.     // Create queuePrint() method, this will be use to print the front of queue
  58.     static void peek() {
  59.         if (front == rear) {
  60.             System.out.println("Queue is Empty");
  61.             return;
  62.         }
  63.         System.out.printf("\nFront element of the queue: %d \n", queue[front]);
  64.     }
  65.  
  66.     // Create isEmpty() method, this will check if the queue is empty
  67.     public boolean isEmpty() {
  68.         return (front == rear);
  69.     }
  70.  
  71.     // Create isFull() method, this will check if the queue is full
  72.     public boolean isFull() {
  73.         return (rear == capacity);
  74.     }
  75. }
  76.  
  77. public class QueueFinal {
  78.     public static void main(String[] args) {
  79.         // Create a queue of capacity 5
  80.         Queue ralph = new Queue(5);
  81.  
  82.         // Check if the queue is empty using isEmpty()
  83.         boolean resultEmpty = ralph.isEmpty();
  84.         System.out.println("Is queue empty: " + resultEmpty); // This will print true (queue is empty)
  85.  
  86.         // Insert elements in the queue using enqueue()
  87.         ralph.enqueue(1);
  88.         ralph.enqueue(2);
  89.         ralph.enqueue(3);
  90.         ralph.enqueue(4);
  91.         ralph.enqueue(5);
  92.  
  93.         // Print Queue elements again using queuePrint()
  94.         System.out.println("Element of the queue after enqueue operation: ");
  95.         ralph.queuePrint(); // This will now print the new elements inserted to the queue
  96.  
  97.         // Check again if the queue is empty
  98.         resultEmpty = ralph.isEmpty();
  99.         System.out.println("\nIs queue empty: " + resultEmpty); // This will now print false (queue is not empty)
  100.  
  101.         // Dequeue elements using dequeue()
  102.         ralph.dequeue();
  103.         ralph.dequeue();
  104.  
  105.         // Print Queue elements after the dequeue operation
  106.         System.out.println("Element of the queue after two dequeue operations: ");
  107.         ralph.queuePrint();
  108.  
  109.         // Print the front of the queue using the peek()
  110.         ralph.peek();
  111.  
  112.         // Check if the queue is full using isFull()
  113.         boolean resultFull = ralph.isFull();
  114.         System.out.println("Is queue full: " + resultFull);
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement