Stack using the array- Java
Stack using the array
import java.util.*;
class Stack
{
int top;
int s[]=new int[10];
Stack()
{
top=-1;
}
void push()
{
if(top==9)
System.out.println("Stack is full");
else{
System.out.println("Enter data");
Scanner sc=new Scanner(System.in);
int d=sc.nextInt();
s[++top]=d;
}
}
void pop()
{
if(top==-1) System.out.println("stack empty");
else
System.out.println(s[top--]);
}
void disp()
{
int i;
System.out.println("Stack contents....");
for(i=top;i>=0;i--)
System.out.println(s[i]);
}
}
class Main
{
public static void main(String args[])
{
int op;
Stack st=new Stack();
do
{
System.out.println("1.push \n2.pop\n3.dis\n4.exit");
Scanner s=new Scanner(System.in);
op=s.nextInt();
switch(op)
{
case 1:st.push();break;
case 2:st.pop();break;
case 3:st.disp();break;
case 4: System.exit(0);
}
}
while(op!=4);
}
}
Comments
Post a Comment