鱼C论坛

 找回密码
 立即注册
查看: 982|回复: 3

泛型类

[复制链接]
发表于 2023-11-12 17:38:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
java编程完成泛型类CircularQueue(循环队列),CircularQueue的基本功能设计要求:创建循环队列、入队、出队、获取队头元素、判队空、获取队列元素个数。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 17:39:11 | 显示全部楼层
  1. public class CircularQueue<T> {
  2.     private int front;
  3.     private int rear;
  4.     private int size;
  5.     private int capacity;
  6.     private T[] elements;

  7.     public CircularQueue(int capacity) {
  8.         this.capacity = capacity;
  9.         this.elements = (T[]) new Object[capacity];
  10.         this.front = 0;
  11.         this.rear = 0;
  12.         this.size = 0;
  13.     }

  14.     public void enqueue(T element) {
  15.         if (size == capacity) {
  16.             throw new IllegalStateException("Queue is full");
  17.         }
  18.         elements[rear] = element;
  19.         rear = (rear + 1) % capacity;
  20.         size++;
  21.     }

  22.     public T dequeue() {
  23.         if (size == 0) {
  24.             throw new IllegalStateException("Queue is empty");
  25.         }
  26.         T element = elements[front];
  27.         elements[front] = null;
  28.         front = (front + 1) % capacity;
  29.         size--;
  30.         return element;
  31.     }

  32.     public T getFront() {
  33.         if (size == 0) {
  34.             throw new IllegalStateException("Queue is empty");
  35.         }
  36.         return elements[front];
  37.     }

  38.     public boolean isEmpty() {
  39.         return size == 0;
  40.     }

  41.     public int size() {
  42.         return size;
  43.     }
  44. }
复制代码


这段代码定义了一个泛型类CircularQueue,实现了循环队列的基本功能。循环队列通过数组实现,使用front和rear指针分别指向队头和队尾,size记录队列中元素的个数。在入队enqueue操作中,首先判断队列是否已满,然后将元素添加到rear指向的位置,并更新rear指针和size;在出队dequeue操作中,首先判断队列是否为空,然后取出front指向的元素,更新front指针和size。获取队头元素getFront、判队空isEmpty和获取队列元素个数size操作则比较简单,直接根据相应的条件进行判断或返回相应的值。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-12 17:40:09 | 显示全部楼层
当然,以下是一个简单的泛型类CircularQueue的实现,满足上述的基本功能设计要求:

[/code]java
import java.util.NoSuchElementException;

public class CircularQueue<T> {
    private Object[] queue;
    private int head;
    private int tail;
    private int size;
    private int capacity;

    public CircularQueue(int capacity) {
        this.capacity = capacity;
        queue = new Object[capacity];
        head = 0;
        tail = 0;
        size = 0;
    }

    public void enqueue(T item) {
        if (size == capacity) {
            throw new IllegalStateException("Queue is full");
        }
        queue[tail] = item;
        tail = (tail + 1) % capacity;
        size++;
    }

    public T dequeue() {
        if (size == 0) {
            throw new NoSuchElementException("Queue is empty");
        }
        T item = (T) queue[head];
        head = (head + 1) % capacity;
        size--;
        return item;
    }

    public T peek() {
        if (size == 0) {
            throw new NoSuchElementException("Queue is empty");
        }
        return (T) queue[head];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }
}
[/code]

这个泛型类CircularQueue实现了循环队列的基本功能,包括创建循环队列、入队、出队、获取队头元素和判队空、获取队列元素个数。你可以根据这个基础上进行进一步扩展和优化。希望这个例子能够帮助你完成泛型类CircularQueue的基本功能设计。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-12 20:10:31 | 显示全部楼层
我们可以根据你的需求创建一个泛型的循环队列类。下面是泛型类CircularQueue的一个基本实现:

  1. public class CircularQueue<T> {
  2.     private T[] queue;
  3.     private int head;
  4.     private int tail;
  5.     private int size;
  6.     @SuppressWarnings("unchecked")
  7.     public CircularQueue(int capacity) {
  8.         queue = (T[]) new Object[capacity];
  9.         head = 0;
  10.         tail = 0;
  11.         size = 0;
  12.     }
  13.    
  14.     public boolean enqueue(T element) {
  15.         if (size == queue.length) {
  16.             return false; // 队列已满
  17.         }
  18.         queue[tail] = element;
  19.         tail = (tail + 1) % queue.length;
  20.         size++;
  21.         return true;
  22.     }
  23.     public T dequeue() {
  24.         if (size == 0) {
  25.             return null; // 队列为空
  26.         }
  27.         T result = queue[head];
  28.         head = (head + 1) % queue.length;
  29.         size--;
  30.         return result;
  31.     }
  32.     public T getFront() {
  33.         if (size == 0) {
  34.             return null; // 队列为空
  35.         }
  36.         return queue[head];
  37.     }
  38.     public boolean isEmpty() {
  39.         return size == 0;
  40.     }
  41.     public int getSize() {
  42.         return size;
  43.     }
  44. }
复制代码


这个泛型类包含了以下方法:

- CircularQueue(int capacity): 创建指定容量的循环队列。
- enqueue(T element): 将元素添加到队列末尾,如果队列已满,返回false
- dequeue(): 从队列头部删除元素并返回,如果队列为空,返回null
- getFront(): 返回队列头部的元素,如果队列为空,返回null
- isEmpty(): 判断队列是否为空,如果为空,返回true
- getSize(): 返回队列中元素的个数。

请注意,这个循环队列的容量是固定的,如果你需要动态扩容的循环队列,需要进行适当的修改。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-3 13:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表