WAP to implement a stack in Java

Free Java Programs

WAP to implement a stack in Java

import java.io.*;
class Stack
{
int st[]=new int[10];
int top,i;
Stack()
{
top=-1;
i=0;
}
void push(int s)
{
if(top==9)
System.out.println("Stack is full");
else
st[++top]=s;
}
int pop()
{
if(top<0)
{
System.out.println("Stack is Empty");
return 0;
}
else
return st[top--];
}
}
class ImplementStack
{
public static void main(String args[]) throws IOException
{
Stack stck =new Stack();
String ch;
int i;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter value for a stack");
for(i=0;i<10;i++)
stck.push(Integer.parseInt(br.readLine()));
System.out.println("values in stack are");
for(i=0;i<10;i++)
System.out.println(stck.pop());
}}

Post a Comment

Previous Post Next Post