Advertisement
Guest User

N AGUENTO MAIS ESSES ERROS

a guest
Mar 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Vector;
  3.  
  4. public class PilhaNumerica<T>
  5. {
  6. public Vector<T> vetor;
  7. public int topo;
  8.  
  9. public PilhaNumerica()
  10. {
  11.  
  12. vetor = new Vector<T>();
  13. topo = -1;
  14. }
  15.  
  16. public boolean vazia()
  17. {
  18. return topo == -1 ? true : false;
  19. }
  20.  
  21.  
  22. public T pop()
  23. {
  24. T elemento;
  25.  
  26. if (!this.vazia()){
  27.  
  28. elemento = vetor.get(topo--);
  29. return elemento;
  30. }
  31. else{
  32. return null;
  33. }
  34. }
  35.  
  36. public void push(T elemento)
  37. {
  38. vetor.add(++topo, elemento);
  39. ordena();
  40. }
  41.  
  42. public boolean retornaTopo(T elemento)
  43. {
  44. if(!this.vazia()){
  45. elemento = vetor.get(topo);
  46. return true;
  47. }
  48. else
  49. return false;
  50. }
  51.  
  52. public void ordena() {
  53.  
  54. int [] vetorOrdena = new int[this.vetor.size()];
  55.  
  56. for(int i = 0; i < this.vetor.size();i++) {
  57. vetorOrdena[i] = (int) vetor.get(i);
  58. }
  59.  
  60. int aux = 0;
  61.  
  62. for (int i = 0; i < vetorOrdena.length; i++)
  63. {
  64. for (int j = 0; j < vetorOrdena.length; j++)
  65. {
  66. if (vetorOrdena[i] < vetorOrdena[j])
  67. {
  68. aux = vetorOrdena[i];
  69. vetorOrdena[i] = vetorOrdena[j];
  70. vetorOrdena[j] = aux;
  71. }
  72. }
  73. }
  74. this.vetor.clear();
  75. for (int i = 0; i < vetorOrdena.length; i++)
  76. {
  77. vetor.add((T) vetorOrdena);
  78. }
  79. }
  80.  
  81. public static void main(String args[])
  82. {
  83. Scanner scanner = new Scanner(System.in);
  84.  
  85. PilhaNumerica pilha = new PilhaNumerica<Integer>();
  86.  
  87. pilha.push(1);
  88. pilha.push(9);
  89. pilha.push(10);
  90. pilha.push(5);
  91. pilha.push(3);
  92.  
  93. for(int i = 0; i < pilha.vetor.size();i++) {
  94. System.out.println(pilha.pop());
  95. }
  96.  
  97. scanner.close();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement