为了账号安全,请及时绑定邮箱和手机立即绑定

C#实现栈并应用两个实例

标签:
C# 数据结构

概述
先用C#实现一个栈的类,如下:

public class MyStack<T> : IEnumerable<T>, IDisposable
    {
        private int _top = 0;
        private int _size = 0;
        private T[] _stack = null;

        public int Top {
            get {
                return _top;
            }
        }

        public int Size {
            get {
                return _size;
            }
        }

        public int Length {
            get {
                return _top;
            }
        }

        public T this[int index] {
            get {
                return _stack[index];
            }
        }

        public MyStack(int size) {
            _size = size;
            _top = 0;
            _stack = new T[size];
        }

        public bool IsEmpty() {
            return _top == 0;
        }

        public bool IsFull() {
            return _top == _size;
        }

        public void Clear() {
            _top = 0;
        }        

        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool Push(T node) {
            if (!IsFull()) {
                _stack[_top] = node;
                _top++;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 出栈
        /// </summary>
        /// <returns></returns>
        public T Pop() {
            T node = default(T);
            if (!IsEmpty()) {
                _top--;
                node = _stack[_top];
            }
            return node;
        }

        public void Traverse() {
            for(int i = 0; i < _top; i++) {
                Console.WriteLine(_stack[i]);
            }
        }

        public IEnumerator<T> GetEnumerator() {
            for(int i = 0; i < _stack.Length; i++) {
                if(_stack[i] != null) {
                    yield return _stack[i];
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }

        public void Dispose() {
            _stack = null;
        }
    }

上面要注意的是入栈时,是直接把值赋给栈顶,然后再将栈顶加1,出栈的时候,先将栈顶减一,再出栈,切记。
实例一:
用栈来实现进制转换,即十进制转化为2进制、8进制、16进制,代码如下:

        /// <summary>
        /// 十进制转换为其他进制
        /// </summary>
        /// <param name="number">要转换的数字</param>
        /// <param name="target">目标进制</param>
        /// <returns></returns>
        public static string Process(int number, int target) {
            string result = string.Empty;
            string format = "0123456789ABCDEF"; 
            MyStack<int> stack = new MyStack<int>(30);
            int mod = 0;
            while(number != 0) {
                mod = number % target;
                stack.Push(mod);
                number = number / target;
            }
            while (!stack.IsEmpty()) {
                int pos = stack.Pop();
                result += format[pos];
            }
            return result;
        }

实例二
用栈来检查一个字符串括号是否配对,代码如下:

/// <summary>
        /// 检查字符串中括号是否成对匹配
        /// </summary>
        /// <param name="charter">待检查的字符串</param>
        /// <returns></returns>
        public static bool Check(string charter) {
            bool result = false;
            MyStack<char> stack = new MyStack<char>(30);
            MyStack<char> needStack = new MyStack<char>(30);
            char currentNeed = '0';
            for (int i = 0; i < charter.Length; i++) {
                if(charter[i] != currentNeed) {
                    char t = charter[i];
                    stack.Push(t);
                    switch (t) {
                        case '[':
                            if(currentNeed != '0') {
                                needStack.Push(currentNeed);
                            }
                            currentNeed = ']';
                            break;
                        case '(':
                            if(currentNeed != '0') {
                                needStack.Push(currentNeed);
                            }
                            currentNeed = ')';
                            break;
                        case '{':
                            if (currentNeed != '0') {
                                needStack.Push(currentNeed);
                            }
                            currentNeed = '}';
                            break;
                    }
                }
                else {
                    stack.Pop();
                    currentNeed = needStack.Pop();
                }
            }
            if (stack.IsEmpty()) {
                result = true;
            }
            return result;
        }

总结:
希望能帮到你,望推荐!

点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消