From 99688bc7ad95b4110297f9ba0e95efc0ad46b442 Mon Sep 17 00:00:00 2001 From: deepthi <102957802+saideepthipadala@users.noreply.github.com> Date: Mon, 10 Oct 2022 16:00:24 +0530 Subject: [PATCH] 622. Design Circular Queue --- MyCircularQueue.java | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 MyCircularQueue.java diff --git a/MyCircularQueue.java b/MyCircularQueue.java new file mode 100644 index 0000000..a1db405 --- /dev/null +++ b/MyCircularQueue.java @@ -0,0 +1,62 @@ +//https://leetcode.com/problems/design-circular-queue/ + + +class MyCircularQueue { + + public int [] arr; + int front = 0,rear=-1; + int num_of_elem =0; + public MyCircularQueue(int k) { + arr = new int[k]; + num_of_elem=k; + } + + public boolean enQueue(int value) { + + if(!isFull()){ + rear = (rear+1)%num_of_elem; + arr[rear]=value; + return true; + } + else return false; + + } + + public boolean deQueue() { + if(!isEmpty()){ + if(front == rear) { + rear =-1;front = 0; + } + else { + front =(front+1) %num_of_elem; + } + return true; + } + else return false; + } + + public int Front() { + if(!isEmpty()) return arr[front]; + else return -1; + + + } + + public int Rear() { + if(!isEmpty()) return arr[rear]; + else return -1; + + } + + public boolean isEmpty() { + if(rear == -1) return true; + else return false; + + } + + public boolean isFull() { + if(front == (rear+1)%num_of_elem && !isEmpty()) return true; + else return false; + + } +}