为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C++


在研发之余,将做工程过程中比较重要的一些内容备份一下,如下的内容内容是关于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];  

    }  

}  

}

©著作权归作者所有:来自51CTO博客作者Cool_Manone的原创作品,如需转载,请注明出处,否则将追究法律责任


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消