码迷,mamicode.com
首页 > Windows程序 > 详细

C# 自定义堆栈进行回文检测的代码

时间:2018-12-04 19:08:11      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:get   ++   vat   回文   过程   break   substr   collect   代码   

在研发之余,将做工程过程中比较重要的一些内容备份一下,如下的内容内容是关于C# 自定义堆栈进行回文检测的内容,希望对各朋友也有用。

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];  
    }  
}  

}

C# 自定义堆栈进行回文检测的代码

标签:get   ++   vat   回文   过程   break   substr   collect   代码   

原文地址:http://blog.51cto.com/14101311/2325772

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!