C# 判断字符串是否为回文(用栈和队列)

 namespace _004_栈和队列实例_判断字符串是否为回文
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            Stack<char> stack = new Stack<char>();
            Queue<char> queue = new Queue<char>();

            for(int i = 0; i < str.Length; i++)
            {
                stack.Push(str[i]);
                queue.Enqueue(str[i]);
            }
            bool isHui = true;                        //isHui初始化为真
            while(stack.Count > 0)
            {
                if(stack.Pop() != queue.Dequeue())   //只要发现有一个不等,就把isHui设置为假
                {
                    isHui = false;
                    break;                           //发现有一个不等就退出循环
                }
            }

            Console.WriteLine("字符串是否是回文" + isHui);
            Console.ReadKey();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_39225721/article/details/80087072