现在的位置: 首页 > 综合 > 正文

栈及实现

2018年06月10日 ⁄ 综合 ⁄ 共 858字 ⁄ 字号 评论关闭

栈:数据项的列表,只能从表的末端进行存取访问,可存取访问的端称为栈顶。栈的标准模型是自助餐厅的盘子堆,始终从顶部拿走盘子。工作人员也把盘子放回盘子堆。栈是著名的后进先出(LIFO)数据结构

      栈最基本的两种操作就是向站内添加数据和删除数据。进栈(PUSH),出栈(POP)即取数也会删除数据。

     自定义栈的最基本操作,c#编写,参考自数据结构与算法 c# 描述书籍    

  class CStack
    {
        private int index;
        private ArrayList list;

        //初始化
        public CStack()
        {
            list = new ArrayList();
            index = -1;
        }

      //栈的数据数量
        public int count
        {
            get { return list.Count; }
        }

     //入栈
        public void push(object item)
        {
            list.Add(item);
            index++;
        }

//出栈

        public object pop()
        {
            object obj = list[index];
            list.RemoveAt(index);
            index--;
            return obj;
        }

//清空

        public void clear()
        {
            list.Clear();
            index = -1;
        }

//查看
        public object peek()
        {
            return list[index];
        }
    }

 

抱歉!评论已关闭.