Csharp进阶:栈和队列

栈的特点是先进后出(就像子弹)

Stack stack = new Stack();

将数据压入到栈

stack.Push(1);

stack.Push(2);

stack.Push(3);

Pop用来弹出栈顶数据,就是删除栈顶元素

stack.Pop();

stack.Peek()用来获取栈顶元素

 

object numberObj = stack.Peek();

***********************************

通过栈特性转换进制的方法(10转其他)

class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine(Test02(30, 26));

 

       }

static string Test02(int number,int baseNumber)

        {

            Stack<char> statck =new Stack<char>();

            while (number/baseNumber !=0)

            {

                statck.Push( (char)(number % baseNumber +'a'));

                number /= baseNumber;

            }

            statck.Push((char)(number +'a'));

            return new string(statck.ToArray());

class StackExtern:Stack

    {

        public new char[] ToArray()

        {

 

            int count = this.Count;

            int[] chArr = new int[count];

            int index = 0;

            while (this.Count > 0)

            {

                object obj = Pop();

                Console.WriteLine((char)obj);

               

                chArr[index] = (int)obj;

                //index++;

            }

 

            return null;

        }

}

**************************************

队列

队列的特点是先进先出(就像排队)

Count(集合的容量)

Queue queue = new Queue();

EnterQueue  入队

queue.Enqueue(1);

queue.Enqueue(2);

queue.Enqueue(3);

Dequeue移除队列顶端元素并返回其他元素

queue.Dequeue();

Peek返回队列顶端元素

object obj = queue.Peek();


你可能感兴趣的:(Csharp)