《数据结构》严蔚敏版(java解)——第三章 栈和队列04 链式队列操作

来源:互联网 发布:淘宝怎么买vr岛国资源 编辑:程序博客网 时间:2024/06/06 05:00

代码实现

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;}}


0 0
原创粉丝点击