|
| 1 | +package Task1; |
| 2 | + |
| 3 | +public class QueueX { |
| 4 | + private int[] arr; // array to store queue elements |
| 5 | + private int front; // front points to the front element in the queue |
| 6 | + private int rear; // rear points to the last element in the queue |
| 7 | + private int capacity; // maximum capacity of the queue |
| 8 | + private int count; // current size of the queue |
| 9 | + |
| 10 | + // Constructor to initialize a queue |
| 11 | + QueueX(int size) |
| 12 | + { |
| 13 | + arr = new int[size]; |
| 14 | + capacity = size; |
| 15 | + front = 0; |
| 16 | + rear = -1; |
| 17 | + count = 0; |
| 18 | + } |
| 19 | + |
| 20 | + // Utility function to dequeue the front element |
| 21 | + public int dequeue() |
| 22 | + { |
| 23 | + // check for queue underflow |
| 24 | + if (isEmpty()) |
| 25 | + { |
| 26 | + System.out.println("Underflow\nProgram Terminated"); |
| 27 | + System.exit(-1); |
| 28 | + } |
| 29 | + |
| 30 | + int x = arr[front]; |
| 31 | + |
| 32 | + System.out.println("Removing " + x); |
| 33 | + |
| 34 | + front = (front + 1) % capacity; |
| 35 | + count--; |
| 36 | + |
| 37 | + return x; |
| 38 | + } |
| 39 | + |
| 40 | + // Utility function to add an item to the queue |
| 41 | + public void enqueue(int item) |
| 42 | + { |
| 43 | + // check for queue overflow |
| 44 | + if (isFull()) |
| 45 | + { |
| 46 | + System.out.println("Overflow\nProgram Terminated"); |
| 47 | + System.exit(-1); |
| 48 | + } |
| 49 | + |
| 50 | + System.out.println("Inserting " + item); |
| 51 | + |
| 52 | + rear = (rear + 1) % capacity; |
| 53 | + arr[rear] = item; |
| 54 | + count++; |
| 55 | + } |
| 56 | + |
| 57 | + // Utility function to return the front element of the queue |
| 58 | + public int peek() |
| 59 | + { |
| 60 | + if (isEmpty()) |
| 61 | + { |
| 62 | + System.out.println("Underflow\nProgram Terminated"); |
| 63 | + System.exit(-1); |
| 64 | + } |
| 65 | + return arr[front]; |
| 66 | + } |
| 67 | + |
| 68 | + // Utility function to return the size of the queue |
| 69 | + public int size() { |
| 70 | + return count; |
| 71 | + } |
| 72 | + |
| 73 | + // Utility function to check if the queue is empty or not |
| 74 | + public boolean isEmpty() { |
| 75 | + return (size() == 0); |
| 76 | + } |
| 77 | + |
| 78 | + // Utility function to check if the queue is full or not |
| 79 | + public boolean isFull() { |
| 80 | + return (size() == capacity); |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) { |
| 84 | + // TODO Auto-generated method stub |
| 85 | + // create a queue of capacity 5 |
| 86 | + QueueX q = new QueueX(5); |
| 87 | + |
| 88 | + q.enqueue(1); |
| 89 | + q.enqueue(2); |
| 90 | + q.enqueue(3); |
| 91 | + |
| 92 | + System.out.println("The front element is " + q.peek()); |
| 93 | + q.dequeue(); |
| 94 | + System.out.println("The front element is " + q.peek()); |
| 95 | + |
| 96 | + System.out.println("The queue size is " + q.size()); |
| 97 | + |
| 98 | + q.dequeue(); |
| 99 | + q.dequeue(); |
| 100 | + |
| 101 | + if (q.isEmpty()) { |
| 102 | + System.out.println("The queue is empty"); |
| 103 | + } |
| 104 | + else { |
| 105 | + System.out.println("The queue is not empty"); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments