Reference article: http://blog.csdn.net/lijuwen/article/details/1353084
And then again in his own way to knock again, all kinds of wrong ....
Here's My Code:
#include "stdafx.h" #include <iostream> #include <string.h>using namespace Std;typedef char atomtype;// Defines the tag type typedef char atomtype;//defines an atomic type of character typedef enum{atom,list} elemtag;typedef Struct{char *ch;int length;} hstring;typedef struct Glnode{elemtag tag;union{atomtype data; Glnode *sublist;} A Glnode *next;} *gllist,glnode;//generates a string equal to Charsvoid strassign (HString *t,char *chars) {//If T exists a value, free space if T ((*t). Ch) ((*t), ch);// The length of the chars int i = strlen (chars); if (i ==0) {t->ch =null; t->length = 0;} else{//t= (hstring*) malloc (sizeof (HString));(*t). ch = (char*) malloc (sizeof (char));//Allocate a string of space if (!) ( *t). Ch) exit (0); for (int j=0;j<i;j++) t->ch[j]=chars[j]; T->length = i;}} Copy the value of S to Tvoid strcopy (HString *t,hstring *s) {if ((*t). Ch) Free ((*t). Ch);(*t). ch = (char*) malloc (sizeof (char)); *t). Ch) exit (0); for (int i=0;i<s->length;i++) T->ch[i] = s->ch[i]; T->length = S->length;} Determines whether the string is empty int strempty (HString *s) {if (s->length==0&&s->ch==null) return 1;else return 0;} //compares the size of two characters, the former is greater than the latter returns 1, the equivalent returns 0, the former is less than the latter return -1int strcompare (HString *t,hstring *s) {int i,j;for (i=0,j=0;i<t->length,j <s->length;i++,j++) if (t->ch[i]! = T->ch[j]) return T->ch[i]-t->ch[j];return t->length-s-> Length;} Get the length of the string int strLen (HString *s) {return s->length;} Empty the string void Clearstr (HString *s) {//So write with. CH should be no difference if (s->ch) {free (s->ch); S->ch = NULL;} s->length = 0;} Connection string void Strconcat (HString *t,hstring *s1,hstring *s2) {if ((*t). Ch) Free ((*t). Ch); T->length = S1->length + s2->length; (*t). ch = (char*) malloc (sizeof (char)); *t). Ch) exit (0); for (int i=0;i<s1->length;i++) t->ch[i] = s1->ch[i];for (int j=0;j<s2->length;j++) t >CH[S1->LENGTH+J]=S2->CH[J];} Assign S to Subvoid subStr (HString *sub,hstring *s,int pos,int length) {if (pos <1| |) by intercepting length long string from POS location pos>s->length| | length<0| | Length>s->length+1-pos) {printf ("%s", "pos or length invalid"); exit (0);} if ((*sub). Ch) Free ((*sub). Ch), if ((*s). ch = = null) {(*sub). ch = null;*sub). length=0;} else{(*sub). ch = (char*) malloc (sizeof (char)); *sub). Ch) exit (0); for (int i=0;i<length;i++) sub->ch[i]=s->ch[pos+i];sub->length = length;}} Initialize string void Initstr (HString *s) {(*s). length =0; (*s). ch = NULL;} Whether two strings have coincident, after the POS position of S, the coincident returns the first coincident subscript, not coincident returns -1int Indexstr (HString *t,hstring *s,int pos) {int length = (*s). length-(*t) . Length+1;if (pos>0) {for (int i=pos;i<length;i++) {HString temp;substr (&temp,s,i,length); if (!strcompare ( &temp,t)) return i;} return-1;} Insert Tvoid Strinsert (HString *s,int pos,hstring *t) in the POS position of S (pos<1| | Pos> (*s). length+1| | (*t). length<0) exit (0), if ((*s). Ch) Free ((*t). Ch);(*s). ch = (char*) malloc (sizeof (char)); *s). Ch) exit (0), for (int i= (*s). length-1;i>pos-1;i--)//(*s). Ch[i+pos] = (*s). Ch[i];(*s). ch[i+ (*t). Length] = (*s). ch[i];for (int i=0;i< (*t) length;i++) (*s). ch[pos+i-1] = (*t). Ch[i];(*s). Length + = (*t). Length;} Remove len length void Strdelete (HString *s,int pos,int len) {if (Len < 1| |) at POS location of S Len> (*s). length-1| | Pos<1|| Pos> (*s). length-1| | pos< (*s). Length-1-len) exit (0), for (int i=pos;i< (*s). length;i++) (*s). ch[pos]= (*s). Ch[pos+len];(*s). Length-= Len; (*s). ch = (char*) realloc ((*s). CH, (*s). length*sizeof (char));} Replace string, replace V with Tvoid strreplace (HString *s,hstring *t,hstring *v) {int i=1;if (Strempty (S)) exit (0);d o{i = Indexstr (s,v,i) ;//i is the location of the last look if (i) {Strdelete (S,i, (*v). length); Strinsert (s,i,t); i+= (*v). Length;} while (i);} Output strvoid strprint (HString *s) {for (int i=0;i< (*s). length;i++) printf ("%c", (*s). Ch[i]);p rintf ("/n");} Initialize Generalized table int initglist (gllist *l) {*l = Null;return 1;} The STR is divided into two parts, hstr the first, the previous string, str as the first, followed by the string int sever (HString *str,hstring *hstr) {printf ("%s", "Start execution sever/n"); HString ch,c1,c2,c3;initstr (&ch); Initstr (&C1); Initstr (&C2) initstr (&C3);p rintf ("%s", "Init success/n "); Strassign (&c1,", "); Strassign (&C2," ("); Strassign (&C3,") ");p rintf ("%s "," Strassgin success /n "); int n = strLen (str); int i=1;int k=0;do{printf ("%s "," begin to Inter do/n ");p rintf (" i=%d/n ", I); SubStr (&ch,str,i,1);p rintf ("substr:%c", &ch), if (!strcompare (&CH,&C2)) K++;else if (!strcompare (&ch,&c3 )) K--;++i;} while (I<=n&&strcompare (&CH,&C1) | | k!=0);p rintf ("&ch=%c", ch), if (i<n) {strcopy (&CH,STR); subStr (hstr,&ch,1,i-2); SubStr (Str,&ch,i, n-i+1);} Else{strcopy (HSTR,STR); clearstr (str);} return 1;} S is a generalized table-style string, L is the table to be created, void CreateList (Gllist &l,hstring *s) {gllist p; HString emp,sub,hsub;initstr (&emp); Initstr (&sub); Initstr (&hsub); Strassign (&emp, "()"); L = (gllist) malloc (sizeof (Glnode)); L) {printf ("%s", "CREATE Table Failed"); exit (0);} if (!strcompare (s,&emp))//create empty table {L->tag = list; L->next = NULL; L->a.sublist = NULL;} else if (StrLen (S) ==1)//create atomic table {L->tag = atom; L->next = NULL; L->a.data = (*s). Ch[0];} else//Ordinary Generalized table {L->tag = list; L->next = null;//outer brackets substr (&sub,s,1,strlen (S)-2);p rintf ("after outer brackets:%s", sub); Sever (&sub,&hsub); printf ("sublist=%s", Hsub);p rintf ("next=%s", sub), CreateList (l->a.sublist,&hsub);p = L->a.sublist;while (!strempty (&sub)) {sever (&sub,&hsub); CreateList (p->next,&hsub);p = p-> Next;}}} Destroy generalized table void Destroylist (Gllist *l) {gllist sublist,next;if (*l) {if ((*l)->tag = ATOM) sublist = Null;else sublist = (*L)- >a.sublist;next = (*l)->next;free (*l);(*l) = Null;destroylist (&sublist);d estroylist (&next);}} By generalized table L copy get generalized table tint copylist (gllist *t,gllist l) {if (! L) {*t = Null;return 1;}; T = (Gllist *) malloc (sizeof (gllist)); *t)) exit (0);(*t)->tag = l->tag;if (L->tag = ATOM) (*t)->a.data = L->a.data;else copylist (& (*t) A.sublist,l->a.sublist); if (L->next = = NULL) (*t)->next = L->next;else copylist (& (*t)->next,L-> Next); return 1;} Get the length of the table int listlength (gllist *l) {//If it is an empty table, return 0, if it is an atomic table, return 1, if it is a general table, P->TP, the number of elements that is the outermost parenthesis comma separated by the number of int length = 0; Gllist p;if ((*l)->tag = = LIST &&! ( *L)->a.sublist) return 0;else if ((*l)->tag = ATOM) return 1;else {p = (*l)->a.sublist;do{length++;p = P->next ;} WhiLe (p); return length;}} Get the depth of the table int listdepth (gllist *l) {//If the table is empty or the table is of type list but the table's sublist is empty, return1, if tag=0,return0.//otherwise is the general table, each take the tail, traverse once + 1, Max DEP for Max, return max+1if (*l==null| | ((*l)->tag ==list&& (*l)->a.sublist==null)) Return 1;else if ((*l)->tag = = ATOM) return 0;else{int max,depth; Gllist p;for (max=0,depth=0,p= (*l)->a.sublist;p;p=p->next) {depth = listdepth (&p); if (Depth>max) max = depth;} return max+1;}} Whether the list is empty int listempty (gllist *l) {//If the table does not exist or the table is a list type but sublist is empty, if (*l==null| | ((*l)->tag ==list&& (*l)->a.sublist==null)) return 1;else return 0;} Get header gllist GetHead (gllist *l) {//If it is empty table, no table header, otherwise, request tablespace, copy tag, if it is atom, copy atom value, if not, will l->sublist-> Sublist copy to the value to be returned if (Listempty (L)) return null;else{gllist Head;head = (gllist) malloc (sizeof (gllist)), if (!head) exit (0) ; Head->tag = (*l)->tag;if (Head->tag = = ATOM) Head->a.data = (*l)->a.data;elsecopylist (&head-> A.sublist, (*l)->next); return head;}} Get the footer gllist gettail (gllist *l) {//If it is an empty table, no footer, otherwise, the application formSpace, assignment tag, copy l->sublist->next, the footer is a chunk oh, is used to intercept the first, after the second half of the table//If it is an atomic table, there is no footer if (!). l| | (*l)->tag = = ATOM) return NULL; Gllist tail; tail = (gllist) malloc (sizeof (gllist)), if (!tail) exit (0), Tail->tag = List;tail->next = Null;copylist ( &tail->a.sublist, (*l)->a.sublist->next); return tail;} Insert void Insertfirst (Gllist *l,gllist e) {gllist p = (*l)->a.sublist; (*l)->a.sublist = E;e->next = P in front of the first element;} Delete the first element void Deletefirst (Gllist *l,gllist e) {//delete result assignment to E, if not empty, e=a.sublist,l->sublist=e->next,e->next= NULL, if empty, E=lif (*l) {e = (*l)->a.sublist; (*l)->a.sublist = e->next;e->next= NULL;} Elsee = *l;} void Traverse (Gllist *l) {//recursively traverse GL, if the table is not empty, if it is an atom, if it is a child table, sublist traversal,}//creates void createbyscanf from keyboard input (Gllist *l) {// All characters entered may be # letters (), one method has two inputs, the first input (, letters, # because the input (then the first input of the next function, so a # and the letter termination//second input,)) represents the NEX is null, representing the next value to start the input, Enter the next method of the first input char ch;scanf_s ("%c", &ch), if (ch = = ' # ') *l = Null;else if (ch = = ' (') {L = (Gllist *) malloc (sizeof (gllist)); if (! L) exit (0);(*l)->tag = LIST;CREATEBYSCANF (& (*l)->a.sublist);} Else{l = (Gllist *) malloc (sizeof (gllist)); L) exit (0);(*l)->tag = ATOM; (*l)->a.data = ch;} scanf_s ("%c", &ch), if (ch = = ', ') createbyscanf (& (*l)->next); else if (ch = = ') ') (*l)->next = NULL;} void Main () {HString T; Gllist l,m;initstr (&t); initglist (&l); initglist (&m);/*char c[80];gets_s (c); Strassign (&T,C); CreateList (l,&t);//The value of each sub-loop is the same, but each execution of the sub is different from printf ("Generalized table L length =%d/n", Listlength (&l)); */CREATEBYSCANF ( &L);p rintf ("The length of the generalized table L =%d/n", Listlength (&l));/*hstring t,m;initstr (&t); Initstr (&m); Char C[10];gets_ s (c); Strassign (&t,c); subStr (&m,&t,1,1); Strprint (&m); */}
The display is out of tune, and there's a chance to tune it again.
Strings and Generalized tables