简体   繁体   中英

Circular queue array implementation

Im trying to create a waiting list which will hold names of customers in a static array when the main array is full.and When the main array gets an EMPTY slot the first customer in the waiting list array will fill up the EMPTY slot of main array and the added element will be removed Im trying to create this using circular queue implementation.following the FIFO(First in first out) system

This is the circular queue implementation I have come up with

public class CQueue {

int SIZE = 4; 
int front, rear;
int items[] = new int[4];

void initialize (String[]task) {
    for (int i = 0; i < task.length; i++) {
        task[i] = "FULL";
    }
}

CQueue() {
    front = -1;
    rear = -1;
}

boolean isFull() {
    if (front == 0 && rear == SIZE - 1) {
        return true;
    }
    if (front == rear + 1) {
        return true;
    }
    return false;
}

boolean isEmpty() {
    if (front == -1)
        return true;
    else
        return false;
}

void enQueue(int element) {
    if (isFull()) {
        System.out.println("Queue is full");
    } else {
        if (front == -1)
            front = 0;
        rear = (rear + 1) % SIZE;
        items[rear] = element;
        System.out.println("Inserted " + element);
    }
}


int deQueue() {
    int element;
    if (isEmpty()) {
        System.out.println("Queue is empty");
        return (-1);
    } else {
        element = items[front];
        if (front == rear) {
            front = -1;
            rear = -1;
        } 
        else {
            front = (front + 1) % SIZE;
        }
        return (element);
    }
}

void display() {
    int i;
    if (isEmpty()) {
        System.out.println("Empty Queue");
    } else {
        System.out.println("Front -> " + front);
        System.out.println("Items -> ");
        for (i = front; i != rear; i = (i + 1) % SIZE)
            System.out.print(items[i] + " ");
        System.out.println(items[i]);
        System.out.println("Rear -> " + rear);
    }
}

This the delete method which will take user input to delete the element from task array and add the first come element of queue.

void deleteArr(String task[]) {
    CQueue q = new CQueue();
    int NUM;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter customer num to delete : ");
    NUM = sc.nextInt()-1;
    task[NUM] = "EMPTY";
    int element = items[front];
    task[NUM]=Integer.toString(element);
    q.deQueue();
    q.display();
}

The main method

    public static void main(String[] args) {
        int k =1;
        String task[] = new String[12];
        CQueue q = new CQueue();
        q.initialize(task);
        q.display();
        for (int i = 0; i < task.length; i++) {
            if (task[i].equals("FULL")) {
                q.enQueue(k);
                k++;
            }
        }
        while (true) {
            q.deleteArr(task);
            for (int j = 0; j < task.length; j++) {
                System.out.println(task[j]);
            }
        }

}

I am stuck on how to add the queue element to the task array when a task array is deleted

I'd recommend you to check if front>=0 in deteleArr function. Also you should use the queue instance defined in main, and not a new instance:

void deleteArr(String task[]) {
    int NUM;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter customer num to delete : ");
    NUM = sc.nextInt()-1;
    task[NUM] = "EMPTY";
    if(front>=0) {
        task[NUM]=Integer.toString(items[front]);
    }
    this.deQueue();
}

If you want, you can use the following display queue function:

void display() {
    int i;
    System.out.println("Print queue");
    System.out.println("===========");
    if (isEmpty()) {
        System.out.println("Empty Queue");
    } else {
        System.out.println("Front -> " + front);
        for (i = front; i <= rear; i++) {
            System.out.println("item["+i+"]: " +items[i]);
        }
        System.out.println("Rear -> " + rear);
    }
    System.out.println("============");
}

And you can use this another function to display the task array:

public static void printTaskArray(String task[]) {
    System.out.println("Print task[]");
    System.out.println("============");
    if (task.length==0) {
        System.out.println("Empty task");
    } else {
        for (int j = 0; j < task.length; j++) {             
            System.out.println("task["+j+"]: "+task[j]);
        }
    }
    System.out.println("============");
}

With this encapsulation you can change your main as follows:

public static void main(String[] args) {
    int k =1;
    String task[] = new String[12];
    CQueue q = new CQueue ();
    q.initialize(task);
    printTaskArray(task);
    q.display();
    for (int i = 0; i < task.length; i++) {
        if (task[i].equals("FULL")) {
            q.enQueue(k);
            k++;
        }
    }
    printTaskArray(task);
    q.display();
    while (true) {
        q.deleteArr(task);
        printTaskArray(task);
        q.display();
    }
}

I hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM