Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Cola {
- private char[] array = new char[10];
- private int Head;
- private int Tail;
- private int count;
- public Cola() {
- this.Head = 0;
- this.Tail = 0;
- }
- public Cola(int n) {
- this.array = new char[n];
- this.Head = 0;
- this.Tail = 0;
- this.count = 0;
- }
- private int next(int pos) {
- if (++pos >= this.array.length) {
- pos = 0;
- }
- return pos;
- }
- public boolean isEmpty(){
- return this.Head==this.Tail;
- }
- public boolean isFull() {
- return this.Head==this.next(this.Tail);
- }
- public void enQueue(char element){
- if(isFull()) {
- System.out.println("La cola está llena");
- }else {
- this.array[this.Tail]=element;
- this.Tail++;
- this.count++;
- if (this.Tail==this.array.length)this.Tail=0;
- }
- }
- public char deQueue() {
- char eliminado = ' ';
- if(isEmpty()) {
- System.out.println("La cola está vacía");
- }else {
- eliminado = this.array[this.Head];
- this.Head++;
- this.count--;
- if(this.Head==this.array.length)this.Head=0;
- }
- return eliminado;
- }
- public char peek() {
- char primero = ' ';
- if(isEmpty()) {
- System.out.println("La cola está vacía");
- }else {
- primero = this.array[this.Head];
- }
- return primero;
- }
- public int size() {
- return this.count;
- }
- public void tostring() {
- if(isEmpty()) {
- System.out.println("La cola está vacía");
- }else {
- int aux = 0, i= this.Head;
- while(aux < this.size()) {
- System.out.print(this.array[i]+" ");
- aux++;
- i++;
- if(i == this.array.length)i=0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement