Monday, April 21, 2008

STACK

class StacK
{
int top,size;
int a[]= new int[10];

Stac()
{

top=0;
}
void push(int dat)
{
if(top>=9)
{
System.out.println("Stack is full");
}
else
{
top++;
a[top]=dat;
}
}

void pop()
{
int x;
if(top==-1)
{
System.out.println("Stack is empty");
}

else
{
x=a[top];
top--;
System.out.println("The popped element is" +x);
}
}
void display()
{
for(int i=top;i>0;i--)
System.out.println(" " +a[i]);
}


}

class Stack
{
public static void main(String args[])
{
Stac s1=new Stac( );
s1.push(20);
s1.push(30);
s1.display();
s1.pop();
s1.display();
}
}

No comments: