package heap;
public class StackInt {
protected int top;
protected StackInt rest;
public StackInt(){
top = 0;
rest = null;
}
public boolean equals(Object obj){
if (obj instanceof StackInt){
StackInt elem;
elem = (StackInt) obj;
if (elem.rest == null) return this.rest == null && this.top == elem.top;
if (this.rest == null) return elem.rest == null && this.top == elem.top;
return elem.top == this.top && this.rest.equals(elem.rest);
}
return false;
}
public StackInt copy(){
StackInt copy = new StackInt();
if (!empty()){
copy.top = top;
copy.rest = rest.copy();
}
return copy;
}
public StackInt add(int d) {return push(d);}
public int first(){return top;}
public StackInt push(int d){
StackInt out = new StackInt();
out.rest = this;
out.top = d;
return out;
}
public StackInt pop(){
return rest;
}
public boolean empty(){
return (rest == null);
}
/*
public String print(){
String out = "";
if (!empty()){
out += top.print();
out += "," + rest.print();
}
return out;
}
*/
/*
public static void main(String[] args){
StackInt s = new StackInt();
s = s.push(new int2(0,1));
s = s.push(new int2(2,3));
s = s.push(new int2(4,5));
s = s.push(new int2(6,7));
StackInt s2 = s.copy();
s = s.pop();
s = s.pop();
System.out.println(s.print());
System.out.println(s2.print());
}
public static StackInt test(){
StackInt s = new StackInt();
s = s.push(1);
s = s.push(2);
s = s.push(3);
s = s.push(4);
StackInt s2 = s.copy();
s = s.pop();
s = s.pop();
return s;
}
*/
}
|