Given the following Queue implemented as a Linked List Write

Given the following Queue implemented as a Linked List Write an equivalent Queue implemented as array of integer.

Solution

Tested on Eclipse

/**********************QueueImpl.java*****************/

public class QueueImpl {
   /**
   * class variable declaration
   * */
   private static final int capacity = 20;
   int arr[] = new int[capacity];
   int size = 0;
   int top = -1;
   int rear = 0;

   /**
   * push method is used to push element into queue if top < capacity-1 then
   * we will push element into queue otherwise we will show queue is full
   */
   public void push(int data) {
       if (top < capacity - 1) {
           top++;
           arr[top] = data;
           System.out.println(\"Element \" + data + \" is pushed to Queue !\");
       } else {
           System.out.println(\"Queue is full !\");
       }

   }

   /**
   * pop method is used to pop element from queue if top greater then rear
   * then we will pop element from queue otherwise we will show Queue is empty
   */
   public void pop() {
       if (top >= rear) {
           rear++;
           System.out.println(\"Poped element from Queue\");
       } else {
           System.out.println(\"Queue is empty !\");
       }
   }

   public void display() {
       if (top >= rear) {
           System.out.println(\"Elements in Queue : \");
           for (int i = rear; i <= top; i++) {
               System.out.println(arr[i]);
           }
       }
   }
/**
* main method start
* */
   public static void main(String[] args) {
       /**
       * Object creation of class QueueImpl
       * */
       QueueImpl queueDemo = new QueueImpl();
       queueDemo.pop();
       queueDemo.push(23);
       queueDemo.push(2);
       queueDemo.push(73);
       queueDemo.push(21);
       System.out.println(\"************Displaying Queue after push*************\");
       queueDemo.display();
       queueDemo.pop();
       queueDemo.pop();
       System.out.println(\"**********Displaying Queue after pop************\");
       queueDemo.display();
   }
/**
* main method end
* */
}

/*******************output****************/

Queue is empty !
Element 10 is pushed to Queue !
Element 15 is pushed to Queue !
Element 54 is pushed to Queue !
Element 65 is pushed to Queue !
************Displaying Queue after push*************
Elements in Queue :
10
15
54
65
Poped element from Queue
Poped element from Queue
**********Displaying Queue after pop************
Elements in Queue :
54
65


Thanks a lot

 Given the following Queue implemented as a Linked List Write an equivalent Queue implemented as array of integer. SolutionTested on Eclipse /******************
 Given the following Queue implemented as a Linked List Write an equivalent Queue implemented as array of integer. SolutionTested on Eclipse /******************

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site