zoukankan      html  css  js  c++  java
  • C# 队列 堆栈

    通过实例进行C#的Stack类学习
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    namespace ConsoleApplication1
    ...{
    class Program
    ...{
    static void Main(string[] args)
    ...{
    Stack sk = new Stack();
    Stack sk2 = new Stack();
    foreach (int i in new int[4] ...{ 1, 2, 3, 4 })
    ...{
    sk.Push(i);//填充
    sk2.Push(i);
    }

    foreach (int i in sk)
    ...{
    Console.WriteLine(i);//遍历
    }

    sk.Pop();
    Console.WriteLine("Pop");
    foreach (int i in sk)
    ...{
    Console.WriteLine(i);
    }

    sk2.Peek();//弹出最后一项不删除//清清月儿 http://blog.csdn.net/21aspnet/


    Console.WriteLine("Peek");
    foreach (int i in sk2)
    ...{
    Console.WriteLine(i);
    }

    while (sk2.Count != 0)
    ...{
    int i = (int)sk2.Pop();//清空
    sk2.Pop();//清空
    }
    Console.WriteLine("清空");
    foreach (int i in sk2)
    ...{
    Console.WriteLine(i);
    }
    }
    }
    }

    using System;
    using System.Collections;
    public class SamplesQueue  {

        public static void Main()  {

           // Creates and initializes a new Queue.
           Queue myQ = new Queue();
           myQ.Enqueue("Hello");
           myQ.Enqueue("World");
           myQ.Enqueue("!");

           // Displays the properties and values of the Queue.
           Console.WriteLine( "myQ" );
           Console.WriteLine( "\tCount:    {0}", myQ.Count );
           Console.Write( "\tValues:" );
           PrintValues( myQ );
        }


        public static void PrintValues( IEnumerable myCollection )  {
           foreach ( Object obj in myCollection )
              Console.Write( "    {0}", obj );
           Console.WriteLine();
        }
    }
    /*
    This code produces the following output.

    myQ
         Count:    3
         Values:    Hello    World    !
    */


  • 相关阅读:
    [编程题] 基础 [位运算基础]
    [编程题] lc [191. 位1的个数]
    [编程题] lc [69. x 的平方根]
    php 中php-fpm工作原理
    redis分布式锁
    3种Redis分布式锁的对比
    php使用数据库的并发问题(乐观锁与悲观锁)
    php观察者模式应用场景实例详解
    [Usaco2008 Jan]电话网络
    关于二分图结论的一些证明
  • 原文地址:https://www.cnblogs.com/flish/p/1748248.html
Copyright © 2011-2022 走看看