-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e818cc
commit c62709d
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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++]; | ||
} | ||
} |