lwj_C#_集合stack栈和queue队列

来源:互联网 发布:java串口编程源代码 编辑:程序博客网 时间:2024/04/30 23:04

using System;

//使用集合时要先引入命名空间

//使用非泛型时引用的命名空间

using System.Collections;

//使用泛型时引用的命名空间

using System.Collections.Generic;
namespace Application
{
    
    class MainClass
    {
        public static void Main(string[] args)
        {
            //
            int[] arr = {233413 };
            Stack stack = new Stack(arr);
            //入栈
            stack.Push("string");
            //出栈   后进先出;
            string str = stack.Pop() as string;
            Console.WriteLine(str);
            //元素个数;
            int a = stack.Count;
            Console.WriteLine(a);
            //获取栈顶元素;但是不移除
            object b = stack.Peek();
            Console.WriteLine(b);
            //是否包含;
            bool c = stack.Contains("string");
            Console.WriteLine(c);
            //队列
            Queue queue = new Queue(arr);
            //入队
            queue.Enqueue("string");
            //加个空的值
            queue.Enqueue(null);
            //出队
            //object obj = queue.Dequeue();
            //Console.WriteLine(obj);
            //queue.Clear();
            Console.WriteLine(queue.Count);
            object[] array = queue.ToArray();
            //遍历
            foreach (object i in queue)
            {
                Console.Write(i + " ");
            }
            //泛型栈和队列
            Stack<intstack_1 = new Stack<int>();
            Queue<stringqueue_2 = new Queue<string>();
           stack_1.Push(12);
            queue_2.Enqueue("sb");




        }
    }
}
阅读全文
0 0