Wednesday, November 21, 2012

STACK OPERATION in java

STACK OPERATION in java

class stack
{
private int stack[]=new int[10];
private int tos;
stack()
{
tos=-1;
}
void push(int x)
{
if(tos==9)
System.out.println("Stack is full");
else
{
stack[++tos]=x;
System.out.println("Value is inserted: "+stack[tos]);
}
}
int pop()
{
if(tos==-1)
{
System.out.println("Stack is empty");
return 0;
}
else
{
System.out.println("Stack data is deleted: "+stack[tos]);
return(stack[tos--]);
}
}
}

class sttop
{
public static void main(String args[])
{
stack obj=new stack();
int i;
System.out.println("STACK OPERATION");
for(i=0;i<9;i++)
obj.push(i);
for(i=0;i<9;i++)
obj.pop();
}
}



/* OUT PUT


STACK OPERATION


Value is inserted: 0
Value is inserted: 1
Value is inserted: 2
Value is inserted: 3
Value is inserted: 4
Value is inserted: 5
Value is inserted: 6
Value is inserted: 7
Value is inserted: 8


Stack data is deleted: 8
Stack data is deleted: 7
Stack data is deleted: 6
Stack data is deleted: 5
Stack data is deleted: 4
Stack data is deleted: 3
Stack data is deleted: 2
Stack data is deleted: 1
Stack data is deleted: 0
*/

0 comments:

Post a Comment