我是靠谱客的博主 活泼雨,这篇文章主要介绍《数据结构》严蔚敏版(java解)——第三章 栈和队列04 链式队列操作,现在分享给大家,希望可以做个参考。

代码实现

package csdn.wj.linear;

import csdn.wj.assistant.Node;
import csdn.wj.assistant.Queue;
import csdn.wj.assistant.Queue.QueueEmptyException;

public class Queue02 implements Queue {
	private Node front;
	private Node rear;
	private int size;
	public Queue02() {
		front = new Node(size);
		rear = front;
		size = 0;
	}
	//返回队列的大小
	public int getSize() {
		return size;
	}
	//判断队列是否为空
	public boolean isEmpty() {
		return size==0;
	}
	//数据元素 e入队
	public void enqueue(Object e) {
		Node p = new Node((int)e);
		rear.next = p;
		rear = p;
		size++;
	}
	//队首元素出队
	public Object dequeue() throws QueueEmptyException {
		if (size<1)
			throw new QueueEmptyException("错误:队列为空");
			Node p = front.next;
			front.next = p.next;
			size--;
			if (size<1) rear = front;   //如果队列为空,rear指向头结点
			return p.data;
	}
	//取队首元素
	public Object peek() throws QueueEmptyException {
		if (size<1)
			throw new QueueEmptyException("错误:队列为空");
			return front.next.data;
	}
}


最后

以上就是活泼雨最近收集整理的关于《数据结构》严蔚敏版(java解)——第三章 栈和队列04 链式队列操作的全部内容,更多相关《数据结构》严蔚敏版(java解)——第三章内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(169)

评论列表共有 0 条评论

立即
投稿
返回
顶部