The logical structure of a string is similar to that of a linear table. The difference is that the string is for character sets, that is, the elements in a string are all characters, even if the character in the string is composed of numbers such as "123, or "2010-10-1ct" can only be understood as a string with a length of 3 and a length of 10. Each element is only a character.
Therefore, the basic operations on strings are very different from those on Linear tables. A linear table is more concerned with operations on a single element, such as finding an element, inserting or deleting an element, however, the string contains operations such as seeking the position of the substring, getting the specified position substring, and replacing the substring.
To gain a deeper understanding of the string data structure, we need to define the string ADT and then implement some common operations on the string. The ADT of the string is defined as follows:
The element in the ADT string (string) data string is composed of only one character. The adjacent element has a precursor and a Successor Relationship. operationstrassign (& T, chars) initial condition: chars is a String constant. Operation Result: generate a string T whose value is equal to chars. Strcopy (& T, S) initial condition: the string s exists. Operation Result: the string T is copied from string S. Initial strempty (s) condition: the string s exists. Operation Result: If s is an empty string, true is returned; otherwise, false is returned. Strcompare (S, T) initial condition: the strings S and T exist. Operation Result: If S> T, the return value is greater than 0. If S = t, the return value is greater than 0. If S <t, the return value is <0. Strlength (s) initial condition: the string s exists. Operation Result: the number of elements returned by S is called the length of a string. Clearstring (& S) initial condition: the string s exists. Operation Result: Clear s as an empty string. Concat (& T, S1, S2) Initial Conditions: strings S1 and S2 exist. Operation Result: T is used to return a new string joined by S1 and S2. Substring (& sub, S, POs, Len) initial condition: String s exists, 1 ≤ POS ≤ strlength (s) and 0 ≤ Len ≤ strlength (s) -pos + 1 Operation Result: Use sub to return the substring whose second POS character is Len. Index (S, T, POS) Initial Conditions: strings S and T exist, t is non-empty string, 1 ≤ POS ≤ strlength (s ). Operation Result: if the primary string S contains a sub-string with the same tvalue as the string, the first position after the POs character in the primary string S is returned; otherwise, the function value is 0. Replace (& S, T, v) Initial Conditions: strings S, T, and V exist, and t are non-empty strings. Operation Result: Use V to replace all the non-overlapping substrings in the primary string s that are equal to T. Strinsert (& S, POs, T) Initial Conditions: strings S and T exist, 1 ≤ POS ≤ strlength (s) + 1. Operation Result: insert string t before the POs character of string S. Initial Condition of strdelete (& S, POs, Len): String s exists. 1 ≤ POS ≤ strlength (S)-len + 1. Operation Result: remove the substring whose length is Len from string S. Destroystring (& S) initial condition: the string s exists. Operation Result: the string S is destroyed. Endadt
The following five operations are called the minimum operation subset:
- Strassign
- String comparison strcompare
- Evaluate strlength
- Connection Concat
- Substring
String abstract data type ADT