C#下Stack的实现及其解决实例(栈解决判断字符串是否回文)

这几天在看一些数据结构方面的书,自己总结了一下栈的实现方式,通过栈解决了回文的判断,如果有错的话大家在评论里指出来。

    public class WStack<T>
    {
        //储存数据的数组
        private T[] StoreArray;
        //当前栈中储存数据数量
        private int m_Count;
        //栈的容量
        private int m_Capacity;
        //指向栈顶的指针
        private int m_Index;
        
        public int Count{get{return m_Count;}private set { m_Count = value; }}

        public int Capacity { get { return m_Capacity; }private set { m_Capacity = value; } }

        /// <summary>
        /// 构造函数有参则初始化容量如果未传参则默认初始化容量为4
        /// </summary>
        public WStack(int Capacity=4)
        {
            m_Index = -1;
            m_Capacity = Capacity;
            m_Count = 0;
            StoreArray = new T[m_Capacity];
        }
        /// <summary>
        /// 将数据推入栈顶
        /// </summary>
        /// <param name="data"></param>
        public void Push(T data)
        {
            //当储存的数量和容量相等时仍然要将数据推入栈的话栈必须要进行扩容
            //满了的话栈扩容为两倍
            if(m_Count == m_Capacity)
            {
                m_Capacity *= 2;
                T[] tempArray = new T[m_Capacity];
                //拷贝当前数据
                Array.Copy(StoreArray, 0, tempArray, 0, StoreArray.Length);
                StoreArray = tempArray;
            }
            //指针指向栈尾
            StoreArray[++m_Index] = data;
            //储存数量自增1
            m_Count++;
        }
        /// <summary>
        /// 返回栈顶数据,不删除
        /// </summary>
        /// <returns></returns>
        public T Peek()
        {
            //栈内数据为0不允许执行Peek
            if(m_Count==0)
            {
                Console.WriteLine("该栈数量为零");
                return default(T);
            }
            else
            {
                //返回栈顶数据
                return StoreArray[m_Index];
            }
        }
        /// <summary>
        /// 返回栈顶数据,删除
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            //栈内数据为0不允许执行Pop
            if (m_Count == 0)
            {
                Console.WriteLine("该栈数量为零");
                return default(T);
            }
            else
            {
                //得到栈顶数据
                T data = StoreArray[m_Index];
                //删除栈顶数据
                StoreArray[m_Index] = default(T);
                //由于删除了数据所以数量减少
                m_Count--;
                //指针指向下一个数据
                m_Index--;
                return data;
            }
        }
        /// <summary>
        /// 清除所有数据并初始化容量等数据
        /// </summary>
        public void Clear()
        {
            StoreArray = null;
            m_Count = 0;
            m_Index = -1;
        }
    }

以下是回文解决方案:
该方法基于栈实现的原理将字符串拆分为单独的字符,一个一个将栈中的字符推出和原字符串相比较
只要有一个不符合就不是回文

class Program
 {
     public class Account
     {
         public string name;

         public int index;

         public Account(string name, int index)
         {
             this.name = name;
             this.index = index;
         }

         public override string ToString()
         {
             return string.Format("用户名字是{0},用户序号是{1}", name, index);
         }
     }

     static void Main(string[] args)
     {
         //WStack<Account> test = new WStack<Account>();
         //Account account0 = new Account("OneSitDown0", 1);
         //Account account1 = new Account("OneSitDown1", 2);
         //Account account2 = new Account("OneSitDown2", 3);
         //Account account3 = new Account("OneSitDown3", 4);
         //Account account4 = new Account("OneSitDown4", 5);
         //test.Push(account0);
         //test.Push(account1);
         //test.Push(account2);
         //test.Push(account3);
         //test.Push(account4);
         //Console.WriteLine(test.Capacity);
         //Console.WriteLine(test.Pop().ToString());
         //Console.WriteLine(test.Peek().ToString());
         //Console.WriteLine(test.Pop().ToString());
         //Console.WriteLine(test.Pop().ToString());
         string testStr = "123321";
         Console.WriteLine(isPalindrome(testStr));
         Console.ReadKey();
     }
     /// <summary>
     /// 判断是否回文
     /// </summary>
     /// <param name="tempStr"></param>
     /// <returns></returns>
     public static bool isPalindrome(string tempStr)
     {
         WStack<char> temp = new WStack<char>();
         for (int i = 0; i < tempStr.Length; i++)
         {
             temp.Push(tempStr[i]);
         }
         for (int i = 0; i < tempStr.Length; i++)
         {
             if (tempStr[i] != temp.Pop())
             {
                 return false;
             }
         }
         return true;
     }
 }
 ```
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值