数据结构 -- 队列

队列

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

队列的入队和出队过程

图片(注:百度搜索而来的图片)链接: link.
Alt

java代码实现

public class Queue<E> {
    int size;
    private Node head;
    private Node tail;

    public Queue() {
        this.size = 0;
        this.head = null;
        this.tail = null;
    }
    
    public static void main(String[] args) throws Exception {
        int[] arr = new int[]{27, 24, 16, 42 ,41, 25, 20, 37, 20, 13, 14, 2, 39, 5, 16};
        Queue<Integer> queue = new Queue<>();
        for (int i = 0; i < arr.length; i++) {
            System.out.println("将要入队元素 : " + arr[i]);
            queue.enqueue(arr[i]);
            if (i % 5 == 4) {
                System.out.println("head : " + queue.front());
                System.out.println("dequeue : " + queue.dequeue());
            }
            System.out.println("size : " + queue.getSize());
            System.out.println(queue.toString());
            System.out.println("--------------------------------------------------------");
        }
    }


    /**
     * 入队
     *
     * @param e
     */
    public void enqueue(E e) {
        if (this.tail == null) {
            this.tail = new Node(e);
            this.head = this.tail;
        } else {
            Node newNode = new Node(e);
            this.tail.next = newNode;
            this.tail = this.tail.next;
        }
        this.size++;
    }

    /**
     * 出队
     *
     * @return
     */
    public E dequeue() throws Exception {
        if (this.isEmpty()) {
            throw new Exception("queue already empty!");
        }
        Node retNode = this.head;
        this.head = this.head.next;
        retNode.next = null;
        if (this.head == null) {
            this.tail = null;
        }
        this.size--;
        return retNode.e;
    }

    /**
     * 获得队首元素
     *
     * @return
     */
    public E front() {
        if (this.head != null) {
            return this.head.e;
        } else {
            return null;
        }
    }

    public int getSize() {
        return this.size;
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    @Override
    public String toString() {
        return _toString();
    }

    private String _toString() {
        Node copyNode = this.head;
        StringBuffer stringBuffer = new StringBuffer();
        while (copyNode != null) {
            stringBuffer.append(copyNode.e).append(" => ");
            copyNode = copyNode.next;
        }
        return stringBuffer.toString();
    }

    /**
     * 节点
     */
    private class Node {
        private E e;
        private Node next;

        public Node(E e) {
            this.e = e;
            this.next = null;
        }

        public String toString() {
            return this.e.toString();
        }
    }
}
将要入队元素 : 27
size : 1
27 => 
--------------------------------------------------------
将要入队元素 : 24
size : 2
27 => 24 => 
--------------------------------------------------------
将要入队元素 : 16
size : 3
27 => 24 => 16 => 
--------------------------------------------------------
将要入队元素 : 42
size : 4
27 => 24 => 16 => 42 => 
--------------------------------------------------------
将要入队元素 : 41
head : 27
dequeue : 27
size : 4
24 => 16 => 42 => 41 => 
--------------------------------------------------------
将要入队元素 : 25
size : 5
24 => 16 => 42 => 41 => 25 => 
--------------------------------------------------------
将要入队元素 : 20
size : 6
24 => 16 => 42 => 41 => 25 => 20 => 
--------------------------------------------------------
将要入队元素 : 37
size : 7
24 => 16 => 42 => 41 => 25 => 20 => 37 => 
--------------------------------------------------------
将要入队元素 : 20
size : 8
24 => 16 => 42 => 41 => 25 => 20 => 37 => 20 => 
--------------------------------------------------------
将要入队元素 : 13
head : 24
dequeue : 24
size : 8
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 
--------------------------------------------------------
将要入队元素 : 14
size : 9
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 
--------------------------------------------------------
将要入队元素 : 2
size : 10
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 
--------------------------------------------------------
将要入队元素 : 39
size : 11
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 => 
--------------------------------------------------------
将要入队元素 : 5
size : 12
16 => 42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 => 5 => 
--------------------------------------------------------
将要入队元素 : 16
head : 16
dequeue : 16
size : 12
42 => 41 => 25 => 20 => 37 => 20 => 13 => 14 => 2 => 39 => 5 => 16 => 
--------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值