2
Code taken from a Data Structure booklet.
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 into the stack. If I pop the last item with this method (decreasing 1 at the top variable this.topo--;
and returning the vector info
with the index at the diminished top return this.info[this.topo];
) I wouldn’t end up returning the last item from the stack instead of unpacking that item?
I don’t understand your doubt
– Maniero
If the top points to the empty space above the last stack item, I decreasing the top -1, I won’t end up returning the last empty space instead of unpacking 1 item?
– enriq
What is empty space above the last item?
– Maniero
The memory space, on top of the last stack item.
– enriq