From c62709da69e5a2cb173612d4dd2b6b1a762f76b1 Mon Sep 17 00:00:00 2001 From: Disha Thakurata <146114938+dishathakurata@users.noreply.github.com> Date: Tue, 14 May 2024 11:20:12 +0530 Subject: [PATCH] Create Implement queue using array --- Implement queue using array | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Implement queue using array diff --git a/Implement queue using array b/Implement queue using array new file mode 100644 index 0000000..687be09 --- /dev/null +++ b/Implement queue using array @@ -0,0 +1,62 @@ +//Implement queue using array + +import java.util.Scanner; + +class GfG { + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + + while(t > 0) { + MyQueue obj = new MyQueue(); + int Q = sc.nextInt(); + + while(Q > 0) { + int QueryType = 0; + QueryType = sc.nextInt(); + + if(QueryType == 1) { + int a = sc.nextInt(); + obj.push(a); + } + else if(QueryType == 2) { + System.out.print(obj.pop() + " "); + } + + Q--; + } + System.out.println(""); + + t--; + } + } +} + +class MyQueue { + + int front, rear; + int arr[] = new int[100005]; + + MyQueue() { + front=0; + rear=0; + } + + void push(int x) { + if(rear == arr.length - 1) { + System.out.println("Overflow"); + } + + else { + arr[rear++] = x; + } + } + + int pop() { + if(front == rear || rear == 0) { + return -1; + } + + return arr[front++]; + } +}