Skip to content

Commit

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

0 comments on commit 2e818cc

Please sign in to comment.