c# Queue、Queue<T>、Stack、Stack<T>、Dictionary<TKey,TValue>、LinkedList<T>

            //顺序存储结构,即线性表,线性表可以动态扩大或者缩小,它在一片连续区域中存储数据,线性表不能按照索引进行查找
            //它是通过对地址的引用来搜索元素,为了找到某个元素,它必须遍历所有元素直到找到元素,所以线性表的优点是插入和删除数据效率高,缺点是查找效率相对低一些
            //线性表分为:队列、栈、索引群集

            //多线程集合类(线程安全)
            //1、ConcurrentBag<T>对应List<T>
            //2、ConcurrentQueue<T>对应Queue<T>
            //3、ConcurrentStack<T>对应Stack<T>
            //4、ConcurrentDictionary<TKey,TValue>对应Dictionary<TKey,TValue>

            //‎队列‎:先进先出(FIFO:First In, First Out),应用实例:消息队列,结果:HelloWorld!
            var q = new System.Collections.Queue();
            q.Enqueue("Hello");
            q.Enqueue("World");
            q.Enqueue("!");

            var qt = new System.Collections.Generic.Queue<string>();
            qt.Enqueue("Hello");
            qt.Enqueue("World");
            qt.Enqueue("!");

            //栈:后进先出(LIFO:Last In, First Out),结果:!WorldHello
            var s = new System.Collections.Stack();
            s.Push("Hello");
            s.Push("World");
            s.Push("!");

            var st = new System.Collections.Generic.Stack<string>();
            st.Push("Hello");
            st.Push("World");
            st.Push("!");

            //字典:值存储在基于Key的HashCode上,如果需要进行Key查找Value,字典会使查找更快捷
            var d = new Dictionary<string, string>();
            d.Add("txt", "notepad.exe");
            d.Add("bmp", "paint.exe");
            d.Add("dib", "paint.exe");
            d.Add("rtf", "wordpad.exe");

            //双向链表:双向链表的每一个节点都向前指向Previous节点,向后指向Next节点
            string[] w = { "the", "fox", "jumps", "over", "the", "dog" };
            var l = new LinkedList<string>(w);
            l.AddFirst("today");

参考文献:

Queue 类 (System.Collections) | Microsoft Docs

Queue<T> 类 (System.Collections.Generic) | Microsoft Docs

Stack 类 (System.Collections) | Microsoft Docs

Stack<T> 类 (System.Collections.Generic) | Microsoft Docs

Dictionary<TKey,TValue> 类 (System.Collections.Generic) | Microsoft Docs

LinkedList<T> 类 (System.Collections.Generic) | Microsoft Docs

ConcurrentBag<T> 类 (System.Collections.Concurrent) | Microsoft Docs

ConcurrentQueue<T> 类 (System.Collections.Concurrent) | Microsoft Docs

ConcurrentStack<T> 类 (System.Collections.Concurrent) | Microsoft Docs

ConcurrentDictionary<TKey,TValue> 类 (System.Collections.Concurrent) | Microsoft Docs

posted @ 2021-05-20 15:19  狼王爷  阅读(85)  评论(0编辑  收藏  举报