Week 5 :
Write a Java program
that:
i) Implements 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());
}
}
No comments:
Post a Comment