Answer to Question #247559 in Algorithms for Michael Obeng

Question #247559

1.    Create a class to implement a queue using a circular array[20 marks]<o:p></o:p>

 a.     The class should contain functions to

                                    i.         Insert a new value, [5 marks]

                                  ii.         Delete a value. [5 marks]



1
Expert's answer
2021-10-06T16:09:52-0400
import java.util.ArrayList;
class Queue{
private int queueSize, frontIndex, rearIndex;
private ArrayList<Integer> queue = new ArrayList<Integer>();
Queue(int size)
{
    this.queueSize = size;
    this.frontIndex = this.rearIndex = -1;
}
public void enQueue(int data)
{   

    if((frontIndex == 0 && rearIndex == size - 1) ||
      (rearIndex == (frontIndex - 1) % (size - 1))){
        System.out.print("Queue is full");} 
    else if(frontIndex == -1){
        frontIndex = 0;
        rearIndex = 0;
        queue.add(rearIndex, data);} 
    else if(rearIndex == size - 1 && frontIndex != 0){rearIndex = 0;
     queue.set(rearIndex, data);}
     else{ rearIndex = (rearIndex + 1);  
        if(frontIndex <= rearIndex){queue.add(rearIndex, data);}
        else {queue.set(rearIndex, data);}
    }
}
 public int deQueue(){
    int temp;
    if(frontIndex == -1){
        System.out.print("Queue is Empty");
        return -1;}
     temp = queue.get(frontIndex); 
    if(frontIndex == rearIndex){
        frontIndex = -1;
        rearIndex = -1;} 
    else if(front == size - 1){frontIndex = 0;}
    else{frontIndex = frontIndex + 1;}    
    return temp;}
public void displayQueue(){     
    if(frontIndex == -1)  {
        System.out.print("Queue is Empty");
        return; } 
    System.out.print("Number of elements in the circular queue are: ");
     if(rearIndex >= frontIndex)    {
        for(int i = frontIndex; i <= rearIndex; i++){
            System.out.print(queue.get(i));
            System.out.print(" ");        }
        System.out.println();}
else    {
        for(int i = frontIndex; i < queueSize; i++)       {
            System.out.print(queue.get(i));
            System.out.print(" ");        }
        for(int i = 0; i <= rearIndex; i++)        {
            System.out.print(queue.get(i));
            System.out.print(" ");        }
        System.out.println();    } }
public static void main(String[] args){
    CircularQueue q = new CircularQueue(5);     
    q.enQueue(14);
    q.enQueue(22);
    q.enQueue(13);
    q.enQueue(-6);     
    q.displayQueue();
     int x = q.deQueue();
    if(x != -1)    {
        System.out.print("Deleted value = ");
        System.out.println(x);    }
x = q.deQueue();
    if(x != -1)    {
        System.out.print("Deleted value = ");
        System.out.println(x);    }
     q.displayQueue();
         q.enQueue(9);
    q.enQueue(20);
    q.enQueue(5);     
    q.displayQueue();     
    q.enQueue(20);}}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS