将添加到以前的Queue类对象

时间:2020-12-12 07:19:25

I have written an ObjectQueue class to handle objects, but i would like to also be able to handle LinkedList types, so that i will have a queue of objects using a linked list. is this even possible?

我编写了一个ObjectQueue类来处理对象,但我也希望能够处理LinkedList类型,这样我就可以使用链表来拥有一个对象队列。这有可能吗?

basically i want something similar to

基本上我想要类似的东西

ArrayList<LinkedList <Job> > data

except with: ObjectQueue<LinkedList <Job> > data

除了:ObjectQueue >数据

how could i modify my class:

我怎么能修改我的课程:

public class ObjectQueue {
      private Object[] item;
      private int front;
      private int rear;
      private int size;


    public ObjectQueue() {
        size = 100;
        item = new Object[size];
        front = size-1;
        rear  = size-1;
    }

    public ObjectQueue(int max) {
        size = max;
        item = new Object[size];
        front = size-1;
        rear  = size-1;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
       return rear == size-1 ? front == 0 : front == rear+1;
    }

    public void clear() {
        front = size-1;
        rear  = size-1;
    }


    public void insert(Object x) {
        if (isFull()) {
            System.out.println("Insert Runtime Error: Queue Overflow");
            System.exit(1);
        }
        if (rear == size-1)         // or rear = (rear+1) % size;
            rear = 0;
        else
            rear++;
        item[rear] = x;
    }

    public Object remove() {
        if (isEmpty()) {
            System.out.println("Remove Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)            // or front = (front+1) % size;
            front = 0;
        else
            front++;
        return item[front];
    }

    public Object query() {
        if (isEmpty()) {
            System.out.println("Query Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)
            return item[0];
        else
            return item[front+1];
    }
}

1 个解决方案

#1


0  

You need to make your class generic:

你需要让你的类通用:

public class ObjectQueue<T> {
    ...
    public void insert(T x) {
        ...
    }
    public T remove() {
        ...
    }
    ...
}

#1


0  

You need to make your class generic:

你需要让你的类通用:

public class ObjectQueue<T> {
    ...
    public void insert(T x) {
        ...
    }
    public T remove() {
        ...
    }
    ...
}