[C#] C# 自定义堆栈进行回文检测 →→→→→进入此内容的聊天室

来自 , 2020-05-18, 写在 C#, 查看 157 次.
URL http://www.code666.cn/view/fbad540b
  1. using System;  
  2. using System.Collections;  
  3.  
  4. namespace CStack  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             CStack alist = new CStack();  
  11.  
  12.             string ch;  
  13.             string word = "上海自来水来自海上";  
  14.             bool isPalindrome = true;  
  15.  
  16.             for (int x = 0; x < word.Length; x++)  
  17.             {  
  18.                 alist.Push(word.Substring(x,1));  
  19.             }  
  20.  
  21.             int pos = 0;  
  22.  
  23.             while (alist.Count > 0)  
  24.             {  
  25.                 ch = alist.Pop().ToString();  
  26.                 if (ch !=word.Substring(pos,1))  
  27.                 {  
  28.                     isPalindrome = false;  
  29.                     break;  
  30.                 }  
  31.                 pos++;  
  32.             }  
  33.  
  34.  
  35.             Console.WriteLine(isPalindrome);  
  36.         }  
  37.     }  
  38.  
  39.     public class CStack  
  40.     {  
  41.         private int p_index;  
  42.         private ArrayList list;  
  43.  
  44.         public CStack()  
  45.         {  
  46.             list = new ArrayList();  
  47.             p_index = -1;  
  48.         }  
  49.  
  50.         public int Count  
  51.         {  
  52.             get { return list.Count; }  
  53.         }  
  54.  
  55.         public void Push(object item)  
  56.         {  
  57.             list.Add(item);  
  58.             p_index++;  
  59.         }  
  60.  
  61.         public object Pop()  
  62.         {  
  63.             if (0 > p_index)  
  64.             {  
  65.                 return null;  
  66.             }  
  67.             object obj = list[p_index];  
  68.             list.RemoveAt(p_index);  
  69.             p_index--;  
  70.             return obj;  
  71.         }  
  72.  
  73.         public void Clear()  
  74.         {  
  75.             list.Clear();  
  76.             p_index = -1;  
  77.         }  
  78.  
  79.         public object Peek()  
  80.         {  
  81.             if (p_index < 0)  
  82.             {  
  83.                 return null;  
  84.             }  
  85.  
  86.             return list[p_index];  
  87.         }  
  88.     }  
  89. }  
  90. //csharp/7214

回复 "C# 自定义堆栈进行回文检测"

这儿你可以回复上面这条便签

captcha