Last active
February 26, 2024 12:51
-
-
Save itszechs/52fd719adcf7b23469b3de9147fc4c66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Queue { | |
| void enqueue(int data); | |
| void dequeue(); | |
| int front(); | |
| int rear(); | |
| int size(); | |
| void print(); | |
| } | |
| class QueueUsingArray implements Queue { | |
| private final int[] queue; | |
| private final int capacity; | |
| private int size = 0; | |
| public QueueUsingArray(int capacity) { | |
| queue = new int[capacity]; | |
| this.capacity = capacity; | |
| } | |
| @Override | |
| public void enqueue(int data) { | |
| if (size >= this.capacity) { | |
| throw new IllegalStateException("Queue is full"); | |
| } | |
| queue[size] = data; | |
| size++; | |
| } | |
| @Override | |
| public void dequeue() { | |
| ensureNotEmpty(); | |
| for (int i = 0; i < size - 1; i++) { | |
| queue[i] = queue[i + 1]; | |
| } | |
| size--; | |
| } | |
| @Override | |
| public int front() { | |
| ensureNotEmpty(); | |
| return queue[0]; | |
| } | |
| @Override | |
| public int rear() { | |
| ensureNotEmpty(); | |
| return queue[size - 1]; | |
| } | |
| @Override | |
| public int size() { | |
| return size; | |
| } | |
| @Override | |
| public void print() { | |
| for (int i = 0; i < size; i++) { | |
| System.out.print(queue[i] + " "); | |
| } | |
| System.out.println(); | |
| } | |
| private void ensureNotEmpty() { | |
| if (size <= 0) { | |
| throw new IllegalStateException("Queue is empty"); | |
| } | |
| } | |
| } | |
| class QueueUsingLinkedList implements Queue { | |
| private Node head = null; | |
| private Node last = null; | |
| private int size = 0; | |
| @Override | |
| public void enqueue(int data) { | |
| Node newNode = new Node(data); | |
| if (head == null) { | |
| head = newNode; | |
| last = head; | |
| } else { | |
| last.next = newNode; | |
| last = newNode; | |
| } | |
| size++; | |
| } | |
| @Override | |
| public void dequeue() { | |
| head = head.next; | |
| size--; | |
| } | |
| @Override | |
| public int front() { | |
| ensureNotEmpty(); | |
| return head.data; | |
| } | |
| @Override | |
| public int rear() { | |
| ensureNotEmpty(); | |
| return last.data; | |
| } | |
| @Override | |
| public int size() { | |
| return size; | |
| } | |
| @Override | |
| public void print() { | |
| Node current = head; | |
| while (current != null) { | |
| System.out.print(current.data + " "); | |
| current = current.next; | |
| } | |
| System.out.println(); | |
| } | |
| private void ensureNotEmpty() { | |
| if (head == null || last == null) { | |
| throw new IllegalStateException("Queue is empty"); | |
| } | |
| } | |
| } | |
| class QueueExample { | |
| public static void main(String[] args) { | |
| testQueue(new QueueUsingArray(50)); | |
| testQueue(new QueueUsingLinkedList()); | |
| } | |
| private static void testQueue(Queue myQueue) { | |
| myQueue.enqueue(1); | |
| myQueue.enqueue(2); | |
| myQueue.enqueue(3); | |
| System.out.print("Queue after enqueue operations: "); | |
| myQueue.print(); | |
| System.out.println("Front element: " + myQueue.front()); | |
| System.out.println("Rear element: " + myQueue.rear()); | |
| myQueue.dequeue(); | |
| System.out.print("Queue after dequeue operation: "); | |
| myQueue.print(); | |
| System.out.println("Front element: " + myQueue.front()); | |
| System.out.println("Rear element: " + myQueue.rear()); | |
| System.out.println("Size of the queue: " + myQueue.size()); | |
| System.out.println("==================================="); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment