Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Vector;
- public class PilhaNumerica<T>
- {
- public Vector<T> vetor;
- public int topo;
- public PilhaNumerica()
- {
- vetor = new Vector<T>();
- topo = -1;
- }
- public boolean vazia()
- {
- return topo == -1 ? true : false;
- }
- public T pop()
- {
- T elemento;
- if (!this.vazia()){
- elemento = vetor.get(topo--);
- return elemento;
- }
- else{
- return null;
- }
- }
- public void push(T elemento)
- {
- vetor.add(++topo, elemento);
- ordena();
- }
- public boolean retornaTopo(T elemento)
- {
- if(!this.vazia()){
- elemento = vetor.get(topo);
- return true;
- }
- else
- return false;
- }
- public void ordena() {
- int [] vetorOrdena = new int[this.vetor.size()];
- for(int i = 0; i < this.vetor.size();i++) {
- vetorOrdena[i] = (int) vetor.get(i);
- }
- int aux = 0;
- for (int i = 0; i < vetorOrdena.length; i++)
- {
- for (int j = 0; j < vetorOrdena.length; j++)
- {
- if (vetorOrdena[i] < vetorOrdena[j])
- {
- aux = vetorOrdena[i];
- vetorOrdena[i] = vetorOrdena[j];
- vetorOrdena[j] = aux;
- }
- }
- }
- this.vetor.clear();
- for (int i = 0; i < vetorOrdena.length; i++)
- {
- vetor.add((T) vetorOrdena);
- }
- }
- public static void main(String args[])
- {
- Scanner scanner = new Scanner(System.in);
- PilhaNumerica pilha = new PilhaNumerica<Integer>();
- pilha.push(1);
- pilha.push(9);
- pilha.push(10);
- pilha.push(5);
- pilha.push(3);
- for(int i = 0; i < pilha.vetor.size();i++) {
- System.out.println(pilha.pop());
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement