Stack-C#数组构造堆栈

//@file stack.cs
//功能:数组构造堆栈stack
//VS2005 complied ok with chinayaosir
using System;
using System.Collections;//导入容器空间
//using System.Text;

namespace DS{

class Stack{
        private int index_top;//指向堆栈顶部
        private ArrayList list;
        //0.1.构造函数
        public Stack(){
            list = new ArrayList();
            index_top = -1;
        }
        //0.2元素个数
        public int count{
            get { return list.Count; }
        }
        //0.3清空堆栈
        public void clear(){
            list.Clear();
            index_top = -1;
        }       
        //1.push()
        public void push(object item){
            list.Add(item);
            index_top++;
        }
        //2.pop()
        public object pop(){
            object obj = list[index_top];
            list.RemoveAt(index_top);
            index_top--;
            return obj;
        }

        //3.取栈顶元素
        public object peek(){
            return list[index_top];
        }
        //4.测试堆栈Main函数
        static void Main(string[] args)
        {
            Stack slist = new Stack();
            string w,word = "Hello-Stack";

            for (int x = 0; x < word.Length; x++)
                slist.push(word.Substring(x, 1));
            //后进先出正向压入"Hello-Stack"
            Console.WriteLine(word);
            //后进先出反向弹出"kcatS-olleH"
            while (slist.count > 0)
            {
                w= slist.pop().ToString();
                Console.Write(w);
            }
            slist.clear();
            Console.ReadLine();
        }
}//class end

}//namespace end

/*
 运行结果是:
 Hello-Stack
 kcatS-olleH
  */

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值