Skip to content

Instantly share code, notes, and snippets.

@edipofederle
Created July 10, 2010 21:15
Show Gist options
  • Save edipofederle/471034 to your computer and use it in GitHub Desktop.
Save edipofederle/471034 to your computer and use it in GitHub Desktop.
public class Stack {
private Element top;
private Integer tam;
private Element temp;
public Stack(){
this.top = null;
this.tam = 0;
}
public boolean empty(){
if(tam == 0){
return true;
}else{
return false;
}
}
public void add(Element element){
if(empty()){
tam++;
top = element;
element.setAntElement(null);
}else{
tam++;
temp = top;
top = element;
element.setAntElement(temp);
}
}
public Integer getTopValue(){
if(!empty())
return this.top.getElementValue();
else
return null;
}
public Element getTop(){
return this.top;
}
public boolean remove(){
if(empty()){
return true;
}else{
top = getTop().getAntElement();
tam--;
return true;
}
}
public String search(Integer elm){
if(empty()){
return "Pilha Vazia";
}else{
Element temp = getTop();
Integer temp_tam = tam;
while(temp_tam > 0){
if(temp.getElementValue() == elm)
return "Achou -> " + temp.getElementValue();
temp_tam--;
temp = temp.getAntElement();
}
}
return null;
}
public Integer tamanho(){
return this.tam;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment