zoukankan      html  css  js  c++  java
  • C#数据结构之串

      串(string)是n(n>=0)个字符组成的有限序列。

      由于串中的字符都是连续存储的,在C#中有恒定不变的特性。一经创建就保持不变。

      为了区别C#中的string,因此以stringDS类模拟string的数据结构,代码如下:

    class stringDS
        {
            private char[] _data;
            //字符数组
            
            //索引器
            public char this[int index] {
                get {
                    return _data[index];
                }
                set {
                    _data[index] = value;
                }
            }
            
            //构造函数
            public stringDS(char[] arr)
            {
                _data = new char[arr.Length];
                for (int i = 0; i < arr.Length; i++) {
                    _data[i] = arr[i];
                }
            }
            
            //构造函数
            public stringDS(int len)
            {
                char[] arr = new char[len];
                _data = arr;
            }
            
            //求串长
            public int GetLength()
            {
                return _data.Length;
            }
            
            //串比较
            public int Compare(stringDS s)
            {
                int len = (this.GetLength() <= s.GetLength()) ? this.GetLength() : s.GetLength();
    //            len=len-1;
                int i = 0;
                for(i=0;i<len;i++)
                {
                    if(this[i]!=s[i])
                    {
                        break;
                    }
                }
                if (i < len) {
                    if (this[i] < s[i]) {
                        return -1;
                    } else if (this[i] > s[i]) {
                        return 1;
                    }
                } else if (this.GetLength() == s.GetLength()) {
                    return 0;
                } else if (this.GetLength() < s.GetLength()) {
                    return -1;
                }
                return 1;
            }
            
            //求子串
            public stringDS SubString(int index, int len)
            {
                if (index < 0 || index > (this.GetLength() - 1)
                    || len < 0 || len > (this.GetLength() - index)) {
                    Console.WriteLine("position or len is error!");
                    return null;
                }
                stringDS s = new stringDS(len);
                for (int i = 0; i < len;++i) {
                    s[i] = this[i + index];
                }
                return s;
            }
            
            //串连接
            public stringDS Concat(stringDS s)
            {
                stringDS s1 = new stringDS(this.GetLength() + s.GetLength());
                for (int i = 0; i < this.GetLength(); i++) {
                    s1._data[i] = this[i];
                }
                for (int i = 0; i < s.GetLength(); i++) {
                    s1._data[this.GetLength() + i] = s[i];
                }
                return s1;
            }
            
            //串插入
            public stringDS Insert(int index, stringDS s)
            {
                int len = s.GetLength();
                int len2 = len + this.GetLength();
                stringDS s1 = new stringDS(len2);
                if (index < 0 || index > this.GetLength() - 1) {
                    Console.WriteLine("Position is error!");
                    return null;
                }
                for (int i = 0; i < index; i++) {
                    s1[i] = this[i];
                }
                for (int i = index; i < index + len; i++) {
                    s1[i] = s[i - index];
                }
                for (int i = index + len; i < len2; i++) {
                    s1[i] = this[i - len];
                }
                return s1;
            }
            
            //串删除
            public stringDS Delete(int index, int len)
            {
                if (index < 0 || index > (this.GetLength() - 1)
                    || len < 0 || len > (this.GetLength() - index)) {
                    Console.WriteLine("position or len is error!");
                    return null;
                }
                stringDS s = new stringDS(this.GetLength() - len);
                for (int i = 0; i < index; i++) {
                    s[i] = this[i];
                }
                for (int i = index + len; i < this.GetLength(); i++) {
                    s[i] = this[i];
                }
                return s;
            }
            
            //串定位
            public int Index(stringDS s)
            {
                if (this.GetLength() < s.GetLength()) {
                    Console.WriteLine("There is not string s!");
                    return -1;
                }
                int i = 0;
                int len = this.GetLength() - s.GetLength();
                while (i< len) {
                    stringDS temp=this.SubString(i,s.GetLength());
                    if (temp.Compare(s) == 0) {
                        break;
                    }
                    i++;
                }
                if (i <= len) {
                    return i;
                }
                return -1;
            }
                
        }
  • 相关阅读:
    16、集合--Map接口
    LVM快照备份与恢复
    CentOS 6.3下配置LVM(逻辑卷管理)
    Tomcat 9.0 配置问题 403 Access Denied
    nginx和php-fpm调用方式
    Linux下安装php环境并且配置Nginx支持php-fpm模块
    Nginx 服务器安装及配置文件详解
    Linux编辑器vim键盘详解
    Nginx配置文件详解
    keepalive配置文件详解
  • 原文地址:https://www.cnblogs.com/zhangyuanbo12358/p/4491765.html
Copyright © 2011-2022 走看看