环形队列(链表实现)(java版本)

8 篇文章 0 订阅
3 篇文章 0 订阅
import java.util.Iterator;

public class CircleQueue<T> implements Iterable<T> {
    private Node first;//链表头节点
    private Node last;//链表尾节点
    private int N;//链表大小
    /**
     * 迭代器实现
     * @return 迭代器
     */
    @Override
    public Iterator<T> iterator() {
        return new circleQueueIterator();
    }
    private class circleQueueIterator implements Iterator<T> {
        int queueSize=N;
        private Node currentNode=first;
        @Override
        public boolean hasNext() {
            return queueSize!=0;
        }

        @Override
        public T next() {
            queueSize--;
            T data=currentNode.data;
            currentNode=currentNode.next;
            return data;
        }
    }

    /**
     * 节点内部类
     */
    private class Node{
        T data;
        Node next;
    }
    /**
     * 在表尾巴添加节点
     * @param data
     */
    public void enqueue(T data){
        Node nextNode=new Node();
        nextNode.data=data;
        if(first==null){
            first=nextNode;
            last=nextNode;
        }else{
            nextNode.next=first;
            last.next=nextNode;
            last=nextNode;
        }
        N++;
    }

    /**
     * 删除表头第一个节点
     * @return 移除的节点数据
     */
    public T dequeue(){
        T data=first.data;
        last.next=first.next;
        first=first.next;
        N--;
        return data;
    }

    /**
     * 链表大小
     * @return 链表长度
     */
    public int size(){
        return N;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值