首页 > 其他分享 >【code基础】队列

【code基础】队列

时间:2022-10-04 13:11:17浏览次数:40  
标签:code 队列 基础 System element Queue println out

队列的定义

Queue的实现类有LinkedList和PriorityQueue 最常用的实现类是LinkedList

  • 队列通常(先进先出)的方式排序各个元素,在 FIFO 队列中,所有的新元素都插入队列的末尾。
  • 其他种类的队列可能使用不同的元素放置规则。每个 Queue 实现必须指定其顺序属性。如优先级队列和 LIFO 队列(或堆栈):
    • 优先级队列根据提供的比较器或元素的自然顺序对元素进行排序,
    • LIFO 队列(或堆栈)按 LIFO(后进先出)的方式对元素进行排序
public class QueueMain {
    public static void main(String[] args) {
        MyQueue1 q = new MyQueue1();
        q.enQueue(5);
        q.enQueue(3);
        if (!q.isEmpty()) System.out.println(q.Front()); //5
        q.deQueue();
        if (!q.isEmpty()) System.out.println(q.Front()); //3
        q.deQueue();
        q.enQueue(4);
        if (!q.isEmpty()) System.out.println(q.Front()); //4

    }
}

队列的操作

  • 队列的数据结构为Queue,内置实现是LinkList
  • 队列的获取队首元素为q.peek()
  • 队列入队操作位q.offer(x)
  • 队列的出队为q.poll()
  • 队列的长度为q.size()
public class InnerQueueMain {
    public static void main(String[] args) {
        // 1. Initialize a queue.
        Queue<Integer> q = new LinkedList<>();
        Queue<Integer> q2 = new ArrayDeque<>();

        // 2. Get the first element - return null if queue is empty.
        System.out.println("The first element is: " + q.peek()); //null

        // 3. Push new element.
        q.offer(5);
        q.offer(13);
        q.offer(8);
        q.offer(6);

        // 4. Pop an element.
        q.poll(); //5

        // 5. Get the first element.
        System.out.println("The first element is: " + q.peek()); //13
        // 7. Get the size of the queue.
        System.out.println("The size is: " + q.size());  //3
    }
}

标签:code,队列,基础,System,element,Queue,println,out
From: https://www.cnblogs.com/xiaoyu-jane/p/16753612.html

相关文章

  • Codeforces Global Round 22
    题目链接这场彻底打崩了,只做了A,B,C,可以看出我离退役已经不远力!A.GloryAddictstrash题不写。感觉出得很没意思。B.PrefixSumAddicts用\(s_{n-k+1}\sims_n\)......
  • Windows基础
    一、DOS命令color#改变cmd颜色ping-t-l65550[ip]#死亡之ping(发送大于64k的文件并一直ping就成了ping死亡)ipconfig#查看ipipconfig/release#释放......
  • leetcode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公
    一、题目大意给定一个二叉搜索树,找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树T的两个结点p、q,最近公共祖先表示为一个结点......
  • python在VScode中中文输出乱码的解决方案
    ————————————————版权声明:本文为CSDN博主「Rhett.Yao」的原创文章。原文链接:https://blog.csdn.net/m0_63109538/article/details/126419637——————......
  • (五)MySQL基础继续--连接(join)
    昨天在说MySQL的分组(groupby)时,最后有提到为什么会多出来一行-->null,原来null表示所有人加起来的数。mysql>selectname,sum(scores)asscores_countfromteacherg......
  • 键盘按键键码值(keyCode)对照表
    使用linux下xev可以获取键盘上所有按键的键值,记录如下:字母和数字键的键码值(keyCode)按键 键码  按键 键码 按键  键码   按键 键码A 65 J ......
  • CodeForces 1455G Forbidden Value
    洛谷传送门CF传送门小清新动态开点线段树优化dp题。首先题目中的if嵌套看起来就很烦,可以考虑建树,外面再套一层大的if0...end,这样就将本题转化成一个树上问题。......
  • Codeforces Round #823 (Div. 2) A-D
    比赛链接A题解知识点:贪心。对于一个轨道,要么一次性清理,要么一个一个清理。显然,如果行星个数大于直接清理的花费,那么选择直接清理,否则一个一个清理。即\(\sum\min(c,......
  • Java手写实现链表队列和数组队列【数据结构与算法】
    packagealgorithm;/**@authorAdministrator@date2022-09-1317:50*/publicclassQueueLinked{privatestaticclassNode{Eitem;Nodenext;publicNode(Eitem,N......
  • LeetCode 20. 有效的括号(超详细超容易理解的动画解法!!!)
    大家好,我是程序员吴师兄,今天跟大家分享一道和栈一、题目描述给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字符串需满足:左括号必须用相同类......