zoukankan      html  css  js  c++  java
  • 自定义堆栈(回文检测)

    using System;
    using System.Collections;
    
    namespace CStack
    {
        class Program
        {
            static void Main(string[] args)
            {
                CStack alist = new CStack();
    
                string ch;
                string word = "上海自来水来自海上";
                bool isPalindrome = true;
    
                for (int x = 0; x < word.Length; x++)
                {
                    alist.Push(word.Substring(x,1));
                }
    
                int pos = 0;
    
                while (alist.Count > 0)
                {
                    ch = alist.Pop().ToString();
                    if (ch !=word.Substring(pos,1))
                    {
                        isPalindrome = false;
                        break;
                    }
                    pos++;
                }
    
    
                Console.WriteLine(isPalindrome);
            }
        }
    
        public class CStack
        {
            private int p_index;
            private ArrayList list;
    
            public CStack()
            {
                list = new ArrayList();
                p_index = -1;
            }
    
            public int Count
            {
                get { return list.Count; }
            }
    
            public void Push(object item)
            {
                list.Add(item);
                p_index++;
            }
    
            public object Pop()
            {
                if (0 > p_index)
                {
                    return null;
                }
                object obj = list[p_index];
                list.RemoveAt(p_index);
                p_index--;
                return obj;
            } 
    
            public void Clear()
            {
                list.Clear();
                p_index = -1;
            }
    
            public object Peek()
            {
                if (p_index < 0)
                {
                    return null;
                }
    
                return list[p_index];
            }
        }
    }
    

  • 相关阅读:
    外部存储 使用详解
    内部存储 使用详解
    SQLite 使用详解
    SharePerference 使用详解
    Preference 使用详解
    Notification 使用详解
    PopupWindow 使用详解
    Fragment 使用详解
    Dialog 使用详解
    ListView 使用详解
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671522.html
Copyright © 2011-2022 走看看