加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】队列-循环队列

发布时间:2020-12-15 06:04:42 所属栏目:安全 来源:网络整理
导读:在队列的数组实现中,我们很容易发现数在出队后,数组的前面部分会有剩余空间没有被使用,所以我们为了最大程度的利用固定长度的数组,我们采用循环队列的存储方式,这种方式的最大问题在于resize的时候比较麻烦,所以我们不考虑resize的情况。 基本结构如下

在队列的数组实现中,我们很容易发现数在出队后,数组的前面部分会有剩余空间没有被使用,所以我们为了最大程度的利用固定长度的数组,我们采用循环队列的存储方式,这种方式的最大问题在于resize的时候比较麻烦,所以我们不考虑resize的情况。

基本结构如下,这里front指向第一个元素的位置,rear指向最后一个元素的下一位,所以循环队列要浪费一个空间,这样才能区别队满和队空得情况。

	// member variable
	private String[] queue;
	private int front = 0;// point to the first element
	private int rear = 0; // point to the next after the last element
	private int maxSize;

	// constructor with parameter
	public QueueOfStringsCircle(int capacity) {
		queue = new String[capacity];
		maxSize = queue.length;
	}

入队
	public void enqueue(String str) {
		if (!isFull()) {
			queue[rear] = str;
			rear = (rear + 1) % maxSize;
		} else
			System.out.println("queue is full");
	}

注意理解这里模运算的技巧,maxsize一定等于整个数组的长度的,而不是元素的个数。

出队

	public String dequeue() {
		if (!isEmpty()) {
			String result = queue[front];
			queue[front] = null;
			front = (front + 1) % maxSize;
			return result;
		} else {
			System.out.println("queue is empty");
			return null;
		}
	}

队满
	public boolean isFull() {
		return (rear + 1) % maxSize == front;
	}


队空

	public boolean isEmpty() {
		return rear == front;
	}


求size
	public int size() {
		if (rear >= front)
			return rear - front;
		else
			return maxSize - (front - rear);
	}

如果尾大于头,说明没有循环产生,直接减就ok

如果尾小于头,说明循环已经产生,这个时候应该取尾减头的相反数再加上最大长度。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读