IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

手写数据结构-基于链表的队列

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

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

文章永久链接:https://tech.souyunku.com/?p=38387


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(89) 打赏



未经允许不得转载:搜云库技术团队 » 手写数据结构-基于链表的队列

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367