zoukankan      html  css  js  c++  java
  • 手写数据结构-基于链表的队列

    1.基于链表的队列基础

    结构特性:先进先出

    实现原理:采用双指针,多维护一个指针指向队尾,向尾部进行添加操作时间复杂度会变为O(1)。

    2.手写基于链表的队列及复杂度分析(和基于动态数组的循环队列对比)

    package com.tc.javabase.datastructure.linklist.queue;
    
    import com.tc.javabase.datastructure.queue.Queue;
    
    /**
     * @Classname LinkedListQueue
     * @Description 基于链表实现的队列
     *
     * 采用头指针和尾指针的方式实现基于链表实现的队列
     *
     * 时间复杂度分析:
     *  *  入队:O(1)
     *  *  出队:O(1)
     *  *  查询队首元素:O(1)
     *
     *
     * @Date 2020/7/18 18:42
     * @Created by zhangtianci
     */
    public class LinkedListQueue<E> implements Queue<E> {
        private class Node{
            public E e;
            public Node next;
            public Node(E e, Node next) {
                this.e = e;
                this.next = next;
            }
    
            public Node(E e){
                this(e,null);
            }
    
            public Node(){
                this(null,null);
            }
        }
    
        private Node head,tail;
        private int size;
    
        @Override
        public int getSize() {
            return size;
        }
    
        @Override
        public boolean isEmpty() {
            return size == 0 ? true : false;
        }
    
        /**
         * 入队
         *
         * 时间复杂度:O(1)
         * @param e
         */
        @Override
        public void enqueue(E e) {
            if (tail == null){
                tail = new Node(e);
                head = tail;
            }else {
                tail.next = new Node(e);
                tail = tail.next;
            }
    
            size++;
        }
    
        /**
         *  出队
         *  时间复杂度:O(1)
         * @return
         */
        @Override
        public E dequeue() {
            if (isEmpty()){
                throw new IllegalArgumentException("队列为空!");
            }
            Node retn = head;
            head = head.next;
            retn.next = null;
            if (head == null){
                tail = null;
            }
            size--;
            return retn.e;
        }
    
        /**
         *   查看队首元素
         *   时间复杂度:O(1)
         * @return
         */
        @Override
        public E getFront() {
            if (isEmpty()){
                throw new IllegalArgumentException("队列为空!");
            }
            return head.e;
        }
    
        @Override
        public String toString() {
    
            StringBuilder builder = new StringBuilder();
            builder.append("Queue head");
            Node curr = head;
            while (curr != null){
                builder.append(curr.e);
                builder.append("->");
                curr = curr.next;
            }
            builder.append("NULL tail");
            return builder.toString();
        }
    
        public static void main(String[] args) {
    
            LinkedListQueue<Integer> queue = new LinkedListQueue<>();
            for(int i = 0 ; i < 10 ; i ++){
                queue.enqueue(i);
                System.out.println(queue);
    
                if(i % 3 == 2){
                    queue.dequeue();
                    System.out.println(queue);
                }
            }
        }
    }
    
  • 相关阅读:
    redis要注意的一些知识
    redis数据类型及常用命令使用
    基于zookeeper的分布式锁
    基于redis的分布式锁
    基于数据库的分布式锁
    数据库的4中隔离级别
    远程调用历史及代码编写demo
    数据库的ACID跟事务隔离级别
    JAVA8 十大新特性详解
    java8 :: 用法 (JDK8 双冒号用法)
  • 原文地址:https://www.cnblogs.com/tc971121/p/13443697.html
Copyright © 2011-2022 走看看