Java数据结构--循环队列

一、简介

1.1 概念

队列简称队–他同堆栈一样,也是一种运算受限的线性表,其限制是仅允许在表的一端进行插入,而在表的另一端进行删除;
在队列中把插入数据元素的一端称为队尾,删除数据元素的一端称为队头;
向队尾插入元素称为进队和入队,新元素入队后成为新的队尾元素;从队列中删除元素称为离队或出队,元素出队后,其后续元素成为新的队头元素;
由于队列的插入和删除操作分别在队尾和队头进行,每个元素必然按照进入的次序离队,也就是说先进队的元素必然先离队,所以称队列为先进先出表

循环队列:将数组的最后一个元素的下一个元素从逻辑上认为是数组的第一个元素,这样就形成了逻辑上的环 。

1.2 结构图

在这里插入图片描述

二、简单实现

//循环顺序队列
public class CircleSequenceQueue {

    static final int defaultSize = 10; //默认队列的长度
    int front;  //队头
    int rear;   //队尾
    int count;  //统计元素个数的计数器
    int maxSize; //队的最大长度
    Object[] queue;  //队列
	
    public CircleSequenceQueue() {
        init(defaultSize);
    }

    public CircleSequenceQueue(int size) {
        init(size);
    }
	
	/**
	* 初始化队列的大小
	*/
    public void init(int size) {
        maxSize = size;
        front = rear = 0;
        count = 0;
        queue = new Object[size];
    }

  /**
	* 在队尾追加元素
	*/
    public void append(Object obj) throws Exception {
   
        if (count > 0 && front == rear) {
            throw new Exception("队列已满!");
        }
        queue[rear] = obj;
        rear = (rear + 1) % maxSize;
        count++;
    }

    /**
	* 删除最后一个元素
	*/
    public Object delete() throws Exception {
        if (isEmpty()) {
            throw new Exception("队列为空!");
        }
        Object obj = queue[front];
        front = (front + 1) % maxSize;
        count--;
        return obj;
    }

    /**
	* 获取队头
	*/ 
    public Object getFront() throws Exception {
        if (!isEmpty()) {
            return queue[front];
        } else {
            return null;
        }
    }

    /**
	*判断队列是否为空
	*/
    public boolean isEmpty() {
        return count == 0;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 数组模拟队列 ``` public class ArrayQueue { private int[] queue; private int front; private int rear; private int maxSize; public ArrayQueue(int maxSize) { this.maxSize = maxSize; queue = new int[maxSize]; front = -1; rear = -1; } public boolean isFull() { return rear == maxSize - 1; } public boolean isEmpty() { return front == rear; } public void add(int data) { if (isFull()) { System.out.println("队列已满"); return; } rear++; queue[rear] = data; } public int remove() { if (isEmpty()) { throw new RuntimeException("队列为空"); } front++; return queue[front]; } public void show() { if (isEmpty()) { System.out.println("队列为空"); return; } for (int i = front + 1; i <= rear; i++) { System.out.print(queue[i] + " "); } System.out.println(); } } ``` 2. 链表模拟队列 ``` public class LinkedListQueue { private Node front; private Node rear; public void add(int data) { Node node = new Node(data); if (rear == null) { front = node; rear = node; } else { rear.setNext(node); rear = node; } } public int remove() { if (isEmpty()) { throw new RuntimeException("队列为空"); } int data = front.getData(); front = front.getNext(); if (front == null) { rear = null; } return data; } public boolean isEmpty() { return front == null; } public void show() { if (isEmpty()) { System.out.println("队列为空"); return; } Node node = front; while (node != null) { System.out.print(node.getData() + " "); node = node.getNext(); } System.out.println(); } private static class Node { private int data; private Node next; public Node(int data) { this.data = data; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } } ``` 3. 循环队列 ``` public class CircularQueue { private int[] queue; private int front; private int rear; private int maxSize; public CircularQueue(int maxSize) { this.maxSize = maxSize; queue = new int[maxSize]; front = 0; rear = 0; } public boolean isFull() { return (rear + 1) % maxSize == front; } public boolean isEmpty() { return front == rear; } public void add(int data) { if (isFull()) { System.out.println("队列已满"); return; } queue[rear] = data; rear = (rear + 1) % maxSize; } public int remove() { if (isEmpty()) { throw new RuntimeException("队列为空"); } int data = queue[front]; front = (front + 1) % maxSize; return data; } public void show() { if (isEmpty()) { System.out.println("队列为空"); return; } for (int i = front; i != rear; i = (i + 1) % maxSize) { System.out.print(queue[i] + " "); } System.out.println(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

书香水墨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值