Created
July 10, 2010 21:15
-
-
Save edipofederle/471034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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