熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c# Stack操作類

2013-11-13 10:30:05  來源: .NET編程 

  Stack類代碼

   using System;

   //using SystemCollectionsGeneric;

   using SystemText;

   using SystemCollections;

  

   namespace StackHelper

   {

       class StackHelper

       {

           private Stack list;

           public Stack List

           {

               set { list = value; }

               get { return list; }

           }

           public StackHelper()

           {

               list = new Stack();//注意不是list=new StackHelper()

               ConsoleWriteLine(Stack);

           }

           //壓入一個元素

           public void Add(object o)

           {

               listPush(o);

               ConsoleWriteLine(壓入\t元素{}o);

           }

           //刪除棧頂元素

           public void Remove()

           {

               listPop();

               ConsoleWriteLine(彈出棧頂元素);

           }

           //取出棧頂元素

           public void GetValue()

           {

               listPeek();

               ConsoleWriteLine(取出棧頂元素的值);

           }

           //使用foreach進行遍歷

           public void GetValues()

           {

               ConsoleWriteLine(遍歷);

               foreach (object o in list)

               {

                   ConsoleWriteLine(stringFormat(\t元素的值{}o));

               }

           }

           //得到Stack的信息

           public void GetInfo()

           {

               ConsoleWriteLine(stringFormat(信息\t元素總數{}listCount));

           }

       }

   }

  測試頁面代碼

   using System;

   using SystemCollectionsGeneric;

   using SystemText;

   using SystemCollections;

  

   namespace StackHelper

   {

       class Program

       {

           static void Main(string[] args)

           {

               StackHelper list=new StackHelper();

               listAdd(a);

               listAdd(b);

               listAdd(c);

               listGetInfo();

               listGetValues();

               listGetValue();

               listGetValues();

               listGetInfo();

               listRemove();

               listGetValues();

               listGetInfo();

           }

       }

   }

  注意Pop ()和Peek()方法的區別Pop()取出棧頂元素棧頂元素被彈出StackPeek()讀得棧頂元素但站定元素沒有被彈出Stack


From:http://tw.wingwit.com/Article/program/net/201311/13833.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.