The---of the data structure The block chain storage representation of the C language implementation string

Source: Internet
Author: User

C language implements the block chain storage representation of the string//Xin Yang # 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; Define the block chain to store the strings of each node of the typedef struct CHUNK{CHAR ch[chunksize];struct Chunk *next;} chunk;//defines the block chain stored by 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 Strempty (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-&GT;CH+JS) ==blank) {js++;         if (js==chunksize) {ps=ps->next;       js=0;      }     };       while (* (PT-&GT;CH+JT) ==blank) {jt++;         if (jt==chunksize) {pt=pt->next;   jt=0;    }     };     if (* (PS-&GT;CH+JS)!=* (PT-&GT;CH+JT)) return * (PS-&GT;CH+JS)-* (PT-&GT;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 (lstring *t,lstring s1,lstring S2) {lstring a1,a2;   Initstring (&AMP;A1);   Initstring (&AMP;A2);   Strcopy (&AMP;A1,S1);   Strcopy (&AMP;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&gT   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&&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-&GT;CH+J)!=blank) {* (q+n) =* (H-&GT;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-&GT;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-&GT;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; return 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-&GT;CH+J)!=blank) {printf ("%c", * (h-&         GT;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 (&AMP;T1);   Initstring (&AMP;T2);   printf ("=============== delimiter =========================\n"); printf ("String T1 empty after initializing string T1?")   %d (1: Empty 0: NO) string length =%d\n ", Strempty (T1), strlength (t1));   K=strassign (&AMP;T1,S3); if (K==ok)  {printf ("string T1: \ n");   Strprint (t1);   } else printf ("Error \ n");   K=strassign (&AMP;T1,S4);     if (K==ok) {printf ("string T1: \ n");   Strprint (t1);   } else printf ("Error \ n");   K=strassign (&AMP;T1,S1);     if (K==ok) {printf ("string T1: \ n");   Strprint (t1);   } else printf ("Error \ n"); printf ("String T1 empty no?")   %d (1: Empty 0: NO) string length =%d\n ", Strempty (T1), strlength (t1));   Strassign (&AMP;T2,S2);   printf ("string T2: \ n");   Strprint (T2);   Strcopy (&AMP;T3,T1);   printf ("by string T1 copy to get string t3, string t3: \ n");   Strprint (T3);   Initstring (&AMP;T4);   Strassign (&AMP;T4,S5);   printf ("string T4:");   Strprint (T4);   Replace (&AMP;T3,T4,T2);   printf ("=============== delimiter =========================\n");   printf ("T2 replaces string t3 in the T4 string, the string t3 is: \ n");   Strprint (T3);   Clearstring (&AMP;T1);   printf ("=============== delimiter =========================\n"); printf ("After emptying the string T1, the string T1 empty no?")   %d (1: Empty 0: NO) string length =%d\n ", Strempty (T1), strlength (t1));   Concat (&AMP;T1,T2,T3);   printf ("=============== delimiter =========================\n"); printf ("String T1 (=t2+t3) is: \ n"));   Strprint (t1);   Zip (&AMP;T1);   printf ("After removing unnecessary placeholders, string T1: \ n");   Strprint (t1);   Pos=index (t1,t3,1);   printf ("pos=%d\n", POS);   printf ("=============== delimiter =========================\n");   printf ("Insert string T2 before the first POS character in string t1, enter pos: \ n");   scanf ("%d", &pos);   K=strinsert (&AMP;T1,POS,T2);     if (k) {printf ("after inserting the string t2, the string T1 is: \ n");   Strprint (t1); } else printf ("Insert failed!   \ n "); return 0; }



The---of the data structure The block chain storage representation of the C language implementation string

Related Article

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.