Advertisement
jtentor

DemoQueue1 - Queue.java

May 17th, 2020
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public class Queue<ELEMENT> {
  2.     private ELEMENT[] data;
  3.     private int head;
  4.     private int tail;
  5.     private int count;
  6.  
  7.     public Queue() {
  8.         this(10);
  9.     }
  10.  
  11.     public Queue(int capacity) {
  12.         this.data = (ELEMENT[]) new Object[capacity];
  13.         this.head = 0;
  14.         this.tail = 0;
  15.         this.count = 0;
  16.     }
  17.  
  18.     // Equivalent to Count property in C#
  19.     public int size() {
  20.         return this.count;
  21.     }
  22.  
  23.     private int Next(int position) {
  24.         return (++position >= this.data.length)? 0: position;
  25.     }
  26.  
  27.     // Equivalent to Enqueue method in C#
  28.     public void add(ELEMENT element) {
  29.         if (this.count >= this.data.length) {
  30.             throw new RuntimeException("La cola está llena...");
  31.         }
  32.         this.data[this.tail] = element;
  33.         this.tail = this.Next(this.tail);
  34.         ++this.count;
  35.     }
  36.  
  37.     // Equivalent to Dequeue method in C#
  38.     public ELEMENT remove() {
  39.         if (this.count <= 0) {
  40.             throw new RuntimeException("La cola está vacía...");
  41.         }
  42.         ELEMENT temp = this.data[this.head];
  43.         this.head = this.Next(this.head);
  44.         --this.count;
  45.         return temp;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement