Advertisement
Eulis

Untitled

Aug 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HuxleyCode {
  4.  
  5. static int eleBuffer = 0;
  6. static int preenche = 0;
  7.  
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. Scanner in = new Scanner(System.in);
  11. Pilha memAuxiliar = new Pilha();
  12. Fila memPrincipal = new Fila();
  13. int eleBufferaux =0;
  14. int retira = 0;
  15. int tamBuffer = in.nextInt();
  16. int operacoes = in.nextInt();
  17. int numero = 0;
  18. int contOperacoes = 0;
  19.  
  20. char recebido = 'R';
  21. String a = "";
  22.  
  23. in.nextLine();
  24.  
  25.  
  26.  
  27. while (in.hasNext()) {
  28.  
  29. String entrada = in.nextLine();
  30. int tam = entrada.length();
  31.  
  32. for (int b = 5; b < tam; b++) {
  33. a = a + entrada.charAt(b);
  34. }
  35.  
  36. int entrada2 = Integer.parseInt(a);
  37.  
  38. if (contOperacoes % operacoes == 0) {
  39. while (eleBuffer != tamBuffer && memAuxiliar.topo != null) {
  40. memPrincipal.enfileira(memAuxiliar.desempilha().getNumero());
  41. eleBuffer++;
  42. eleBufferaux++;
  43. }
  44. }
  45.  
  46. if (entrada.charAt(0) == recebido) {
  47.  
  48. while (preenche < entrada2) {
  49.  
  50. if (eleBuffer != tamBuffer) {
  51. memPrincipal.enfileira(numero);
  52. numero++;
  53. eleBuffer++;
  54. preenche++;
  55. eleBufferaux++;
  56. }
  57.  
  58. else {
  59. memAuxiliar.empilha(numero);
  60. numero++;
  61. preenche++;
  62. }
  63. }
  64. preenche = 0;
  65. }
  66.  
  67. else {
  68.  
  69. while (retira < entrada2 && retira != eleBufferaux) {
  70.  
  71. if (eleBuffer != 0) {
  72. System.out.print(memPrincipal.desenfileira().getNumero() + " ");
  73. eleBuffer--;
  74. } else if (entrada2 == 0) {
  75. System.out.println();
  76. }
  77. retira++;
  78. }
  79. retira = 0;
  80. System.out.println();
  81.  
  82. }
  83.  
  84. a = "";
  85. contOperacoes++;
  86.  
  87.  
  88. }
  89.  
  90. while (eleBuffer != 0) {
  91. System.out.print(memPrincipal.desenfileira().getNumero() + " ");
  92. eleBuffer--;
  93. }
  94. System.out.println();
  95. while (memAuxiliar.topo != null) {
  96. System.out.print(memAuxiliar.desempilha().getNumero() + " ");
  97. }
  98.  
  99. }
  100.  
  101. static class Fila {
  102.  
  103. No comeco;
  104. No fim;
  105.  
  106. public Fila() {
  107. this.comeco = this.fim = null;
  108. }
  109.  
  110. void enfileira(int numero) {
  111.  
  112. No no = new No(numero);
  113.  
  114. if (this.fim == null) {
  115. this.comeco = this.fim = no;
  116. return;
  117. }
  118.  
  119. this.fim.proximo = no;
  120. this.fim = no;
  121.  
  122. }
  123.  
  124. No desenfileira() {
  125. if (this.comeco == null)
  126. return null;
  127.  
  128. No no = this.comeco;
  129. this.comeco = this.comeco.proximo;
  130.  
  131. if (this.comeco == null)
  132. this.fim = null;
  133. return no;
  134.  
  135. }
  136. }
  137.  
  138. static class No {
  139.  
  140. int numero;
  141.  
  142. No proximo;
  143.  
  144. public No(int numero) {
  145. this.numero = numero;
  146. this.proximo = null;
  147. }
  148.  
  149. public int getNumero() {
  150. return numero;
  151. }
  152.  
  153. }
  154.  
  155. static class Pilha {
  156. No topo;
  157.  
  158. public Pilha() {
  159.  
  160. }
  161.  
  162. void empilha(int numero) {
  163. No no = new No(numero);
  164. no.proximo = topo;
  165. topo = no;
  166.  
  167. }
  168.  
  169. No desempilha() {
  170. No no = topo;
  171. topo = no.proximo;
  172. return no;
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement