数据结构
本文最后更新于260 天前,其中的信息可能已经过时,如有错误请发送邮件到3368129372@qq.com

  • 实现
    Deque<E> stack = new ArrayDeque<>();
    // 压栈操作
    stack.push(1);
    stack.push(2);
    stack.push(3);
    // 出栈操作
    int poppedElement = stack.pop();
    System.out.println("出栈元素:" + poppedElement);
    // 获取栈顶元素但不出栈
    int peekedElement = stack.peek();
    System.out.println("栈顶元素:" + peekedElement);
    // 检查栈是否为空
    boolean isEmpty = stack.isEmpty();
    System.out.println("栈是否为空:" + isEmpty); // 输出:栈是否为空:false
    // 获取栈的大小
    int size = stack.size();
    System.out.println("栈的大小:" + size);

队列

  • 实现
    Queue<E> q = new LinkedList<>();
    Deque<Integer> queue = new LinkedList<>();
    // 入队操作
    queue.offer(1);
    queue.offer(2);
    queue.offer(3);
    // 出队操作
    int dequeuedElement = queue.poll();
    System.out.println("出队元素:" + dequeuedElement);
    // 获取队头元素但不出队
    int peekedElement = queue.peek();
    (queue.peekFirst()与queue.peekLast())
    System.out.println("队头元素:" + peekedElement);
    // 检查队列是否为空
    boolean isEmpty = queue.isEmpty();
    System.out.println("队列是否为空:" + isEmpty);
    // 获取队列的大小
    int size = queue.size();
    System.out.println("队列的大小:" + size);

  • 优先队列
    //里面能自己写Comparator,取出来的是堆顶,最大堆取得是最大值
    PriorityQueue<Integer> minHeap = new PriorityQueue<>();
    // 插入元素 - offer()
    minHeap.offer(5); // O(logN)
    minHeap.offer(2); // O(logN)
    minHeap.offer(8); // O(logN)
    minHeap.offer(1); // O(logN)
    minHeap.offer(4); // O(logN)
    // 检索元素 - peek() 和 poll()
    int min = minHeap.peek(); // O(1)
    int removedMin = minHeap.poll(); // O(logN)
    // 获取队列大小 - size()
    int size = minHeap.size(); // O(1)
    // 判空 - isEmpty()
    boolean isEmpty = minHeap.isEmpty(); // O(1)
    // 转化为数组 - toArray()
    Integer[] array = minHeap.toArray(new Integer[0]); // O(N)
    // 清空队列 - clear()
    minHeap.clear(); // O(N)
    // 遍历队列(不改变队列状态) - 使用迭代器或循环遍历
    for (Integer num : minHeap) {
    // 遍历操作
    }
    // 复杂度注释:
    // - 插入操作:O(logN)
    // - 检索操作 (peek()):O(1)
    // - 检索并删除操作 (poll()):O(logN)
    // - 获取队列大小 (size()):O(1)
    // - 判空 (isEmpty()):O(1)
    // - 转化为数组 (toArray()):O(N)
    // - 清空队列 (clear()):O(N)
    // - 遍历队列 (不改变队列状态):O(N)
    }
    }

map

  • HashMap
    // 创建一个HashMap对象
    Map<String, Integer> hashMap = new HashMap<>();
    // 添加键值对
    hashMap.put("apple", 1);
    hashMap.put("banana", 2);
    hashMap.put("cherry", 3);
    // 获取键对应的值
    Integer appleValue = hashMap.get("apple");
    // 获取,获取不到则用默认值
    Integer pearValue = hashMap.getOrDefault("pear",4);
    // 检查键是否存在
    boolean containsKey = hashMap.containsKey("banana");
    // 删除键值对
    hashMap.remove("cherry");
    // 获取HashMap的大小
    int size = hashMap.size();
    // 清空HashMap
    hashMap.clear();
    // 检查HashMap是否为空
    boolean isEmpty = hashMap.isEmpty();
感谢您的收看~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇