-
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
ab2ca09
commit 2e818cc
Showing
1 changed file
with
63 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,63 @@ | ||
//Implement stack using array | ||
|
||
import java.util.*; | ||
import java.io.*; | ||
import java.lang.*; | ||
|
||
class GfG { | ||
public static void main(String args[])throws IOException { | ||
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
|
||
while(t>0) { | ||
MyStack obj = new MyStack(); | ||
int Q = Integer.parseInt(read.readLine()); | ||
int k = 0; | ||
String str[] = read.readLine().trim().split(" "); | ||
|
||
while(Q>0) { | ||
int QueryType = 0; | ||
QueryType = Integer.parseInt(str[k++]); | ||
|
||
if(QueryType == 1) { | ||
int a = Integer.parseInt(str[k++]); | ||
obj.push(a); | ||
} | ||
else if(QueryType == 2) { | ||
System.out.print(obj.pop()+" "); | ||
} | ||
|
||
Q--; | ||
} | ||
System.out.println(""); | ||
|
||
t--; | ||
} | ||
} | ||
} | ||
|
||
class MyStack { | ||
int top; | ||
int arr[] = new int[1000]; | ||
|
||
MyStack() { | ||
top = -1; | ||
} | ||
void push(int a) { | ||
if(top == arr.length - 1) { | ||
System.out.println("Overflow"); | ||
} | ||
|
||
else { | ||
arr[++top] = a; | ||
} | ||
} | ||
|
||
int pop() { | ||
if(top == -1) { | ||
return -1; | ||
} | ||
|
||
return arr[top--]; | ||
} | ||
} |