关于Java算法与数据结构-队列的顺序存储实现

关于Java算法与数据结构-队列的顺序存储实现

package com.ssm.cts.test;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn 
 * Project: cts 
 * Module Name: QueueTest
 * Comments: 队列
 * JDK version used: JDK1.8 
 * Author: jzh 
 * Create Date: 2018-12-07
 * Modified By: jzh 
 * Modified Date: 2018-12-07
 * Why & What is modified:
 * Version: <1.0>
 */

// Queue接口
interface Queue {
  // 返回队列的大小
  public int getSize();

  // 判断队列是否为空
  public boolean isEmpty();

  // 数据元素e入队
  public void add(Object e);

  // 队首元素出队
  public Object poll() throws QueueEmptyException;

  // 取队首元素
  public Object peek() throws QueueEmptyException;
}

// QueueEmptyException队列为空时出队或取队首元素抛出此异常
@SuppressWarnings("serial")
class QueueEmptyException extends RuntimeException {
  public QueueEmptyException(String err) {
    super(err);
  }
}

// Queue的顺序存储实现
class QueueArray implements Queue {
  //private static final int LEN = 9;// 队列默认大小
  public Object[] elements; // 数据元素数组
  private int capacity; // 数组的大小elements.length
  private int front; // 队首指针,指向队首
  private int rear; // 队尾指针,指向队尾后一个位置
  
  public QueueArray(int LEN) {
    capacity = LEN + 1;
    elements = new Object[capacity];
    front = rear = 0;
  }

    // 返回队列的大小
  public int getSize() {
    return (rear - front + capacity) % capacity;
  }

    // 判断队列是否为空
  public boolean isEmpty() {
    return front == rear;
  }

    // 数据元素e入队
  public void add(Object e) {
    if (getSize() == capacity - 1)
      expandSpace();
    elements[rear] = e;
    rear = (rear + 1) % capacity;
  }

  private void expandSpace() {
    Object[] a = new Object[elements.length * 2];
    int i = front;
    int j = 0;
    
    // 将从front开始到rear前一个存储单元的元素复制到新数组
    while (i != rear) {
      a[j++] = elements[i];
      i = (i + 1) % capacity;
    }
    elements = a;
    capacity = elements.length;
    front = 0;
    rear = j; // 设置新的队首、队尾指针
  }

    // 队首元素出队
  public Object poll() throws QueueEmptyException {
    if (isEmpty()) {
      throw new QueueEmptyException("错误:队列为空");
    }
    
    Object obj = elements[front];
    elements[front] = null;
    front = (front + 1) % capacity;
    return obj;
  }

    // 取队首元素
  public Object peek() throws QueueEmptyException {
    if (isEmpty())
      throw new QueueEmptyException("错误:队列为空");
    return elements[front];
  }
}

public class QueueTest {
  // 测试
  public static void main(String[] args) {
    QueueArray queue = new QueueArray(9);
    queue.add("j");
    queue.add("z");
    queue.add("h");
    
    System.out.println("队列长度:" + queue.getSize());
    
    // 使用 for 循环数组
    for (Object obj  : queue.elements) {
      if (obj != null) {
        System.out.print(obj + " ");
      }
    }
    
    // 使用 foreach 循环数组
    for (int i = 0; i < queue.getSize(); i++) {
        System.out.print(queue.elements[i] + " ");
    }
    
    System.out.println();
    
    // 队首元素出队
    Object o = queue.poll();
    System.out.println(o);
    
    System.out.println("队列长度:" + queue.getSize());

  }

}

 

发表回复

您的电子邮箱地址不会被公开。