From 3a9446cb4e485715a3369c78e2da87d6786fe993 Mon Sep 17 00:00:00 2001 From: Sarath Mohan Date: Sun, 13 Oct 2019 00:09:25 +0530 Subject: [PATCH] Created new file in java for ArrayQueue(CircularQueue) --- Data Structures/Java/Queue/ArrayQueue.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Data Structures/Java/Queue/ArrayQueue.java diff --git a/Data Structures/Java/Queue/ArrayQueue.java b/Data Structures/Java/Queue/ArrayQueue.java new file mode 100644 index 00000000..352b857e --- /dev/null +++ b/Data Structures/Java/Queue/ArrayQueue.java @@ -0,0 +1,47 @@ + +public class ArrayQueue { + int arr[]; + int front,rear; + int MAXSIZE; + ArrayQueue(int k){ + arr=new int[k]; + MAXSIZE=k; + front=-1; + rear=-1; + } + void enaq(int key) { + if((rear+1)%MAXSIZE==front) { + System.out.println("Queue is full"); + } + else if (front==-1 && rear==-1) { + front =0; + rear=0; + arr[rear]=key; + } + else { + rear=(rear+1)%MAXSIZE; + arr[rear]=key; + } + } + int deaq() { + if(front==-1 && rear==-1) { + return Integer.MAX_VALUE; + } + else if(front==rear) { + int val=arr[rear]; + front=-1; + rear=-1; + return val; + } + else { + int val=arr[front]; + front=(front+1)%MAXSIZE; + return val; + } + } + void arqprint() { + for(int i=front;i<=rear;i++) { + System.out.println(arr[i]); + } + } +}