Code taken from a Data Structure workbook.
public class PilhaContig {
private Item [] info;
private int topo;
public PilhaContig(int qte){
this.topo = 0;
this.info = new Item [qte];
}
public Item getInfo(){
return this.info[this.topo-1];
}
public int getTopo(){
return this.topo;
}
public boolean eVazia(){
return (this.topo == 0);
}
public boolean eCheia(){
return (this.topo == this.info.length);
}
public boolean empilhar (Item elem){
if (this.eCheia())
return false;
else {
this.info[this.topo]= elem;
this.topo++;
return true;
}
}
public Item desempilhar(){
if (this.eVazia())
return null;
else{
this.topo--;
return this.info[this.topo];
}
}
In the stack class the top variable points to the empty memory space above the last item inserted in the stack. If I unpick the last item with this method (decreasing 1 in the variable top this.topo--;
and returning the info
vector with the index at the top decreased return this.info[this.topo];
) I would not end up returning the last item of the stack instead of unstacking this item?