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

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

            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();
        }
    }
C# 判断字符串是否为回文(用栈和队列)_第1张图片 C# 判断字符串是否为回文(用栈和队列)_第2张图片

你可能感兴趣的:(数据结构)