public class ArrayQueue { private static final String TAG = "ArrayQueue"; private Object[] queue; private int SIZE = 10;//初始化大小 private int front = 0; private int rear = 0; private int usesize = 0; public ArrayQueue() { queue = new Object[SIZE]; } public boolean isEmpty() { return (rear - front) == 0; } public int getSize() { return rear - front; } public void insert(Object obj) throws Exception { if (front > rear || rear > SIZE) throw new Exception("无法进入队列!"); if (((rear - front) + 1) % SIZE == 0) throw new Exception("队列已满!"); queue[rear] = obj; rear++; usesize++; } public void delete() throws Exception { if (front >= rear || rear > SIZE) throw new Exception("无法删除!"); queue[front] = null; front++; usesize--; } public void display() throws Exception { if (usesize == 0) throw new Exception("空队列!"); for (int i = front; i < rear; i++) { System.out.print(queue[i] + "<-"); } System.out.println(""); } public static void main(String[] args) throws Exception { ArrayQueue aq = new ArrayQueue(); aq.insert("123"); aq.insert("qwe"); aq.insert("tt"); aq.insert("h8h0-----=1ew"); System.out.println(aq.queue[0]); System.out.println(aq.queue[1]); System.out.println(aq.queue[2]); System.out.println(aq.queue[3]); System.out.println(aq.getSize()); aq.display(); } }

相关文章:

  • 2021-09-16
  • 2022-12-23
  • 2021-09-09
  • 2021-11-19
  • 2021-11-07
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-23
  • 2021-11-10
  • 2022-02-11
  • 2021-05-20
  • 2021-11-28
  • 2021-08-07
相关资源
相似解决方案