Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Livro{
- int num;
- Livro prox;
- public Livro(int num){
- this.num = num;
- this.prox = null;
- }
- }
- class ListaUnica{
- Livro topo;
- int tamanho;
- public ListaUnica(){
- this.topo=null;
- }
- public void inserir(Livro livro){
- if(topo == null){
- this.topo = livro;
- this.tamanho++;
- } else {
- livro.prox = topo;
- this.topo = livro;
- this.tamanho++;
- }
- }
- public int remover(){
- Livro antigoTopo;
- if(this.topo.prox == null){
- antigoTopo = this.topo;
- this.topo = null;
- return antigoTopo.num;
- } else {
- antigoTopo = this.topo;
- Livro novoTopo = this.topo.prox;
- this.topo.prox = null;
- this.topo = novoTopo;
- tamanho--;
- return antigoTopo.num;
- }
- }
- public void inverter(int quantidade){
- if(topo == null){
- } else {
- Livro[] trocados = new Livro[quantidade];
- trocados[0] = this.topo;
- for(int i=1;i<quantidade;i++){
- trocados[i] = trocados[i-1].prox;
- }
- Livro elo = trocados[quantidade-1].prox;
- for(int i=(quantidade-1);i>0;i--){
- trocados[i].prox = trocados[i-1];
- }
- trocados[0].prox = elo;
- this.topo = trocados[quantidade-1];
- }
- }
- }
- public class L1Q4 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Arquivo arquivo = new Arquivo("L1Q4.in", "L1Q4.out");
- int comando = 1, caso = 0, contador = 0, quantidade = 0;
- ListaUnica novaLista = new ListaUnica();
- while(!arquivo.isEndOfFile()){
- if(contador == 0){
- novaLista = new ListaUnica();
- caso++;
- arquivo.print("caso #"+caso+": ");
- contador++;
- quantidade = arquivo.readInt();
- } else {
- while(comando != 0){
- comando = arquivo.readInt();
- switch(comando){
- case 1 :
- int novonumero = arquivo.readInt();
- Livro novoLivro = new Livro(novonumero);
- novaLista.inserir(novoLivro);
- break;
- case 2 :
- if(quantidade <= novaLista.tamanho){
- novaLista.inverter(quantidade);
- } else {
- novaLista.inverter(novaLista.tamanho);
- }
- break;
- case 3 :
- arquivo.print(novaLista.remover()+ " ");
- break;
- }
- }
- contador = 0;
- arquivo.println("");
- comando = 1;
- }
- }
- arquivo.close();
- }
- }
Add Comment
Please, Sign In to add comment