Advertisement
makispaiktis

Lab 2 - Queue

Dec 3rd, 2018 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. public class Queue {
  2.  
  3.     long[] queueArray;
  4.     int front;
  5.     int rear;
  6.     int maxSize;
  7.     int nItems;
  8.    
  9.     // Constructors
  10.     public Queue(){
  11.        
  12.         front = 0;
  13.         rear = -1;
  14.         maxSize = 0;
  15.         nItems = 0;
  16.         queueArray = new long[0];
  17.     }
  18.    
  19.     public Queue(int maxSize) {
  20.        
  21.         front = 0;
  22.         rear = -1;
  23.         this.maxSize = maxSize;
  24.         nItems = 0;
  25.         queueArray = new long[maxSize];
  26.     }
  27.    
  28.     // Methods
  29.     public boolean isEmpty() {
  30.        
  31.         if(nItems == 0) {
  32.            
  33.             return true;
  34.         }
  35.        
  36.         else {
  37.            
  38.             return false;
  39.         }
  40.     }
  41.    
  42.     public boolean isFull() {
  43.        
  44.         return (nItems == maxSize);
  45.     }
  46.    
  47.     public int size() {
  48.        
  49.         return nItems;
  50.     }
  51.    
  52.     public void printQueue() {
  53.        
  54.         for(int i=front; i<=rear; i++) {
  55.            
  56.             System.out.print(queueArray[i] + "  ");
  57.         }
  58.        
  59.         System.out.println();
  60.         System.out.println();
  61.     }
  62.    
  63.     public void insert(long j) {
  64.        
  65.         if(isFull()) {
  66.            
  67.             System.out.println("Queue is full! " + j + " cannot be inserted.");
  68.         }
  69.        
  70.         else {
  71.            
  72.             //if(rear == maxSize - 1) {
  73.                
  74.                 for(int i=0; i<nItems; i++) {
  75.                    
  76.                     // Shift
  77.                     queueArray[i] = queueArray[i + front];
  78.                 }
  79.                
  80.                 front = 0;
  81.                 rear = nItems - 1;
  82.                
  83.             //}
  84.            
  85.             rear++;
  86.             queueArray[rear] = j;
  87.             nItems++;
  88.         }
  89.     }
  90.    
  91.     public long remove() {
  92.        
  93.         if(isEmpty()) {
  94.            
  95.             System.out.println("Queue is empty. There are no items to be removed.");
  96.             return -10000;
  97.         }
  98.        
  99.         long temp = queueArray[front];
  100.         front++;
  101.         nItems--;
  102.        
  103.         if(front == maxSize) {
  104.            
  105.             front = 0;
  106.             rear = -1;
  107.         }
  108.        
  109.         return temp;
  110.     }
  111.    
  112.     public long peek() {
  113.        
  114.         if(isEmpty()) {
  115.            
  116.             System.out.println("Queue is empty. Could not return the first element of the queue.");
  117.             return -10000;
  118.         }
  119.        
  120.         return queueArray[front];
  121.     }
  122.    
  123.     public static void main(String[] args) {
  124.        
  125.         Queue queue = new Queue(5);
  126.        
  127.         // 1st insert
  128.         System.out .println("After the insert of 10, 20, 30, 40 ,45, 50:");
  129.         queue.insert(10);
  130.         queue.insert(20);
  131.         queue.insert(30);
  132.         queue.insert(40);
  133.         queue.insert(45);
  134.         queue.insert(50);
  135.         queue.printQueue();
  136.        
  137.         // 1st remove
  138.         System.out.println("After removing the 3 first elements:");
  139.         for(int i=0; i<3; i++) {
  140.            
  141.             queue.remove();
  142.         }
  143.        
  144.         queue.printQueue();
  145.        
  146.         // 2nd insert
  147.         System.out.println("After the insert of 55, 60, 70, 80:");
  148.         queue.insert(55);
  149.         queue.insert(60);
  150.         queue.insert(70);
  151.         queue.insert(80);
  152.         queue.printQueue();
  153.        
  154.         // 2nd remove
  155.         System.out.println("After removing all the 5 elenments of the queue:");
  156.         for(int i=0; i<queue.size(); i++) {
  157.            
  158.             queue.remove();
  159.         }
  160.        
  161.        
  162.        
  163.     }
  164.    
  165.    
  166.    
  167.    
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement