Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.2 kB
0
Indexable
Never
package Assignment2;
public class QueueArray
{
    private int[] array;
    private int front;
    private int rear;

    public QueueArray(int size)
    {
        array = new int[size];
        front = -1;
        rear = -1;
    }

    public void enqueue(int x)
    {
        if (rear == array.length - 1)
        {
            System.out.println("Queue is full");
        }
        else
        {
            if (front == -1)
            {
                front = 0;
            }
            rear++;
            array[rear] = x;
        }
    }

    public int dequeue()
    {
        int result = -1;
        if (front == -1 || front > rear)
        {
            System.out.println("Queue is empty");
        }
        else
        {
            result = array[front];
            front++;
        }
        return result;
    }

    public void display()
    {
        if (front == -1 || front > rear)
        {
            System.out.println("Queue is empty");
        }
        else
        {
            for (int i = front; i <= rear; i++)
            {
                System.out.print(array[i] + " ");
            }
            System.out.println();
        }
    }
}