【数据结构与算法 | 基础篇】单向循环链表实现队列

1. 前言

我们可以使用单向循环链表来实现队列.队列的特点是FIRST IN FIRST OUT.从队头删除节点,从队尾增加节点.

本文实现了从队头添加元素,从队尾删除元素.

2. 实现

自定义的Queue接口.

public interface Queue<E> {
    //向队伍插入值, 插入成功返回true, 否则返回false
    boolean offer(E value);
    //对队头获取值, 但不移除
    E poll();
    //从队头获取值, 并移除队头
    E peek();
    //判断队伍是否为空
    boolean isEmpty();

}

实现部分 : 

public class LInkedListQueue<E> implements Queue<E>, Iterable<E>{
    private static class Node<E> {
        E value;
        Node<E> next;
        public Node(E value, Node next) {
            this.value = value;
            this.next = next;
        }
    }
    //head指针指向队头
    Node<E> head;
    //tail指针指向队尾
    Node<E> tail;
    //队伍的实际长度
    int size;
    //队伍最大容量
    int capacity = Integer.MAX_VALUE;
    Node<E> dummy;
    public LInkedListQueue() {
        //初始化哨兵节点, 其next指针指向自身
        dummy = new Node(null, null);
        dummy.next = dummy;
        tail = dummy;
        head = dummy;
    }
    public LInkedListQueue(int capacity) {
        this();
        this.capacity = capacity;
    }
    @Override
    public boolean offer(E value) {
        Node p = new Node(value, head);
        tail.next = p;
        size++;
        //移动尾指针, 使尾指针指向尾节点
        tail = p;
        if(size > capacity) {
            return false;
        }
        return true;
    }

    @Override
    public E poll() {
        //如果队列为空, 则返回null
        if(isEmpty()) {
            return null;
        }
        E value = head.next.value;
        return value;
    }

    @Override
    public E peek() {
        //如果队列为空, 则返回null
        if(isEmpty()) {
            return null;
        }
        E value;
        //如果队列只有一个元素,
        if(head.next == tail) {
            value = head.next.value;
            head.next = null;
            tail = head;
            size--;
        } else {
            value = head.next.value;
            head.next = head.next.next;
        }
        return value;
    }

    @Override
    public boolean isEmpty() {
        //只有当head, tail指针都指向哨兵节点时, 此时队伍为空
        return head == tail;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = dummy.next;
            @Override
            public boolean hasNext() {
                return p != head;
            }

            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }
}

单元测试 : 

public class LInkedListQueueTest {
    @Test
    public void test() {
        LInkedListQueue<Integer> queue = new LInkedListQueue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);
        queue.offer(6);
//        for (Integer element : queue) {
//            System.out.println(element);
//        }
        System.out.println(queue.poll());
        //1
        System.out.println(queue.peek());
        //1
        System.out.println(queue.poll());
        //2
    }

}

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值