6 Data Structure notes

Source: Internet
Author: User
ArticleDirectory
    • Basic concepts of strings
    • Implementation of basic string operations

In the ApplicationProgramThe most frequently used type is a string. String is short for string. It is a special line table. Its special feature is that the data elements in the string are character by character. Strings are widely used in many computer fields. For example, in the Compilation Program of assembly and advanced language, both the source program and the target program are strings. In the transaction processing program, the customer's information, such as name and address, as well as the goods name, origin and format, are all processed as strings. In addition, strings have some characteristics. Therefore, the string is studied as a data structure.

Basic concepts of strings

String is a finite sequence consisting of n (n ≥ 0) characters. It is generally recorded as: S = "c1c2... CN (n ≥ 0) where S is the string name, double quotation marks are used as the separator of the string, and the Character Sequence caused by double quotation marks is the string value. Ci (1 ≤ I ≤ n) can be letters, numbers or other characters. n is the length of a string. When n = 0, it is called an empty string ). A subsequence composed of any consecutive characters in a string is called a substring of the string ). The string containing the substring is called as the primary string. The position of the first character of a substring in the primary string is the position of the substring.

Because the characters in the string are continuously stored, the string in C # has a constant feature, that is, once a string is created, it cannot be extended, shortened, or changed to any character. Therefore, we will not discuss the chain storage of strings here, nor use interfaces to represent string operations.

Implementation of basic string operations

1. Obtain the string length
The length of a string is the number of characters in the string. You can evaluate the length of the string by calculating the length of the array data. The following describes how to evaluate the length of a string:

 
 Public IntGetlength (){ReturnData. length ;}

2. String comparison
If the two strings are of the same length and contain the same characters, the strings are equal and 0 is returned; if the character at the corresponding position of the string S is greater than the character of the string or if the length of the string S is greater than that of the string, and the character at the corresponding position in the length of the string is the same, returns-1, which is less than string s; returns 1 for other cases, which is greater than string S. String comparisonAlgorithmThe implementation is as follows:

 Public   Int Compare (stringds s ){ Int Len = (( This . Getlength () <= S. getlength ())? This . Getlength (): S. getlength ());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 ;}

3. substring search
From the index position of the Main string, find the substring with the length of Len. If it is found, return the substring. Otherwise, return an empty string. The algorithm is implemented as follows:

  Public  stringds substring ( int  index,  int  Len) { If  (index <0) | (index>  This . getlength ()-1) | (LEN <0) | (LEN>  This . getlength ()-index) {console. writeline (" position or length is error!  ");  return   null ;} stringds S =  New  stringds (LEN );  for  ( int  I = 0; I 
  
    This  [I + index-1];} 
    return  S ;}
  

4. String connection
Concatenates a string with another string into a string, and returns a new string. The length of the new string is the sum of the two strings, and the first part of the new string is the original string, the length is the length of the string. The latter part of the new string is the length of the string S. The serial join algorithm is implemented as follows:

 
PublicStringds Concat (stringds s) {stringds S1 =NewStringds (This. Getlength () + S. getlength ());For(IntI = 0; I <This. Getlength (); ++ I) {s1.data [I] =This[I];}For(IntJ = 0; j <S. getlength (); ++ J) {s1.data [This. Getlength () + J] = s [J];}ReturnS1 ;}

5. String insertion
String insertion is to insert a string s at the index of a string. If the position meets the condition, the operation returns a new string. The length of the new string is the sum of the length of the string and the length of the string S, the first part of the new string is the character between the start character of the string and the index character, and the second part is the string S, part 2 is the character from the index position to the end position of the string. If the position does not match the condition, an empty string is returned. The string Insertion Algorithm is as follows:

 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 ;}

6. delete a string
The string is deleted from the main string from the consecutive Len characters starting from the index position of the string. If the position and length meet the conditions, this operation returns a new string. The length of the new string is the length of the original string minus Len, the first part of the new string is the character from the start of the original string to the index position, and the last part is the character from the index + Len position to the end of the original string. If the position and length do not match the condition, an empty string is returned. The string deletion algorithm is implemented as follows:

       Public Stringds Delete ( Int Index, Int Len ){ If (Index <0) | (index> This . Getlength ()-1) | (LEN <0) | (LEN> This . Getlength ()-index) {console. writeline (" Position or length 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 ;}

7. Locate strings
Locate the first occurrence of the substring s in the primary string. If it is found, return the position where the substring s appears for the first time in the main string. Otherwise, return-1. The string locating algorithm is implemented as follows:

 
Public IntIndex (stringds s ){If(This. Getlength () <S. getlength () {console. writeline ("There is not string s!");Return-1 ;}IntI = 0;IntLen =This. Getlength ()-S. getlength ();While(I <Len ){If(Compare (s) 0 ){Break;}}If(I <= Len ){ReturnI ;}Return-1 ;}

 

In C #, a string represents a constant set of character sequences. String is a closed type, so,It cannot be inherited by other classes, but it directly inherits from the object. Therefore, string is a reference type, not a value type,Allocate space on the managed stack rather than on the thread Stack. The string type also inherits icomparable, icloneable, iconvertible, icomparable <string>, ienumerable <char>, ienumerable, and iequatable <string> interfaces. The constant of string indicates that a string cannot be extended, shortened, or changed any character once it is created. So,When we operate on a string, the string cannot be changed., C # also provides Stringbuilder Type to support efficient dynamic creation of strings.
In C #, the new operator cannot be used to create a string. Instead, a mechanism called string resident is used.This is because the C # Language regards string as a primitive type. The primitive type is directly supported by the compiler.Source codeUse literal to directly express strings. When C # CompilerCodeDuring compilation, the text constant string is stored in the metadata of the hosting module. When CLR is initialized, CLR creates an empty hash, where the key is a string and the value is a reference to the character string object in the managed heap. A hash table is a hash table. When the JIT compiler compiles a method, it searches for each text constant string in the hash. If it cannot be found, a New String object (pointing to a string) will be constructed in the managed heap, and then the string and the reference to the string object will be added to the hash list; if yes, no operation is performed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.