Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Cola {
- private int[] contenedor;
- private int head;
- private int tail;
- private int contador;
- private int capacidad=10;
- public Cola(){
- this.contenedor=new int[capacidad];
- this.head=0;
- this.tail=0;
- this.contador=0;
- }
- public Cola(int tamaño){
- this.head=0;
- this.tail=0;
- this.contador=0;
- this.capacidad=tamaño;
- this.contenedor=new int[tamaño];
- }
- public boolean vacia(){
- if(this.head == this.tail)
- {
- return(true);
- }
- else
- {
- return false;
- }
- }
- private int siguiente(int posicion){
- ++posicion;
- if(posicion>capacidad){
- posicion=0;
- }
- return posicion;
- }
- public void encolar(int elemento){
- if(contador>=capacidad){
- throw new RuntimeException("La cola esta llena.");
- }
- contenedor[tail]=elemento;
- tail=siguiente(tail);
- contador=contador+1;
- }
- public void quitar(){
- if (this.head == this.tail)
- {
- System.out.println("la cola esta vacia");
- }
- else
- {
- for(int i= head ; i < tail - 1; i ++)
- {
- contenedor[i]=contenedor[i+1];
- }
- tail --;
- contador --;
- }
- }
- public int mostrarPrimero(){
- return contenedor[tail];
- }
- public void desplegarCola(){
- if (this.vacia())
- {
- System.out.println("la cola esta vacia");
- }
- else
- {
- String Mostrar = "";
- for (int i=head;i<tail;i++)
- {
- Mostrar = Mostrar + contenedor[i]+"\n";
- }
- System.out.println("Total es: "+ this.tail+"\n"+"Los elementos de la cola: \n"+ Mostrar);
- }
- }
- public int getCapacidad(){
- return capacidad;
- }
- public int getContador(){
- return contador;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement