Data Structure --- C language implements the blockchain storage representation of strings, data structure --- C Language

Source: Internet
Author: User

Data Structure --- C language implements the blockchain storage representation of strings, data structure --- C Language

// Implement the blockchain storage representation of strings in C Language // Yang Xin # include <stdio. h> # include <stdlib. h> # include <string. h> # define CHUNKSIZE 80 # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define INFEASIBLE-1 # define OVERFLOW-2 char blank = '#'; typedef int Status; // defines the node typedef struct Chunk {char ch [CHUNKSIZE]; struct Chunk * next;} Chunk; // define the string typedef struct {Chunk * head, * tail; int curlen;} LString; void InitString (LString * T) {(* T ). curlen = 0; (* T ). head = NULL; (* T ). tail = NULL;} Status StrAssign (LString * T, char * chars) {int I, j, k, l; Chunk * p, * q; I = strlen (chars ); if (! I | strchr (chars, blank) return ERROR; (* T ). curlen = I; j = I/CHUNKSIZE; if (I % CHUNKSIZE) j ++; for (k = 0; k <j; k ++) {p = (Chunk *) malloc (sizeof (Chunk); if (! P) return ERROR; if (k = 0) (* T ). head = q = p; else {q-> next = p; q = p ;}for (l = 0; l <CHUNKSIZE & * chars; l ++) * (q-> ch + l) = * chars ++; if (! * Chars) {(* T ). tail = q; q-> next = NULL; for (; l <CHUNKSIZE; l ++) * (q-> ch + l) = blank ;}} return OK ;} status StrCopy (LString * T, LString S) {Chunk * h = S. head, * p, * q; (* T ). curlen = S. curlen; if (h) {p = (* T ). head = (Chunk *) malloc (sizeof (Chunk); * p = * h; h = h-> next; while (h) {q = p; p = (Chunk *) malloc (sizeof (Chunk); q-> next = p; * p = * h; h = h-> next ;} p-> next = NULL; (* T ). tail = p; return OK;} else return ERROR;} Status StrEmp Ty (LString S) {if (S. curlen) return FALSE; else return TRUE;} int StrCompare (LString S, LString T) {int I = 0; Chunk * ps = S. head, * pt = T. head; int js = 0, jt = 0; while (I <S. curlen & I <T. curlen) {I ++; while (* (ps-> ch + js) = blank) {js ++; if (js = CHUNKSIZE) {ps = ps-> next; js = 0 ;}; while (* (pt-> ch + jt) = blank) {jt ++; if (jt = CHUNKSIZE) {pt = pt-> next; jt = 0 ;}}; if (* (ps-> ch + js )! = * (Pt-> ch + jt) return * (ps-> ch + js)-* (pt-> ch + jt); else {js ++; if (js = CHUNKSIZE) {ps = ps-> next; js = 0;} jt ++; if (jt = CHUNKSIZE) {pt = pt-> next; jt = 0 ;}} return S. curlen-T.curlen;} int StrLength (LString S) {return S. curlen;} Status ClearString (LString * S) {Chunk * p, * q; p = (* S ). head; while (p) {q = p-> next; free (p); p = q;} (* S ). head = NULL; (* S ). tail = NULL; (* S ). curlen = 0; return OK;} Status Concat (LS Tring * T, LString S1, LString S2) {LString a1, a2; InitString (& a1); InitString (& a2); StrCopy (& a1, S1 ); strCopy (& a2, S2); (* T ). curlen = S1.curlen + S2.curlen; (* T ). head = a1.head; a1.tail-> next = a2.head; (* T ). tail = a2.tail; return OK;} Status SubString (LString * Sub, LString S, int pos, int len) {Chunk * p, * q; int I, k, n, flag = 1; if (pos <1 | pos> S. curlen | len <0 | len> S. curlen-pos + 1) return ERROR; n = len/CHUNKSIZE; if (Len % CHUNKSIZE) n ++; p = (Chunk *) malloc (sizeof (Chunk); (* Sub ). head = p; for (I = 1; I <n; I ++) {q = (Chunk *) malloc (sizeof (Chunk); p-> next = q; p = q;} p-> next = NULL; (* Sub ). tail = p; (* Sub ). curlen = len; for (I = len % CHUNKSIZE; I <CHUNKSIZE; I ++) * (p-> ch + I) = blank; q = (* Sub ). head; I = 0; p = S. head; n = 0; while (flag) {for (k = 0; k <CHUNKSIZE; k ++) if (* (p-> ch + k )! = Blank) {n ++; if (n> = pos & n <= pos + len-1) {if (I = CHUNKSIZE) {q = q-> next; I = 0;} * (q-> ch + I) = * (p-> ch + k); I ++; if (n = pos + len-1) {flag = 0; break ;}}p = p-> next;} return OK;} int Index (LString S, LString T, int pos) {int I, n, m; LString sub; if (pos> = 1 & amp; pos <= StrLength (S) {n = StrLength (S); m = StrLength (T); I = pos; while (I <= n-m + 1) {SubString (& sub, S, I, m); if (StrCompare (sub, T )! = 0) ++ I; else return I;} return 0;} void Zip (LString * S) {int j, n = 0; chunk * h = (* S ). head; char * q; q = (char *) malloc (* S ). curlen + 1) * sizeof (char); while (h) {for (j = 0; j <CHUNKSIZE; j ++) if (* (h-> ch + j )! = Blank) {* (q + n) = * (h-> ch + j); n ++;} h = h-> next;} * (q + n) = 0; ClearString (S); StrAssign (S, q);} Status StrInsert (LString * S, int pos, LString T) {int I, j, k; chunk * p, * q; LString t; if (pos <1 | pos> StrLength (* S) + 1) return ERROR; StrCopy (& t, T ); zip (S); I = (pos-1)/CHUNKSIZE; j = (pos-1) % CHUNKSIZE; p = (* S ). head; if (pos = 1) {t. tail-> next = (* S ). head; (* S ). head = t. head;} else if (j = 0) {for (k = 1; k <I; k ++) p = p-> next; Q = p-> next; p-> next = t. head; t. tail-> next = q; if (q = NULL) (* S ). tail = t. tail;} else {for (k = 1; k <= I; k ++) p = p-> next; q = (Chunk *) malloc (sizeof (Chunk); for (I = 0; I <j; I ++) * (q-> ch + I) = blank; for (I = j; I <CHUNKSIZE; I ++) {* (q-> ch + I) = * (p-> ch + I); * (p-> ch + I) = blank;} q-> next = p-> next; p-> next = t. head; t. tail-> next = q;} (* S ). curlen + = t. curlen; Zip (S); return OK;} Status StrDelete (LString * S, int pos, int len) {int I = 1; Chunk * p = (* S ). head; int j = 0; if (pos <1 | pos> (* S ). curlen-len + 1 | len <0) return ERROR; while (I <pos) {while (* (p-> ch + j) = blank) {j ++; if (j = CHUNKSIZE) {p = p-> next; j = 0 ;}} I ++; j ++; if (j = CHUNKSIZE) {p = p-> next; j = 0 ;}}; while (I <pos + len) {while (* (p-> ch + j) = blank) {j ++; if (j = CHUNKSIZE) {p = p-> next; j = 0 ;}} * (p-> ch + j) = blank; I ++; j ++; if (j = CHUNKSIZE) {p = p-> next; j = 0 ;}}; (* S ). curlen-= len; retur N OK;} Status Replace (LString * S, LString T, LString V) {int I = 1; if (StrEmpty (T) return ERROR; do {I = Index (* S, T, I); if (I) {StrDelete (S, I, StrLength (T); StrInsert (S, I, V ); I + = StrLength (V) ;}} while (I); return OK;} void StrPrint (LString T) {int I = 0, j; Chunk * h; h = T. head; while (I <T. curlen) {for (j = 0; j <CHUNKSIZE; j ++) if (* (h-> ch + j )! = Blank) {printf ("% c", * (h-> ch + j); I ++;} h = h-> next ;} printf ("\ n");} void DestroyString () {} int main () {char * s1 = "ABCDEFGHI", * s2 = "12345 ", * s3 = "", * s4 = "asd # tr", * s5 = "ABCD"; Status k; int pos, len; LString t1, t2, t3, t4; initString (& t1); InitString (& t2 ); printf ("= "===\ n "); printf ("after the initialization string t1, string t1 is empty? % D (1: NULL 0: No) String Length = % d \ n ", StrEmpty (t1), StrLength (t1); k = StrAssign (& t1, s3 ); if (k = OK) {printf ("string t1: \ n"); StrPrint (t1);} else printf ("error \ n "); k = StrAssign (& t1, s4); if (k = OK) {printf ("string t1: \ n"); StrPrint (t1 );} else printf ("error \ n"); k = StrAssign (& t1, s1); if (k = OK) {printf ("string t1: \ n "); strPrint (t1);} else printf ("error \ n"); printf ("string t1 null? % D (1: NULL 0: No) String Length = % d \ n ", StrEmpty (t1), StrLength (t1); StrAssign (& t2, s2 ); printf ("string t2: \ n"); StrPrint (t2); StrCopy (& t3, t1); printf ("Copy string t1 to get string t3, string t3 is: \ n "); StrPrint (t3); InitString (& t4); StrAssign (& t4, s5); printf (" string t4: "); StrPrint (t4 ); replace (& t3, t4, t2 ); printf ("= "===\ n "); printf ("after t2 is used to replace t4 in t3, t3 is: \ n"); StrPrint (t3); ClearString (& t1 ); printf ("====================== Separator =======================================\ n "); printf (" after the string t1 is cleared, string t1 null? % D (1: NULL 0: No) String Length = % d \ n ", StrEmpty (t1), StrLength (t1); Concat (& t1, t2, t3 ); printf ("= "===\ n "); printf ("string t1 (= t2 + t3) is: \ n"); StrPrint (t1); Zip (& t1); printf ("after removing unnecessary placeholders, string t1: \ n "); StrPrint (t1); pos = Index (t1, t3, 1); printf (" pos = % d \ n ", pos ); printf ("=" ===\ n "); printf ("insert string t2 before the pos character of string t1, enter pos: \ n"); scanf ("% d", & pos ); k = StrInsert (& t1, p OS, t2); if (k) {printf ("after string t2 is inserted, string t1 is: \ n"); StrPrint (t1);} else printf ("insertion failed! \ N "); return 0 ;}



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.