Advertisement
Montoya-Romina-Anahi

Tp3_P1_ClassCola

Jun 23rd, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. public class Cola {
  2. private int[] contenedor;
  3.  
  4. private int head;
  5.  
  6. private int tail;
  7.  
  8. private int contador;
  9.  
  10. private int capacidad=10;
  11.  
  12. public Cola(){
  13.  
  14. this.contenedor=new int[capacidad];
  15.  
  16. this.head=0;
  17.  
  18. this.tail=0;
  19.  
  20. this.contador=0;
  21.  
  22. }
  23.  
  24.  
  25. public Cola(int tamaño){
  26.  
  27. this.head=0;
  28.  
  29. this.tail=0;
  30.  
  31. this.contador=0;
  32.  
  33. this.capacidad=tamaño;
  34.  
  35. this.contenedor=new int[tamaño];
  36.  
  37. }
  38.  
  39. public boolean vacia(){
  40. if(this.head == this.tail)
  41. {
  42. return(true);
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48.  
  49. }
  50.  
  51. private int siguiente(int posicion){
  52.  
  53. ++posicion;
  54.  
  55. if(posicion>capacidad){
  56.  
  57. posicion=0;
  58. }
  59. return posicion;
  60.  
  61. }
  62.  
  63.  
  64. public void encolar(int elemento){
  65.  
  66. if(contador>=capacidad){
  67.  
  68. throw new RuntimeException("La cola esta llena.");
  69. }
  70.  
  71. contenedor[tail]=elemento;
  72.  
  73. tail=siguiente(tail);
  74.  
  75. contador=contador+1;
  76. }
  77.  
  78. public void quitar(){
  79. if (this.head == this.tail)
  80. {
  81. System.out.println("la cola esta vacia");
  82. }
  83. else
  84. {
  85. for(int i= head ; i < tail - 1; i ++)
  86. {
  87. contenedor[i]=contenedor[i+1];
  88. }
  89. tail --;
  90. contador --;
  91. }
  92. }
  93.  
  94.  
  95. public int mostrarPrimero(){
  96.  
  97. return contenedor[tail];
  98.  
  99. }
  100.  
  101. public void desplegarCola(){
  102. if (this.vacia())
  103. {
  104. System.out.println("la cola esta vacia");
  105. }
  106. else
  107. {
  108. String Mostrar = "";
  109. for (int i=head;i<tail;i++)
  110. {
  111. Mostrar = Mostrar + contenedor[i]+"\n";
  112.  
  113.  
  114. }
  115. System.out.println("Total es: "+ this.tail+"\n"+"Los elementos de la cola: \n"+ Mostrar);
  116.  
  117. }
  118.  
  119. }
  120.  
  121.  
  122. public int getCapacidad(){
  123.  
  124. return capacidad;
  125.  
  126. }
  127.  
  128. public int getContador(){
  129.  
  130. return contador;
  131.  
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement