Advertisement
anas_harby

Untitled

May 4th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package egu.edu.alexu.csd.datastructure.queue.cs23;
  2.  
  3. import eg.edu.alexu.csd.datastructure.queue.IArrayBased;
  4. import eg.edu.alexu.csd.datastructure.queue.IQueue;
  5.  
  6. public class QueueArrayBased implements IQueue, IArrayBased {
  7.  
  8.     int N,f=0,r=0;
  9.     public QueueArrayBased(int N) {
  10.         this.N = N;
  11.     }
  12.    
  13.     Object[] queueArray = new Object[N];
  14.     int size = 0;
  15.    
  16.     @Override
  17.     public void enqueue(Object item) {
  18.         if(size==N)
  19.             throw new RuntimeException();
  20.         else {
  21.             queueArray[r] = item;
  22.             r++;
  23.             if(r==N)
  24.                 r=0;
  25.             size++;
  26.         }
  27.  
  28.     }
  29.  
  30.     @Override
  31.     public Object dequeue() {
  32.         if(size==0)
  33.             throw new RuntimeException();
  34.         else {
  35.             Object temp = queueArray[f];
  36.             queueArray[f] = null;
  37.             f++;
  38.             if(f==N)
  39.                 f=0;
  40.             size--;
  41.             return temp;
  42.         }
  43.     }
  44.  
  45.     @Override
  46.     public boolean isEmpty() {
  47.         if(size==0)
  48.             return true;
  49.         else
  50.             return false;
  51.     }
  52.  
  53.     @Override
  54.     public int size() {
  55.         return size;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement