package stack;
public class STACK {
private int[] data;
private int[] reverse;
private int top;
private int top1;
public static final int dafault_capacity=10;
public STACK(int capacity) throws Exception{
if(capacity<1) {
throw new Exception("INVALID CAPACITY");
}
this.data=new int[capacity];
this.reverse=new int[capacity];
this.top=-1;
this.top1=-1;
}
public int size(){
return this.top+1;
}
public void push(int value) throws Exception{
if(this.size()==this.data.length) {
throw new Exception("STACK IS FULL");
}
this.top++;
this.data[this.top]=value;
}
public void pop() throws Exception{
if(this.size()==0) {
throw new Exception("STACK IS EMPTY");
}
int rv=this.data[this.top];
this.data[this.top]=0;
this.top--;
}
public int top() throws Exception{
if(this.size()==0) {
throw new Exception("STACK IS EMPTY");
}
int rv=this.data[this.top];
return rv;
}
public void pushreverse() throws Exception{
for(int i=0;i<=4;i++) {
this.top1++;
this.reverse[this.top1]=this.top();
this.pop();
}
}
public void display(){
for(int i=this.top;i>=0;i--) {
System.out.print("<<"+this.data[i]);
}
System.out.println(" END");
}
public void display1(){
for(int i=0;i<=4;i++) {
System.out.print("<<"+this.reverse[i]);
}
}
}
CLASS 2
package stack;
public class STACK_CLIENT {
public static void main(String[] args) throws Exception{
STACK st=new STACK(5);
for(int i=1;i<=5;i++) {
st.push(i*10);
}
st.display();
st.pushreverse();
st.display1();
}
}
O/P
<<50<<40<<30<<20<<10 END
<<50<<40<<30<<20<<10
WHY ?