005-数据结构之串的学习
在应用程序中使用最频繁的类型是字符串。字符串简称串,是一种特殊的线性表,其特殊性在于串中的数据元素是一个个的字符。字符串在计算机的很多方面应用很广。如在汇编和高级语言的编程程序中,源程序和目标程序都是字符串数据。在事务处理程序中,顾客的信息如姓名、地址等及货物的名称和产地和规格等,都被作为字符串来处理。另外,字符还具有自身的一些特性。因此,把字符串作为一种数据结构来研究。
原来学到串,我们发现还是在学线性表,看来线性表在数据结构中是一个很重要的知识点啊。我们在这一节学习的有关串类的创建,说一句大白话就是我们不用系统提供的方法来进行字符串的处理,而是自己去写方法,然后自己再如调用方法就行了。这样说是不是很简单的,其实前面的知识点也是这样的做法。接下来我们要创建子类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];
}
}
1索引器 public char this[index]
这个方法就是得到属性的
public char this[int index]{ get { return data[index]; } }
2//得到数字长度
public int GetLength()
{
return data.Length;
}
这个方法也是很简单的。
3//串比较
//如果当前字符串等于字符串的话,返回0
//如果当前字符串小于字符串的话,返回-1
//如果当前字符串大于字符串的话,返回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)
{
if (this[index] > s[index])
{
return 1;
}
else
{
return -1;
}
}
else
{
if (this.GetLength() == s.GetLength())
{
return 0;
}
else
{
if (this.GetLength() > s.GetLength())
{
return 1;
}
else
{
return -1;
}
}
}
}
这个串比较是有点麻烦的,我们在这里说一下它的算法。如果比较两个字符串的大小呢?就是判断在相同位置的ASCII大小,它的思想就是如此,但是其中还有有很多的情况的,所以还是要进行讨论的。我们首先判断要比较的字符串哪个的长度小并得到它的长度,然后用短的字符串的每一个字符比较相对应的长的字符串的字符并找到第一个不同的字符的索引。接下来进行判断,如果没有找到不同的字符的就比较长度就行;如果找到的话,就比较在同索引位置下他们的ASCII码谁大谁小就行,最后返回。
4 //求子串
public StringDS SubString(int index, int len)
{
char[] newData = new char[len];
for (int i = 0; i < len; i++)
{
newData[i] = data[i + index];
}
return new StringDS(newData);
}
这个方法的思想是就是得到指定索引开始发哦指定索引接受的字符并输出
5//串连接
public StringDS Concat(StringDS s)
{
//定义数组
char[] newData = new char[s.GetLength() + this.GetLength()];
//先进行this的赋值
for (int i = 0; i < GetLength(); i++)
{
newData[i] = this[i];
}
//在进行另一个赋值
for (int i = GetLength(); i < newData.Length; i++)
{
newData[i] = s[i - GetLength()];
}
return new StringDS(newData);
}
这个方法是将连个字符串连接起来,就是先创建一个大的数组,然后将两个字符串放入其中就行了。
6串插入
public StringDS Insert(int index, StringDS s)
{
//先定义数组的大小
char[] newData = new char[this.GetLength() + s.GetLength()];
for (int i = 0; i < GetLength(); i++)
{
newData[i] = this[i];
}
//移项
for (int i = newData.Length-1; i >index+s.GetLength()-1; i--)
{
newData[i] = newData[i-s.GetLength()];
}
//插入
for (int i = index; i < index + s.GetLength(); i++)
{
newData[i] = s[i - index];
}
return new StringDS(newData);
}核心思想 就是先移项空出位置,然后将数据插入
7//串删除
public StringDS Delete(int index, int len)
{
char[] newData = new char[GetLength() - len];
for (int i = 0; i < index; i++)
{
newData[i] = this[i];
}
for (int i = index; i < GetLength() - len; i++)
{
newData[i] = newData[i + len];
}
return new StringDS(newData);
}
核心思想缩短数组的大小
8//串定位
public int Index(StringDS s)
{
//就是判断首字符的索引在哪里
int index = -1;
for (int i = 0; i < data.Length; i++)
{
if (this[i] == s[0])
{
index = i;
break;
}
}
return index;
}这个的作用是把输出命名空间改成字符串
以上就是子类,接下来就是启动函数了:
StringDS stringDS = new StringDS("Hello World");
Console.WriteLine("现在串的长度:" + stringDS.GetLength());
Console.WriteLine(stringDS[0]);
StringDS s = new StringDS("cSharp");
Console.WriteLine(stringDS.Compare(s));
Console.WriteLine("求字串:" + stringDS.SubString(0, 5).newString());
Console.WriteLine("与" + "cSharp" + "进行连接:" + stringDS.Concat(new StringDS("cSharp")).newString());
Console.WriteLine("插入数据:" + stringDS.Insert(5, new StringDS(";
Console.WriteLine("删除数据:" + stringDS.Delete(5, 6).newString());
Console.WriteLine("串定位:" + stringDS.Index(new StringDS("World")));
Console.ReadKey();
得到结果

浙公网安备 33010602011771号