Tweet
/* Stack using interface */
import java.io.*;
interface stack
{
void push(int val);
void pop();
void display();
void check();
}
class stacktest implements stack
{
int top,a[],n;
stacktest(int cap)
{
n=cap;
top=-1;
a=new int[n];
}
public void push(int val)
{
if(top==n-1)
System.out.println("\nStack overflow .... \n");
else
a[++top]=val;
}
public void pop()
{
int r;
if(top==-1)
System.out.println("\nStack underflow...\n");
else
{
r=a[top--];
System.out.println("\nDeleted Element : " + r + "\n");
}
}
public void display()
{
if(top==-1)
System.out.println("\nStack Underflow .... \n");
else
{
System.out.print("Stack : ");
for(int i=top;i>=0;i--)
System.out.print(a[i] + " ");
System.out.println();
}
}
public void check()
{
if(top==-1)
System.out.println("\nStack empty... no element in stack....");
else if(top==n-1)
System.out.println("\nStack full.... no empty space...");
else
{
int s=top+1;
System.out.println("\nStack contains " + s + " elements");
}
}
}
class interstack
{
public static void main(String args[])throws IOException
{
System.out.println("\nProgram to implement stack using interface ");
int ch,n,val;
System.out.println("Enter array capacity : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
stacktest obj=new stacktest(n);
do
{
System.out.println("[1] Push [2] Pop [3] Display [4] Check stack status [5] Exit \n");
System.out.println("Enter choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter value to push : ");
val=Integer.parseInt(br.readLine());
obj.push(val);
obj.display();
break;
case 2:
obj.pop();
obj.display();
break;
case 3:
obj.display();
break;
case 4:
obj.check();
break;
case 5: break;
default :
System.out.println("Enter correct choice..");
break;
}
}while(ch!=5);
}
}
Array Stack using interface in Java
Posted by
LAHAUL SETH
~
Array Stack using interface in Java
2012-05-13T10:58:00+05:30
LAHAUL SETH
Java
|
Stack Implementation
|
Comments
Array Stack using interface in Java
2012-05-13T10:58:00+05:30
LAHAUL SETH
Java
|
Stack Implementation
|