Guest User

Untitled

a guest
Feb 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package practica1;
  2.  
  3. public class ContenedorDeEnteros {
  4.  
  5. private Nodo primero;
  6. private int size;
  7.  
  8. public ContenedorDeEnteros() {
  9. primero = null;
  10. size = 0;
  11. }
  12.  
  13. public int cardinal() {
  14. return size;
  15. }
  16.  
  17. public boolean insertar(int valor) {
  18. if(buscar(valor)) {
  19. return false;
  20. }
  21. Nodo nuevo = new Nodo(valor, primero);
  22. primero = nuevo;
  23. size++;
  24. return true;
  25. }
  26.  
  27. public boolean extraer(int valor) {
  28. if(buscar(valor)) {
  29. int pos = indexOf(valor);
  30. if(pos == 0) {
  31. Nodo actual1 = primero.siguiente;
  32. primero = actual1;
  33. size--;
  34. return true;
  35. }
  36. Nodo actual2 = getNodo(pos - 1);
  37. actual2.siguiente = actual2.siguiente.siguiente;
  38. size--;
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. public boolean buscar(int valor) {
  45. if(indexOf(valor) != -1) {
  46. return true;
  47. }
  48. return false;
  49. }
  50.  
  51. public void vaciar() {
  52. primero = null;
  53. size = 0;
  54. }
  55.  
  56. public int[] elementos() {
  57. int[] res = new int[size];
  58. for(int i = 0; i < size; i++) {
  59. res[i] = getNodo(i).valor;
  60. }
  61. return res;
  62. }
  63.  
  64. private Nodo getNodo(int ind) {
  65. Nodo actual = primero;
  66. for(int i = 0; i < ind; i++) {
  67. actual = actual.siguiente;
  68. }
  69. return actual;
  70. }
  71.  
  72. private int indexOf(int valor) {
  73. if(size == 0) {
  74. return -1;
  75. }else {
  76. Nodo actual = primero;
  77. int pos = 0;
  78. while(actual != null) {
  79. if(actual.valor == valor) {
  80. return pos;
  81. }
  82. actual = actual.siguiente;
  83. pos++;
  84. }
  85. return -1;
  86. }
  87. }
  88.  
  89. private class Nodo{
  90. private int valor;
  91. private Nodo siguiente;
  92.  
  93. public Nodo(int value, Nodo next) {
  94. valor = value;
  95. siguiente = next;
  96. }
  97. }
  98. }
Add Comment
Please, Sign In to add comment