Skip to content

Commit

Permalink
Create Implement queue using array
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored May 14, 2024
1 parent 2e818cc commit c62709d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Implement queue using array
Original file line number Diff line number Diff line change
@@ -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++];
}
}

0 comments on commit c62709d

Please sign in to comment.