public void Plalindrome() { SeqStack <char> s = new SeqStack <char>(50); CSeqQueue <char> q = new CSeqQueue <char>(50); Console.WriteLine("请输入回文:"); string str = Console.ReadLine(); // string str = "ABCDEDCBA"; for (int i = 0; i < str.Length; ++i) { s.Push(str[i]); q.In(str[i]); } while (!s.IsEmpty() && !q.IsEmpty()) { if (s.Pop() != q.Out()) { break; } } if (!s.IsEmpty() || !q.IsEmpty()) { Console.WriteLine("这不是回文!"); } else { Console.WriteLine("这是回文!"); } }
/* * 【例3-4】编程判断一个字符串是否是回文。 * 回文是指一个字符序列以中间字符为基准两边字符完全相同, * 如字符序列"ACBDEDBCA"是回文。 * 算法思想:判断一个字符序列是否是回文,就是把第一个字符与最后一个字符相比较, * 第二个字符与倒数第二个字符比较,依次类推,第i个字符与第n-i个字符比较。 * 如果每次比较都相等,则为回文,如果某次比较不相等,就不是回文。 * 因此,可以把字符序列分别入队列和栈, * 然后逐个出队列和出栈并比较出队列的字符和出栈的字符是否相等, * 若全部相等则该字符序列就是回文,否则就不是回文。 */ public static void palindrome() { SeqStack <char> s = new SeqStack <char>(50); SeqQueue <char> q = new SeqQueue <char>(50); string str = Console.ReadLine(); //手动输入字符串 for (int i = 0; i < str.Length; ++i) //循环入 { s.Push(str[i]); q.In(str[i]); } while (!s.IsEmpty() && !q.IsEmpty())//循环出 { if (s.Pop() != q.Out()) { break; } } if (!s.IsEmpty() || !q.IsEmpty())//非空 { Console.WriteLine("这不是回文!"); } else //空 { Console.WriteLine("这是回文!"); } } //public static void Main()
} //【例3-1】数制转换问题 //8进制转换//public void Conversion(int n) /* 【例3-2】括号匹配。 * 括号匹配问题也是计算机程序设计中常见的问题。 * 为简化问题,假设表达式中只允许有两种括号:圆括号和方括号。 * 嵌套的顺序是任意的,([]())或[()[()][]]等都为正确的格式, * 而[(])或(([)])等都是不正确的格式。检验括号匹配的方法要用到栈。 * * 算法思想:如果括号序列不为空,重复步骤1。 * 步骤1:从括号序列中取出1个括号,分为三种情况: * a) 如果栈为空,则将括号入栈; * b) 如果括号与栈顶的括号匹配,则将栈顶括号出栈。 * c) 如果括号与栈顶的括号不匹配,则将括号入栈。 * 步骤2:如果括号序列为空并且栈为空则括号匹配,否则不匹配。 * 算法如下,用顺序栈实现算法: */ public bool MatchBracket(char[] charlist) //匹配括号 { SeqStack <char> s = new SeqStack <char>(50); int len = charlist.Length; #region //开始循环,循环完毕 for (int i = 0; i < len; ++i) //开始循环 { if (s.IsEmpty()) //空栈 { s.Push(charlist[i]); //入栈 }//if (s.IsEmpty()) else if ((((s.GetTop() == '(') && (charlist[i] == ')'))) || (((s.GetTop() == '[') && (charlist[i] == ']')))) { //成功匹配 s.Pop();//出栈 }//else if else { //不匹配 s.Push(charlist[i]);//入栈 }//else }//for (int i = 0; i < len; ++i) #endregion if (s.IsEmpty()) { //空栈 return(true); }//(s.IsEmpty()) else { //非空栈 return(false); } //else } //匹配括号//public bool MatchBracket(char[] charlist)
static void Main(string[] args) { IStack<string> stack = new SeqStack<string>(500); stack.Push("a1"); stack.Push("a2"); stack.Push("a3"); while (stack.IsEmpty() == false) { Console.WriteLine(stack.StackTop); stack.Pop(); } }
static void Main(string[] args) { #region 顺序循环队列 //CSeqQueue<int> queue1=new CSeqQueue<int>(5); //queue1.In(1); //queue1.In(2); //queue1.In(3); //queue1.Out(); //queue1.Out(); //queue1.Out(); //queue1.In(0); //queue1.In(1); //queue1.In(2); //queue1.Out(); //queue1.Out(); //queue1.Out(); //queue1.In(3); //queue1.In(1); //queue1.In(2); //Console.WriteLine("头元素:"+queue1.GetFront()); //Console.WriteLine("队头:"+queue1.Front); //Console.WriteLine("队尾:"+queue1.Rear); //Console.WriteLine("队列长度:"+queue1.GetLength()); //Console.ReadKey(); #endregion #region 链队列 SeqStack <char> s = new SeqStack <char>(50); CSeqQueue <char> q = new CSeqQueue <char>(50); Console.WriteLine("请输入字符串用于判断您输入的字符串是否是回文字符!(按回车键结束)"); string str = Console.ReadLine(); for (int i = 0; i < str.Length; i++) { s.Push(str[i]); q.In(str[i]); } while (!s.IsEmpty() && !q.IsEmpty()) { if (s.Pop() != q.Out()) { break; } } if (!s.IsEmpty() || !q.IsEmpty()) { Console.WriteLine("这不是回文!"); } else { Console.WriteLine("这是回文!"); } Console.ReadKey(); #endregion }