Thursday, February 20, 2014

ARRAY IMPLEMENTATION OF STACK

PROGRAM:

import java.io.*;
import java.lang.*;
class data
{
int i,x;
int a[]=new,int[10];
int top=-1;
void push() throws IOException
{
DataInput Stream in = new DataInputStream(System.in);
System.out.println("enter the elements");
x=Integer.parseInt(in,readLine());
if(top>=5)
{
System.out.println("overflow");
}
else
{a[++top]=x;
}
}
void pop()throws Exception
{
if(top<0)
{
System.out.println("Underflow");
}
else
{
for(i=1;i,=top;i++)
{
top=top-1;
}
}
void display()throws IOExcetion
{
for (i=0;i<=top;i++)
{
System.out.println("the elements ;"+a[i]);
}
}
}

class stack
{
public static void main(string args[])throws IOException
{
Data d=new Data();
DatainputStream in=new DataInputStream(System.in);
int i,x,c;
int top=-1;
int a[]=new int[10];
do
{
System.out.println("enter the choice");
c=Integer.parseInt(in.readline());
switch(c)
{
case 1:
d.push();
break;
case 2:
d.pop();
case 3:
d.display();
break;
}
}
while(c<=3)
}


OUTPUT:

enter your choice:1
enter the elements:10
enter your choice:2
enter the elements:20
enter your choice:1
enter the elements:30
enter your choice:3
elements:
10
20
30
enter your choice:2
30
enter your choice:3
10

20

No comments:

Post a Comment