zoukankan      html  css  js  c++  java
  • 第19节、C#Queue类:队列

    C# Queue(队列)

    是最常见的数据结构之一,队列是一种先进先出的结构,即元素从队列尾部插入,从队列的头部移除(从一端入,另外一端出),类似于日常生活中战队。

    Queue类提供了4个构造方法,如下

    作用
    Queue() 创建 Queue 的实例,集合的容量是默认初始容量 32 个元素,使用默认的增长因子
    Queue(ICollection col) 创建 Queue 的实例,该实例包含从指定实例中复制的元素,并且初始容量与复制的元素个数、增长因子相同
    Queue(int capacity)  创建 Queue 的实例,并设置其指定的元素个数,默认增长因子
    Queue(int capacity, float growFactor) 创建 Queue 的实例,并设置其指定的元素个数和增长因子

    增长因子是指当需要扩大容量是,以当前的容量值乘以增长因子的值来自动增加容量

    示例:

     完整代码:

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace ConsoleApplication1
     9 {
    10    public class Queue队列
    11     {
    12        public static void Main(string[] age)
    13        {
    14            //声明队列,在队列加3个打饭人
    15            Queue que = new Queue();
    16            que.Enqueue("小美");
    17            que.Enqueue("小爱");
    18            que.Enqueue("小甜");
    19            que.Enqueue("小蜜");
    20 
    21            Console.WriteLine("开始打饭");
    22 
    23            //while循环
    24            while (que.Count != 0)
    25            {
    26                Console.WriteLine(que.Dequeue() + "打饭了");//Dequeue异常
    27            }
    28 
    29            //使用ToArray方法将Queue类的实例存放的值赋值到数组
    30            object[] obj = que.ToArray();
    31            foreach (var v in obj)
    32            {
    33                Console.WriteLine(v + "打饭了");
    34            }
    35 
    36            IEnumerator e = que.GetEnumerator();
    37            while (e.MoveNext())
    38            {
    39                Console.WriteLine(e.Current + "打饭了");
    40            }
    41            Console.ReadLine();
    42        }
    43    }
    44 }
    View Code
  • 相关阅读:
    基本配置+路由系统+模板
    ORM之SQLAlchemy
    web应用+250斗笔式模拟框架(socket、pymysql、jinja2)
    Leetcode56. Merge Intervals合并区间
    Leetcode50. Pow(x, n)(快速幂)
    Leetcode43. Multiply Strings字符串相乘(大数相乘)
    (转)Json在Unity中的简单使用
    Leetcode49. Group Anagrams字母异位词分组
    Leetcode48. Rotate Image旋转图像
    Leetcode47. Permutations II全排列2
  • 原文地址:https://www.cnblogs.com/liuzz/p/14535089.html
Copyright © 2011-2022 走看看