OneCompiler

Creating a Stack using an Array in Java Language

183

import java.util.*;

public class Main{
public static class Stack{
int size = 1000;
int arr[] = new int[size];
int top = -1;
void push(int x){
top++;
arr[top] = x;
}
int pop(){
int x = arr[top];
top--;
return x;
}
int top(){
return arr[top];
}
int size(){
return top++;
}
}
public static void main (String[] args) {
Stack s = new Stack();
s.push(3);
System.out.print(s.top());
}
}