Advertisement
LEANDRONIEVA

Cola

Oct 12th, 2022
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. public class Cola {
  2.    
  3.     private char[] array = new char[10];
  4.     private int Head;
  5.     private int Tail;
  6.     private int count;
  7.    
  8.     public Cola() {
  9.         this.Head = 0;
  10.         this.Tail = 0;
  11.     }
  12.    
  13.     public Cola(int n) {
  14.         this.array = new char[n];
  15.         this.Head = 0;
  16.         this.Tail = 0;
  17.         this.count = 0;
  18.     }
  19.  
  20.     private int next(int pos) {
  21.          if (++pos >= this.array.length) {
  22.              pos = 0;
  23.          }
  24.          return pos;
  25.      }
  26.    
  27.     public boolean isEmpty(){
  28.         return this.Head==this.Tail;
  29.     }
  30.    
  31.     public boolean isFull() {
  32.         return this.Head==this.next(this.Tail);
  33.     }
  34.    
  35.     public void enQueue(char element){
  36.         if(isFull()) {
  37.             System.out.println("La cola está llena");
  38.         }else {
  39.             this.array[this.Tail]=element;
  40.             this.Tail++;
  41.             this.count++;
  42.             if (this.Tail==this.array.length)this.Tail=0;
  43.         }
  44.     }
  45.    
  46.     public char deQueue() {
  47.         char eliminado = ' ';
  48.         if(isEmpty()) {
  49.             System.out.println("La cola está vacía");
  50.         }else {
  51.             eliminado = this.array[this.Head];
  52.             this.Head++;
  53.             this.count--;
  54.             if(this.Head==this.array.length)this.Head=0;
  55.         }
  56.        
  57.         return eliminado;
  58.     }
  59.    
  60.     public char peek() {
  61.         char primero = ' ';
  62.        
  63.         if(isEmpty()) {
  64.             System.out.println("La cola está vacía");
  65.         }else {
  66.             primero = this.array[this.Head];
  67.         }
  68.        
  69.         return primero;
  70.     }
  71.    
  72.     public int size() {
  73.         return this.count;
  74.     }
  75.    
  76.     public void tostring() {
  77.         if(isEmpty()) {
  78.             System.out.println("La cola está vacía");
  79.         }else {
  80.             int aux = 0, i= this.Head;
  81.             while(aux < this.size()) {
  82.                 System.out.print(this.array[i]+" ");
  83.                 aux++;
  84.                 i++;
  85.                
  86.                 if(i == this.array.length)i=0;
  87.             }
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement