Cola

  • Uploaded by: Erika
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cola as PDF for free.

More details

  • Words: 231
  • Pages: 3
Cola import java.util.Vector; public class Cola <Tipo> { //private int inicio; //private int fin; private int size; privateVector<Tipo> elementos; public Cola() { super(); elementos = new Vector<Tipo>(); //inicio = fin = 0; size = 0; } public boolean colaVacia () { //if ( (fin-inicio)==0) { if ( size==0) { return true; } return false; } public void encolar ( Tipo o ) { //elementos.add(fin++, o); elementos.add(size++, o); } public Tipo desencolar () { Tipo retorno; try { if(colaVacia()) throw new ErrorColaVacia(); else { //return elementos.get(inicio++); retorno = elementos.get(0); elementos.remove(0); size--; return retorno; } } catch(ErrorColaVacia error) { System.out.println("ERROR: la cola esta vacĂ­a"); return null; } }

/* public int getFin() { return fin; } public int getInicio() { return inicio; } */ public int getSize() { //return (fin-inicio); return (size); } } @SuppressWarnings("serial") class ErrorColaVacia extends Exception { public ErrorColaVacia() { super(); } }

Test public class Test { @SuppressWarnings("unchecked") public static void main(String[] args) { Cola cola; cola = new Cola<String>(); System.out.println("Elementos en cola: " + cola.getSize()); cola.encolar("Uno"); System.out.println("Elementos en cola: " + cola.getSize()); cola.encolar("Dos"); System.out.println("Elementos en cola: " + cola.getSize()); System.out.println("Extraigo.........: " + cola.desencolar().toString()); System.out.println("Elementos en cola: " + cola.getSize()); cola.encolar("Tres"); System.out.println("Elementos en cola: " + cola.getSize()); System.out.println("Extraigo.........: " + cola.desencolar().toString()); System.out.println("Elementos en cola: " + cola.getSize());

System.out.println("Extraigo.........: " + cola.desencolar().toString()); System.out.println("Elementos en cola: " + cola.getSize()); System.out.println("Extraigo.........: " + cola.desencolar().toString()); System.out.println("Elementos en cola: " + cola.getSize()); } }

Related Documents

Cola
April 2020 25
Cola
May 2020 30
Cola
November 2019 31
Coca Cola Pepsi Cola
June 2020 29
Sor-cola
June 2020 9
Cola Wars1
November 2019 16

More Documents from "Gr8 zaibi"

Resume
May 2020 18
Arduino.pdf
December 2019 26
Tema 4
June 2020 19
December 2019 28