Paste Search Dynamic
Recent pastes
ArrayQueue
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class ArrayQueue
  9. {
  10.  
  11.         private int first, last, size;
  12.         private object[] storage;
  13.         public ArrayQueue() {
  14.         this(100);
  15.         }
  16.         public ArrayQueue(int n) {
  17.         size = n;
  18.         storage = new object[size];
  19.         first = last = -1;
  20.         }
  21.         public boolean isFull() {
  22.         return first == 0 && last == size-1 || first == last + 1;
  23.         }
  24.         public boolean isEmpty() {
  25.         return first == -1;
  26.         }
  27.         public void enqueue(object el) {
  28.         if (last == size-1 || last == -1) {
  29.                 storage[0] = el;
  30.                 last = 0;
  31.                 if (first == -1)
  32.                 first = 0;
  33.                 }
  34.         else storage[++last] = el;
  35.         system.out.print("nFist and Last" + "t" + first + "t" + last + "n");
  36.         }
  37.         public object dequeue() {
  38.         object tmp = storage[first];
  39.         if (first == last)
  40.                 last = first = -1;
  41.                 else if (first == size-1)
  42.                 first = 0;
  43.         else first++;
  44.         system.out.print("nFist and Last" + "t" + first + "t" + last + "n");
  45.         return tmp;
  46.         }
  47.         public void printAll() {
  48.         for (int i = 0; i < size; i++)
  49.                 system.out.print(storage[i] + " ");
  50.         }
  51.         public static void main (string[] args) throws java.lang.exception
  52.         {
  53.                 ArrayQueue MyQueue = new  ArrayQueue(6); // your code goes here
  54.                 MyQueue.enqueue(10);
  55.                 MyQueue.enqueue(15);
  56.                 MyQueue.enqueue(20);
  57.                 MyQueue.enqueue(25);
  58.                 MyQueue.enqueue(30);
  59.                 MyQueue.enqueue(35);
  60.                 MyQueue.printAll();
  61.                 boolean Full = MyQueue.isFull();
  62.                 system.out.print("nThe Queue is it Full? t " + Full + "n");
  63.                 system.out.print("n 10 is Dequeued");
  64.                 object  val = MyQueue.dequeue();
  65.                 system.out.print("nThe element dequeued is t " + val + "n");
  66.                 system.out.print("n 50 is Enequeued");
  67.                 MyQueue.enqueue(50);
  68.                 system.out.print("n 15 is Dequeued");
  69.                 val = MyQueue.dequeue();
  70.                 system.out.print("nThe element dequeued is t " + val + "n");
  71.                 system.out.print("n 60 is Enequeued");
  72.                 MyQueue.enqueue(50);
  73.                 MyQueue.printAll();
  74.  
  75.         }
  76. }
Parsed in 0.040 seconds