• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gooliugle
博客园    首页    新随笔    联系   管理    订阅  订阅
数据结构与算法读书笔记6----C# Stack类实现与应用

栈是著名的后进先出(LIFO)数据结构。

1、判断字符串是否是回文字符串。

2、十进制向多进制的转换。


1、代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace CStack
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Console.WriteLine(CStack.IsPalindrome(
"lixuejiao").ToString());
            Console.WriteLine(CStack.IsPalindrome(
"sees").ToString());
            Console.ReadLine();
        }
    }
    
class CStack
    {
        
private int p_index;
        
private ArrayList list;
        
private int count;

        
public int Count
        {
            
get { return list.Count; }
        }
        
public CStack()
        {
            list 
= new ArrayList();
            p_index 
= -1;
        }
        
public void Push(Object item)
        {
            list.Add(item);
            p_index
++;
        }
        
public object Pop()
        {
            
object obj = list[p_index];
            list.RemoveAt(p_index);
            p_index
--;
            
return obj;
        }
        
public void Clear()
        {
            list.Clear();
            p_index 
= -1;
        }
        
public object Peek()
        {
            
return list[p_index];
        }
        
//判断字符串是否是回文字符串
        
//2010.04.25
        public static bool IsPalindrome(string word)
        {
            CStack alist 
= new CStack();
            
string ch;
            
bool isPalindrome = true;
            
for (int num = 0; num < word.Length; num++)
            {
                alist.Push(word.Substring(num,
1));
            }
            
int pos = 0;
            
while (alist.Count > 0)
            {
                ch 
= alist.Pop().ToString();
                
if (ch != word.Substring(pos, 1))
                {
                    isPalindrome 
= false;
                    
break;
                }
                pos
++;
            }
            
if (isPalindrome)
            {
                Console.WriteLine(word 
+ " is a palindrome");
            }
            
else
            {
                Console.WriteLine(word 
+" is not a palindrome");
            }
            
return isPalindrome;
        }
    }
}
2、代码
 class Program
    {
        
static void Main(string[] args)
        {
            
int num, baseNum;
            Console.WriteLine(
"Enter a decimal number: ");
            num 
= Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(
"Enter a base: ");
            baseNum 
= Convert.ToInt32(Console.ReadLine());
            Console.Write(num
+" converts to ");        
            Console.WriteLine(
"Base "+baseNum+" is: ");
            MulBase(num, baseNum);
            Console.ReadLine();
        }
        
public static void MulBase(int n, int b)
        {
            Stack Digits 
= new Stack();
            
do
            {
                Digits.Push(n
%b);
                n
/=b;
            }
while(n!=0);
            
while(Digits.Count>0)
            {
                Console.Write(Digits.Pop());
            }
        }
    }



 


posted on 2010-04-25 20:15  gooliugle  阅读(301)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3