队列—普通队列(数组实现)、环形队列(数组实现)、链表实现

队列是很多高级数据结构和算法的基础,比如集合的List、Queue...

以下demo基本包含了队列的基本方法:

普通队列-数组实现 ArrayQueueCommon.java

/**
 * @Author: ltx
 * @Description: 普通队列-数组实现
 */
public class ArrayQueueCommon {
    private int[] arr;
    private int maxSize;//队列大小
    private int head;//头
    private int rear;//尾

    public ArrayQueueCommon(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        head = rear = -1;
    }

    /**
     * 队列是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判空
     *
     * @return
     */
    private Boolean isEmpty() {
        return rear == head;
    }

    /**
     * 队尾添加元素
     *
     * @param value
     */
    private void add(int value) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        arr[++rear] = value;
    }

    /**
     * 队列头移除元素
     *
     * @return
     */
    private int delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        return arr[++head];
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        int i = head;
        while (i < rear) {
            System.out.print(arr[++i] + ", ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayQueueCommon arrayQueue = new ArrayQueueCommon(5);
        arrayQueue.add(2);
        arrayQueue.add(1);
        arrayQueue.add(5);
        arrayQueue.add(3);
        arrayQueue.add(4);
        arrayQueue.add(7);
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(8);
        arrayQueue.add(9);
        arrayQueue.show();
    }
}

环形队列-数组实现 ArrayQueueLoop.java

/**
 * 环形队列-数组实现
 */
public class ArrayQueueLoop {
    private int[] arr;
    private int maxSize;//队列最大长度
    private int head;//头
    private int rear;//尾
    private int size;//队列长度

    /**
     * 初始化
     *
     * @param maxSize
     */
    public ArrayQueueLoop(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    /**
     * 队列是否满了
     *
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 判空
     *
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 队尾添加元素
     *
     * @param value
     */
    private void add(int value) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
        size++;
    }

    /**
     * 队列头移除元素
     *
     * @return
     */
    private int delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        int res = arr[head];
        head = (head + 1) % maxSize;
        size--;
        return res;
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        int i = 0, j = head;
        while (i < size) {
            i++;
            System.out.print(arr[j] + ", ");
            j = (j + 1) % maxSize;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayQueueLoop arrayQueue = new ArrayQueueLoop(5);
        arrayQueue.add(2);
        arrayQueue.add(1);
        arrayQueue.add(5);
        arrayQueue.add(3);
        arrayQueue.add(4);
        arrayQueue.add(7);
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(8);
        arrayQueue.add(9);
        arrayQueue.show();
    }

}

队列-链表实现 LinkedQuque.java

/**
 * 节点
 */
class SignLinkedNode implements Serializable {
    public Integer value;
    //下一个节点
    public SignLinkedNode next;

    public SignLinkedNode() {
    }

    public SignLinkedNode(Integer value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

/**
 * @Author: ltx
 * @Description: 队列-链表实现
 */
public class LinkedQuque {
    //头指针
    SignLinkedNode head;
    //尾指针
    SignLinkedNode rear;
    //队列实际长度
    int size;
    //最大长度
    int maxSize;

    /**
     * 构造方法-初始化
     *
     * @param maxSize
     */
    public LinkedQuque(int maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * 队列是否满了
     * @return
     */
    private Boolean isFull() {
        return size == maxSize;
    }

    /**
     * 队列判空
     * @return
     */
    private Boolean isEmpty() {
        return size == 0;
    }

    /**
     * 队尾添加元素
     * @param node
     */
    private void add(SignLinkedNode node) {
        if (isFull()) {
            System.out.println("队列满了...");
            return;
        }
        //第一个元素
        if (head == null) {
            head = node;
            rear = node;
        } else {
            rear.next = node;
            rear = node;
        }
        size++;
    }

    /**
     * 队列头删除元素
     * @return
     */
    private SignLinkedNode delete() {
        if (isEmpty()) {
            throw new RuntimeException("队列空...");
        }
        SignLinkedNode temp = head;
        head = head.next;
        size--;
        return temp;
    }

    /**
     * 遍历队列里面有效数据
     */
    private void show() {
        SignLinkedNode temp = head;
        while (temp != null) {
            System.out.print(temp + ", ");
            temp = temp.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedQuque arrayQueue = new LinkedQuque(5);
        arrayQueue.add(new SignLinkedNode(2));
        arrayQueue.add(new SignLinkedNode(1));
        arrayQueue.add(new SignLinkedNode(5));
        arrayQueue.add(new SignLinkedNode(3));
        arrayQueue.add(new SignLinkedNode(4));
        arrayQueue.add(new SignLinkedNode(7));
        arrayQueue.show();
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        System.out.println("移除元素: " + arrayQueue.delete());
        arrayQueue.add(new SignLinkedNode(8));
        arrayQueue.add(new SignLinkedNode(9));
        arrayQueue.show();
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++的环形队列是一种数据结构,它可以在队列的两端进行插入和删除操作,并且队列的大小可以动态改变。它的实现基于循环数组,可以有效地解决队列操作中的元素移动问题。 环形队列实现主要涉及以下几个方面: 1. 队列的定义 队列应该包括三个部分,即数据部分、头指针head和尾指针tail。head指向队头元素,tail指向队尾元素。 2. 队列的初始化 队列初始化时需要为队列分配一定的内存空间,并将头指针和尾指针都指向队列的第一个元素。 3. 入队操作 当有新元素要加入队列时,需要将新元素插入到tail指针的后面,并将tail指针向后移动一位。如果队列已满,需要特殊处理。 4. 出队操作 当需要删除队头元素时,需要将head指针指向下一个元素,并将原来的head节点删除。如果队列为空,需要特殊处理。 5. 队列大小的动态改变 当队列已满时,需要动态地扩展队列的大小。可以使用realloc()函数重新分配内存空间,并将原来的数据复制到新的内存空间中。 下面是基于链表实现环形队列的代码示例: ```C++ #include<iostream> using namespace std; //链表节点 struct Node { int data; Node* next; }; //链表实现环形队列 class Queue { public: Queue() { //队列初始化 head = tail = nullptr; size = 0; } ~Queue() { //队列销毁 Node* p = head; while (p) { Node* q = p; p = p->next; delete q; } } //入队操作 void Enqueue(int value) { Node* node = new Node; node->data = value; if (IsEmpty()) { head = tail = node; } else { tail->next = node; tail = node; } //队列大小加1 size++; } //出队操作 int Dequeue() { if (IsEmpty()) { return -1; } else { int value = head->data; Node* p = head; head = head->next; delete p; //队列大小减1 size--; return value; } } //判断队列是否为空 bool IsEmpty() { return head == nullptr; } //获取队列大小 int GetSize() { return size; } private: Node* head; //队头指针 Node* tail; //队尾指针 int size; //队列大小 }; int main() { Queue q; q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); cout << "队列大小为:" << q.GetSize() << endl; cout << "队头元素为:" << q.Dequeue() << endl; cout << "队头元素为:" << q.Dequeue() << endl; cout << "队列大小为:" << q.GetSize() << endl; q.Enqueue(4); cout << "队列大小为:" << q.GetSize() << endl; cout << "队头元素为:" << q.Dequeue() << endl; cout << "队头元素为:" << q.Dequeue() << endl; cout << "队列大小为:" << q.GetSize() << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小绿豆

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值