_ DataStructure_C_Impl: chain string,
// _ DataStructure_C_Impl: chain string # include <stdio. h> # include <stdlib. h> # include <string. h> # define ChunkSize 4 # define stuff '#' // define the node type of the string typedef struct Chunk {char ch [ChunkSize]; struct Chunk * next;} Chunk; // Type Definition of the chain string typedef struct {Chunk * head; Chunk * tail; int length;} LinkString; // initialization string Svoid InitString (LinkString * S) {S-> length = 0; // set the string length to 0 S-> head = S-> tail = NULL; // set the header pointer and tail pointer of the string to null} // generate a string S whose value is equal to cstr. 1 is returned. Otherwise, 0 int StrAssign (LinkString * S, char * cstr) {int I, j, k, len; Chunk * p, * q; is returned; len = strlen (cstr); // len is the length of the chain string if (! Len) return 0; S-> length = len; j = len/ChunkSize; // j indicates the number of knots of the chain string if (len % ChunkSize) j ++; for (I = 0; I <j; I ++) {p = (Chunk *) malloc (sizeof (Chunk); // dynamically generate a node if (! P) return 0; for (k = 0; k <ChunkSize & * cstr; k ++) * (p-> ch + k) = * cstr ++; // assign the characters in the string ctrs to the data field if (I = 0) of the chain string // if it is the first node S-> head = q = p; // The header Pointer Points to the first node else {q-> next = p; q = p;} if (! * Cstr) {// if it is the last link node S-> tail = q; // point the tail pointer to the last node q-> next = NULL; // set the pointer field of the tail pointer to NULL for (; k <ChunkSize; k ++) * (q-> ch + k) = stuff; // fill the last node with '#'} return 1;} // determine whether the string is empty. If S is an empty string, 1 is returned; otherwise, 0 int StrEmpty (LinkString S) {if (S. length = 0) return 1; elsereturn 0;} // evaluate the length of the string int StrLength (LinkString S) {return S. length ;}// String Conversion operation. Converts the content of string S to a string, and copies the characters in string S to cstr. 1 is returned. Otherwise, 0 int ToChars (LinkString S, char ** cstr) {Chunk * p = S is returned. head; // point p to the 1st node int I in string S; char * q; * cstr = (char *) malloc (S. length + 1) * sizeof (char); if (! Cstr |! S. length) return 0; q = * cstr; // point q to cstrwhile (p) {// The blockchain is not over for (I = 0; I <ChunkSize; I ++) if (p-> ch [I]! = Stuff) // if the current character is not a special character '#', assign the character in S to q * q ++ = (p-> ch [I]); p = p-> next;} (* cstr) [S. length] = '\ 0'; // Add the end sign return 1 at the end of the string;} // int StrCopy (LinkString * T, LinkString S) of the string copy operation) {char * str; int flag; if (! ToChars (S, & str) // copy the characters in string S to return 0; flag = StrAssign (T, str ); // assign the str character to the free (str); // release the str space return flag;} // compare the string. If the value of S is greater than T, a positive value is returned. If the value of S is equal to T, 0 is returned. If the value of S is less than T, a negative value int StrCompare (LinkString S, linkString T) {char * p, * q; int flag; if (! ToChars (S, & p) // convert string S to string preturn 0; if (! ToChars (T, & q) // converts string T to string qreturn 0; for (; * p! = '\ 0' & * q! = '\ 0';) if (* p = * q) {p ++; q ++;} else {flag = * p-* q; free (p); // release the space of p free (q); // release the space of q return flag ;} if (* p = '\ 0' | * q =' \ 0') {free (p); free (q); return S. length-T.length;} // link operation on the string. Connect string S to the end of string T int StrConcat (LinkString * T, LinkString S) {int flag1, flag2; LinkString S1, S2; InitString (& S1 ); initString (& S2); flag1 = StrCopy (& S1, * T); // copy the content of string T to flag2 = StrCopy (& S2, S) in S1 ); // copy the content of string S to S2 if (flag1 = 0 | flag2 = 0) // if a string fails to be copied, return 0 return 0; t-> head = S1.head; // modify the header pointer S1.tail of string T-> next = S2.head; // connect string S1 and S2 to the beginning and end of string T-> tail = S2.tail; // modify the tail pointer T of string T-> length = S. length + T-> length; // modify the length of string T return 1;} // Insert the string. Insert string Tint StrInsert (LinkString * S, int pos, LinkString T) {char * t1, * s1; int I, j; int flag; if (pos <1 | pos> S-> length + 1) return 0; if (! ToChars (* S, & s1) // converts string S to string s1return 0; if (! ToChars (T, & t1) // converts string T to string t1return 0; j = strlen (s1); // j is the length of string s1 s1 = (char *) realloc (s1, (j + strlen (t1) + 1) * sizeof (char); // realloc space for s1 (I = j; I> = pos-1; I --) // move the character after pos in string s1 to the position following strlen (t1) s1 [I + strlen (t1)] = s1 [I]; for (I = 0; I <(int) strlen (t1); I ++) // insert t1s1 in string s1 [pos + I-1] = t1 [I]; initString (S); flag = StrAssign (S, s1); // The Sfree (t1); free (s1); return flag ;} // delete a string. Delete int StrDelete (LinkString * S, int pos, int len) {char * str; int I; int flag; if (pos <1 | pos> S-> length-len + 1 | len <0) return 0; if (! ToChars (* S, & str) // converts string S to string strreturn 0; for (I = pos + len-1; I <= (int) strlen (str ); I ++) // Delete the substring whose length starts with the pos character in the string is len. str [I-len] = str [I]; InitString (S ); // The original bucket flag for S release = StrAssign (S, str); // converts string str to string Sfree (str); return flag ;} // obtain the substring operation. Returns the SubString int SubString (LinkString * Sub, LinkString S, int pos, int len) {char * t, starting from the second pos character of string S, * str; int flag; if (pos <1 | pos> S. length | len <0 | len> S. length-pos + 1) return 0; if (! ToChars (S, & str) // converts string S to string strreturn 0; t = str + pos-1; // t points to the pos character t [len] = '\ 0' in the string str; // end the Sub to' \ 0' flag = StrAssign (Sub, t ); // convert string t to Subfree (str); return flag;} // clear string operation. Release the space of the string void ClearString (LinkString * S) {Chunk * p, * q; p = S-> head; while (p) {q = p-> next; free (p); p = q;} S-> head = S-> tail = NULL; S-> length = 0 ;} // output void StrPrint (LinkString S) {int I = 0, j; Chunk * h; h = S. head; while (I <S. length) {for (j = 0; j <ChunkSize; j ++) if (* (h-> ch + j )! = Stuff) {printf ("% c", * (h-> ch + j); I ++;} h = h-> next ;} printf ("\ n") ;}void main () {int I, j; int flag; LinkString S1, S2, S3, Sub; char * str1 = "Welcome "; char * str2 = "Data Structure"; char * str3 = "Computer Architecture"; printf ("initialization and assignment of strings: \ n"); InitString (& S1 ); /* initialize S1, S2, and S3 strings */InitString (& S2); InitString (& S3); InitString (& Sub); StrAssign (& S1, str1 ); /* assign values to S1, S2, and S3 strings */StrAssign (& S2, str2); StrAssign (& S3, str3); printf ("the value of S1 is: "); StrPrint (S1); printf (" string S2 value: "); StrPrint (S2); printf (" string S3 value :"); strPrint (S3); printf ("% d \ n", StrCompare (S1, S2); printf ("connect string S2 to the end of string S1: \ n "); strConcat (& S1, S2);/* connect string S2 to the end of string S1 */printf ("S1 is:"); StrPrint (S1 ); printf ("Delete the 14 characters after the first position of the string S1: \ n"); StrDelete (& S1, 12th ); /* Delete the 14 characters after the first position in string S1 */printf ("S1 is:"); StrPrint (S1 ); printf ("insert string S3 to string S1 after 12th characters: \ n"); StrInsert (& S1, 12, S3 ); /* Insert the S3 string to the 12th characters of the S1 string */printf ("S1 is:"); StrPrint (S1 ); printf ("Remove the 8 characters after the 12th characters in string S1 and assign them to the string Sub: \ n"); SubString (& Sub, S1 ); /* remove the 8 characters after the first position in string S1 and assign the value to Sub */printf ("Sub is:"); StrPrint (Sub ); system ("pause ");}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.