Write a Java program to Implement stack ADT. - Code

Latest

programs

Monday, October 10, 2011

Write a Java program to Implement stack ADT.


Write a Java program to Implement stack ADT.
import java.io.*;
class stack
{
int stack[]=new int[10];
int tos;
stack()
{
tos=-1;
}
void push(int item)
{
if(tos==9)
System.out.println("stack is full");
else
stack[++tos]=item;
}
int pop()
{
if(tos<0)
{
System.out.println("stack underflow");
return 0;
}
else
return stack[tos--];
}
}
class teststack
{
public static void main(String args[])
{
stack mystack1=new stack();
stack mystack2=new stack();
for(int i=0;i<10;i++)
mystack1.push(i);
for(int i=10;i<20;i++)
mystack2.push(i);
System.out.println("stack in my stack1:");
for(int i=0;i<10;i++)
System.out.println(mystack1.pop());
System.out.println("stack in my stack2:");
for(int i=0;i<10;i++)
System.out.println(mystack2.pop());
}
}

3 comments:

  1. thank u friend
    ..............deepahemanth

    ReplyDelete
  2. very nice. for java examples, please visit http://java2novice.com site

    ReplyDelete
  3. Please tell output for this program

    ReplyDelete