数据结构----串

这个博客介绍了一个名为`StringDS`的自定义字符串类,该类实现了字符串的基本操作,如构造函数、索引访问、长度获取、比较、子字符串截取、字符串拼接和查找等功能。此外,还提供了输出字符串的方法。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace{
    class StringDS
    {
        private char[] data;
        public StringDS(char[] array)
        {
            data = new char[array.Length];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = array[i];
            }
        }
        public StringDS (string str)
        {
            data = new char[str.Length];
            for (int i = 0; i < data.Length ; i++)
            {
                data[i] = str[i];
            }
        }
        public StringDS ()
        {
            data = null;
        }
        public char this[int i]
        {
            get{return data[i];}
        }
        public int GetLength()
        {
            return data.Length;
        }
        //当前字符串等于s返回0
        //当前字符串小于s返回-1
        //当前字符串大于s返回1
        public int Compare(StringDS s)
        {
            int len = this.GetLength()<s.GetLength() ? this.GetLength(): s.GetLength();
            int index = -1;
            for (int i = 0; i < len; i++)
            {
                if (this[i]!=s[i])
                {
                    index = i;
                    break;
                }
            }
            if (index != -1)
            {
                return this[index] > s[index] ? 1 : -1;              
            }
            else
            {
                if (this.GetLength()==s.GetLength())
                {
                    return 0;
                }
                else
                {
                   return  this.GetLength() < s.GetLength() ? -1 : 1;                    
                }
            }           
        }
        public StringDS SubString(int index,int length)
        {
            char[] newdata;
            if (index < 0|| index > this.GetLength()-1||length<=0)
            {
                Console.WriteLine("The index does exsit!");
                return default(StringDS);
            }
            else
            {
                if (index+length<=this.GetLength())
                {
                    newdata = new char[length];
                    for (int i = index; i <index +length; i++)
                    {
                        newdata[i-index]= this.data[i];
                    }
                    return new  StringDS(newdata );
                }
                else  
                {
                    newdata = new char[this.GetLength()-index ];
                    for (int i = index; i < this.GetLength(); i++)
                    {
                        newdata[i - index] = this.data[i];
                    }
                    return new StringDS(newdata);                   
                }
            }
        }
        public StringDS  Concal(StringDS s1,StringDS s2)//拼接两个字符串
        {         
            char[] newData = new char[s1.GetLength() + s2.GetLength()];
            for (int i = 0; i < s1.GetLength(); i++)
            {
                newData[i] = s1[i];
            }
            for (int i = s1.GetLength(); i < newData.Length; i++)
            {
                newData[i] = s2[i-s1.GetLength()];
            }
            return new   StringDS(newData);
        }
        public int IndexOf(StringDS s)//查找字符串相等位置
        {
            bool isEqual = true;
            if (this.GetLength()<s.GetLength())
            {
                Console.WriteLine("当前串长度小于目标串");return -1;
            }
            else
            {
                for (int i = 0; i <= this.GetLength()-s.GetLength(); i++)//12345    123
                {
                    for (int j = 0; j < i+s.GetLength(); j++)
                    {
                        if (this[j]!=s[j-i])
                        {
                            isEqual = false;
                        }
                    }
                    if (isEqual)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
        public void OutPut()
        {
            for (int i = 0; i < data.Length ; i++)
            {
                Console.Write(data[i]);
            }
            Console.WriteLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卿屿­­­­­­­-轻尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值