Hi guys , today we are going to learn how to implement stack using array in java. this is one of the most popular java interview question
So lets see how to implement in java using array.
Program
So lets see how to implement in java using array.
Program
public class StackOperation {
static int top=-1;
static int[] dataList ;
public static void main(String[] args)
{
// int[] dataList ={10,23,45,5,3,2};
dataList = new int[6];
push(2);
push(3);
push(5);
push(10);
push(23);
push(45);
push(48);
// push(15);
push(3);
pop();
pop();
peek();
}
public static void push(int data )
{
if(top != dataList.length-1)
{
top++;
System.out.println("pushing data "+data);
dataList[top] =data;
}else{
System.out.println("Stack Overflow");
}
}
public static void pop()
{
if(top!=-1)
{
top--;
}else{
System.out.println("Stack Underflow");
}
}
public static void peek()
{
System.out.println(dataList[top]);
}
Hope you guys are able to understand the code if you have any problem , do let me know by comment below.
Thanks for reading
-Noeik
Thanks for reading
-Noeik