1-3 2-10作业

来源:2-10 作业解析:不浪费一个空间的循环队列

不是可麗兒

2020-07-29 10:47:41

不用size的写法

public class LoopQueue2<E> implements Queue<E> {
   public static void main(String[] args) {
       LoopQueue2<Integer> queue = new LoopQueue2<>();
       for (int i = 0; i < 10; i++) {
           queue.enqueue(i);
           System.out.println(queue);
           if (i % 3 == 2) {
               queue.dequeue();
               System.out.println(queue);
           }
       }
   }

   private E[] data;
   private int front, tail;
//    private int size;

   public LoopQueue2(int capacity) {
       data = (E[]) new Object[capacity];
       front = 0;
       tail = 0;
//        size = 0;
   }

   public LoopQueue2() {
       this(10);
   }

   @Override
   public int getSize() {
       if (tail > front) {
           return tail - front;
       } else if (tail < tail) {
           return tail + data.length - front;
       } else {
           if (isEmpty()) {
               return 0;
           } else {
               return data.length;
           }
       }
   }

   @Override
   public boolean isEmpty() {
       return front == tail && data[front] == null;
   }

   public int getCapacity() {
       return data.length;
   }

   @Override
   public void enqueue(E e) {
       if (getSize() == getCapacity()) {
           resize(getCapacity() * 2);
       }
       data[tail] = e;
       tail = (tail + 1) % data.length;
   }

   @Override
   public E dequeue() {
       if (isEmpty()) {
           throw new IllegalArgumentException("Cannot deequeue from an empty queue.");
       }
       E ret = data[front];
       data[front] = null;
       front = (front + 1) % data.length;
       if (getSize() < data.length / 4 && getCapacity() / 2 != 0) {
           resize(getCapacity() / 2);
       }
       return ret;
   }

   @Override
   public E getFront() {
       if (isEmpty()) {
           throw new IllegalArgumentException("Cannot deequeue from an empty queue.");
       }
       return data[front];
   }

   private void resize(int newCapacity) {
       E[] newData = (E[]) new Object[newCapacity + 1];
       for (int i = 0; i < getSize(); i++) {
           newData[i] = data[(front + i) % data.length];
       }
       data = newData;
       front = 0;
       tail = getSize();
   }

   @Override
   public String toString() {
       StringBuilder res = new StringBuilder();
       res.append(String.format("Queue: size = %d ,capacity = %d\n", getSize(), getCapacity()));
       res.append("front [");
       for (int i = 0; i < getSize(); i++) {
           res.append(data[(front + i) % getCapacity()]);
           if ((front + i + 1) % data.length != tail) {
               res.append(", ");
           }
       }
       res.append("] rail");
       return res.toString();
   }
}

写回答

2回答

liuyubobobo

2020-07-29

在作业解析的文字里,我也给出了我的参考代码,和相应的测试用例,尝试自己测试一下自己的代码,看看是不是有什么问题?:)


继续加油!:)

0

不是可麗兒

提问者

2020-07-29

解决个bug

public class LoopQueue2<E> implements Queue<E> {
   public static void main(String[] args) {
       LoopQueue2<Integer> queue = new LoopQueue2<>();
       for (int i = 0; i < 10; i++) {
           queue.enqueue(i);
           System.out.println(queue);
           if (i % 3 == 2) {
               queue.dequeue();
               System.out.println(queue);
           }
       }
   }

   private E[] data;
   private int front, tail;
//    private int size;

   public LoopQueue2(int capacity) {
       data = (E[]) new Object[capacity];
       front = 0;
       tail = 0;
//        size = 0;
   }

   public LoopQueue2() {
       this(10);
   }

   @Override
   public int getSize() {
       if (tail > front) {
           return tail - front;
       } else if (tail < front) {
           return tail + data.length - front;
       } else {
           if (isEmpty()) {
               return 0;
           } else {
               return data.length;
           }
       }
   }

   @Override
   public boolean isEmpty() {
       return front == tail && data[front] == null;
   }

   public int getCapacity() {
       return data.length;
   }

   @Override
   public void enqueue(E e) {
       if (getSize() == getCapacity()) {
           resize(getCapacity() * 2);
       }
       data[tail] = e;
       tail = (tail + 1) % data.length;
   }

   @Override
   public E dequeue() {
       if (isEmpty()) {
           throw new IllegalArgumentException("Cannot deequeue from an empty queue.");
       }
       E ret = data[front];
       data[front] = null;
       front = (front + 1) % data.length;
       int size=getSize();
       int a=data.length / 4;
       boolean t1=size <a ;
       boolean t2=getCapacity() / 2 != 0;
       if (t1 && t2) {
           resize(getCapacity() / 2);
       }
       return ret;
   }

   @Override
   public E getFront() {
       if (isEmpty()) {
           throw new IllegalArgumentException("Cannot deequeue from an empty queue.");
       }
       return data[front];
   }

   private void resize(int newCapacity) {
       E[] newData = (E[]) new Object[newCapacity];
//        System.out.println(data.toString());
       int size=getSize();
       for (int i = 0; i < size; i++) {
           int r=(front + i) % data.length;
           newData[i] = data[r];
           print(data[r]+"");
       }

       data = newData;
       tail = getSize();
       front = 0;

       printFT();
   }

   @Override
   public String toString() {
       StringBuilder res = new StringBuilder();
       res.append(String.format("Queue: size = %d ,capacity = %d\n", getSize(), getCapacity()));
       res.append("front [");
       for (int i = 0; i < getSize(); i++) {
           res.append(data[(front + i) % getCapacity()]);
           if ((front + i + 1) % data.length != tail) {
               res.append(", ");
           }
       }

       res.append("] rail");
       return res.toString();
   }
   private void print(String s){
       System.out.println(s);
   }private void printFT(){
       System.out.println("F:"+front+"  T:"+tail);
   }
}

0

算法与数据结构

波波老师5年集大成之作,算法与数据结构系统学习,考试、面试、竞赛通用

2603 学习 · 1086 问题

查看课程

相似问题

回答 1

回答 1

回答 2