Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HuxleyCode {
- static int eleBuffer = 0;
- static int preenche = 0;
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner in = new Scanner(System.in);
- Pilha memAuxiliar = new Pilha();
- Fila memPrincipal = new Fila();
- int eleBufferaux =0;
- int retira = 0;
- int tamBuffer = in.nextInt();
- int operacoes = in.nextInt();
- int numero = 0;
- int contOperacoes = 0;
- char recebido = 'R';
- String a = "";
- in.nextLine();
- while (in.hasNext()) {
- String entrada = in.nextLine();
- int tam = entrada.length();
- for (int b = 5; b < tam; b++) {
- a = a + entrada.charAt(b);
- }
- int entrada2 = Integer.parseInt(a);
- if (contOperacoes % operacoes == 0) {
- while (eleBuffer != tamBuffer && memAuxiliar.topo != null) {
- memPrincipal.enfileira(memAuxiliar.desempilha().getNumero());
- eleBuffer++;
- eleBufferaux++;
- }
- }
- if (entrada.charAt(0) == recebido) {
- while (preenche < entrada2) {
- if (eleBuffer != tamBuffer) {
- memPrincipal.enfileira(numero);
- numero++;
- eleBuffer++;
- preenche++;
- eleBufferaux++;
- }
- else {
- memAuxiliar.empilha(numero);
- numero++;
- preenche++;
- }
- }
- preenche = 0;
- }
- else {
- while (retira < entrada2 && retira != eleBufferaux) {
- if (eleBuffer != 0) {
- System.out.print(memPrincipal.desenfileira().getNumero() + " ");
- eleBuffer--;
- } else if (entrada2 == 0) {
- System.out.println();
- }
- retira++;
- }
- retira = 0;
- System.out.println();
- }
- a = "";
- contOperacoes++;
- }
- while (eleBuffer != 0) {
- System.out.print(memPrincipal.desenfileira().getNumero() + " ");
- eleBuffer--;
- }
- System.out.println();
- while (memAuxiliar.topo != null) {
- System.out.print(memAuxiliar.desempilha().getNumero() + " ");
- }
- }
- static class Fila {
- No comeco;
- No fim;
- public Fila() {
- this.comeco = this.fim = null;
- }
- void enfileira(int numero) {
- No no = new No(numero);
- if (this.fim == null) {
- this.comeco = this.fim = no;
- return;
- }
- this.fim.proximo = no;
- this.fim = no;
- }
- No desenfileira() {
- if (this.comeco == null)
- return null;
- No no = this.comeco;
- this.comeco = this.comeco.proximo;
- if (this.comeco == null)
- this.fim = null;
- return no;
- }
- }
- static class No {
- int numero;
- No proximo;
- public No(int numero) {
- this.numero = numero;
- this.proximo = null;
- }
- public int getNumero() {
- return numero;
- }
- }
- static class Pilha {
- No topo;
- public Pilha() {
- }
- void empilha(int numero) {
- No no = new No(numero);
- no.proximo = topo;
- topo = no;
- }
- No desempilha() {
- No no = topo;
- topo = no.proximo;
- return no;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement