C# Stack

Stack is a Simple DataStucture which allows Insert/Remove of Items at one end. It is basically called as LIFO (Last In First Out) data structure (i.e. the item which is added last is the first one to be removed) .NET has a built in class for Stack. It is found in the System.Collections namespace.

1. How to Create a Stack?

 
 
Stack s = new Stack(32);
32 is the intial size of the Stack. It is always better to give intially size so that the Push/Pop operations are performed efficiently with out re-arranging the elements.

2. To Push a element to the Stack?

 
 
s.Push(12);
Push will place an element on the top of stack and also the element which is to be removed first.

3. How to Pop an Element?

 
 
object ob = s.Pop()

Pop will return the recently push item in the stack. It throws Exception when the Stack is Empty.

4. What if i need to just see the Top element without removing it from the Stack?

Stack class has a method called Peek() which return the top element of the Stack. It is very similar to Pop() except that it would not remove the element from the Stack.
Stack类代码:

using System;
//using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace StackHelper
{
    class StackHelper
    {
        private Stack list;
        public Stack List
        {
            set { list = value; }
            get { return list; }
        }
        public StackHelper()
        {
            list = new Stack();//注意:不是list=new StackHelper()
            Console.WriteLine("---------Stack-----------");
        }
        //压入一个元素
        public void Add(object o)
        {
            list.Push(o);
            Console.WriteLine("压入/t元素:{0}",o);
        }
        //删除栈顶元素
        public void Remove()
        {
            list.Pop();
            Console.WriteLine("弹出栈顶元素");
        }
        //取出栈顶元素
        public void GetValue()
        {
            list.Peek();
            Console.WriteLine("取出栈顶元素的值");
        }
        //使用foreach进行遍历
        public void GetValues()
        {
            Console.WriteLine("遍历");
            foreach (object o in list)
            {
                Console.WriteLine(string.Format("/t元素的值:{0}",o));
            }
        }
        //得到Stack的信息
        public void GetInfo()
        {
            Console.WriteLine(string.Format("信息/t元素总数:{0}",list.Count));
        }
    }
}
测试页面代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace StackHelper
{
    class Program
    {
        static void Main(string[] args)
        {
            StackHelper list=new StackHelper();
            list.Add("a");
            list.Add("b");
            list.Add("c");
            list.GetInfo();
            list.GetValues();
            list.GetValue();
            list.GetValues();
            list.GetInfo();
            list.Remove();
            list.GetValues();
            list.GetInfo();
        }
    }
}
注意:Pop ()和Peek()方法的区别。Pop()取出栈顶元素,栈顶元素被弹出Stack;Peek()读得栈顶元素,但站定元素没有被弹出Stack。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值