Generalized table-to-tail list storage representation//Xin Yang # include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h > #define Maxstrlen) typedef char SSTRING[MAXSTRLEN+1]; typedef char Atomtype; Defines the atomic type as a character typedef enum{atom, LIST//atom==0: Atomic list==1: Sub-table} Elemtag; typedef struct GLNODE{ELEMTAG tag;//public part, used to differentiate between atomic nodes and table nodes union//atomic nodes and table nodes in the joint part {Atomtype atom;//Atom is the domain of atomic nodes, Atomtype by user Define Struct{struct Glnode *HP,*TP;} ptr PTR is the pointer field of the table node, PRT.HP and Ptr.tp point to the header and footer}a respectively;} *glist, glnode;//initialization of the generalized table Lint initglist (GList *l) {*l = Null;return 1;} Destroy generalized table Lvoid destroyglist (GList *l) {GList q1,q2;if (*l) {if (*l)->tag = ATOM) {free (*l); *l = NULL;} ELSE{Q1 = (*l)->a.ptr.hp;q2 = (*l)->a.ptr.tp;free (*l), *l = Null;destroyglist (&q1);D estroyglist (&Q2);// Recursive deletion of header and footer nodes}}}//uses the head-and-tail list storage structure, which is replicated by generalized table L to obtain generalized table T. int copyglist (GList *t,glist L) {if (! L) *t = Null;else{*t = (GList) malloc (sizeof (Glnode)), if (!*t) exit (0);(*t)->tag = l->tag;if(L->tag = = ATOM) (*t)->a.atom = l->a.atom;//Copy the single atom else//is a table node, then copy the header and footer {copyglist (& (*t)->a.ptr.hp), L->A.PTR.HP); Copyglist (& (*t)->a.ptr.tp), L->A.PTR.TP);}} return 1;} Returns the length of a generalized table, that is, the number of elements int glistlength (GList L) {int len = 0;if (! L) return 0;if (L->tag = = ATOM) return 1;while (l) {L = l->a.ptr.tp;len++;} return Len;} The depth of the generalized table L is obtained by using the storage structure of the tail-chain table.
int glistdepth (GList L) {int Max, DEP; GList pp;if (! L) return 1;//empty table depth is 1if (L->tag = = Atom) Return 0;//atomic depth is 0for (max = 0, pp = L; pp; pp = pp->a.ptr.tp) {//recursive pp->a. PTR.HP is the child of the head pointer in depth DEP = Glistdepth (PP->A.PTR.HP); if (Dep > max) max = dep;} Return max+1;//The depth of the non-empty table is the maximum value of the depth of each element plus 1}//to determine if the generalized table is an empty int glistempty (GList L) {if (! L) return 1;elsereturn 0;} Take the head of the generalized table L GList gethead (GList l) {GList h,p;if (! L) {printf ("Empty table without table header!\n"); exit (0);} p = l->a.ptr.tp; L->A.PTR.TP = NULL; Copyglist (&h,l); L->A.PTR.TP = P;return h;} Take the generalized table L of the tail GList gettail (GList l) {GList t;if (! L) {printf ("Empty table without footer!\n"); exit (0);} Copyglist (&t, L->A.PTR.TP); return t;} Insert Element e As the first element of the Generalized table L (table header, also possibly child table) int Insertfirst_gl (GList *l,glist e) {GList p = (GList) malloc (sizeof (Glnode)); if (!p) exit (0);p->tag = list;p->a.ptr.hp = E;P->A.PTR.TP = *l;*l = P;return 1;} Delete the first element of the generalized table L, and return its value int deletefirst_gl (GList *l,glist *e) {GList p;*e = (*l)->a.ptr.hp;p = *l;*l = (*l)->a.ptr.tp; with E Free (p); return 1;} Using recursive algorithm to traverse generalized table L void TraveRSE_GL (GList l,void (*v) (Atomtype)) {if (L) if (L->tag = = ATOM) v (l->a.atom); Else{traverse_gl (L->A.PTR.HP,V); TRAVERSE_GL (L->A.PTR.TP,V);}} Generate a string whose value equals chars tint strassign (sstring T,char *chars) {int i;if (strlen (chars) > Maxstrlen) return 0;else{t[0] = Strle n (chars); for (i = 1; I <= t[0]; i++) T[i] = * (chars + i-1); return 1;}} Copied by string S tint strcopy (sstring T, sstring S) {int i;for (i = 0; I <= s[0]; i++) T[i] = S[i];return 1;} Returns 1 if S is an empty string, otherwise 0 int strempty (sstring S) {if (s[0] = = 0) return 1;elsereturn 0; If s>t, the return value >0; if s=t, the return value = 0; if s<t, the return value <0 int strcompare (sstring s,sstring T) {int i;for (i = 1; I <= s[0] & & I <= t[0]; ++i) if (s[i]! = T[i]) return s[i]-T[i];return s[0]-t[0];} Returns the number of elements of the string int strlength (sstring S) {return s[0];} Clear S to empty string int clearstring (sstring S) {s[0] = 0;//string length to zero return 1;} Returns a substring of length len from the first POS character of the string s with a sub. int SubString (sstring sub,sstring s,int pos,int len) {int i;if (pos < 1 | | pos >s[0] | | Len < 0 | | len > S[0]-P OS+1) return 0;for (i = 1; I <= len; i++) sub[i]=s[pos+i-1]; Sub[0] = Len;return 1;} The non-empty string str is cut into two parts: Hsub as the first ', ' before the substring, str as the substring after the sever (sstring str,sstring hstr) {int n,i, K; Sstring ch,c1,c2,c3;n = strlength (str); Strassign (C1, ","); Strassign (C2, "("); Strassign (C3, ")"); SubString (ch,str,1,1); for (i = 1,k = 0;i <= n && strcompare (ch,c1) | | K! = 0; ++i) {SubString (ch, str, I, 1); if ( ! Strcompare (CH, c2)) ++k;else if (! Strcompare (CH,C3))--k;} if (i <= N) {SubString (Hstr, str, 1, i-2); SubString (str, str, I, n-i + 1);} Else{strcopy (Hstr, str); Clearstring (str);}} Generalized table L. Set emp= "()" int createglist (GList *l, sstring S) {sstring sub,hsub,emp; GList p,q; Strassign (EMP, "()"); if (! Strcompare (S, emp)) *l = null;//Create empty table Else{*l = (GList) malloc (sizeof (Glnode)), if (!*l)//Build Table node exit (0); if (Strlength (S) = = 1) S is a single atom {(*l)->tag = Atom; (*l)->a.atom = s[1]; Create a single-atom generalized table}else{(*l)->tag = List;p = *l; SubString (Sub, S, 2, strlength (s)-2); The outer brackets do{//the N sub-table sever (sub, hsub);//Detach the header string from the Sub Hsub Createglist (&P->a. ptr. HP, hsub); q = p;if (! Strempty (sub))//The footer is not empty {p = (Glnode *) malloc (sizeof (Glnode)), if (!p) exit (0);p->tag = LIST;Q->A.PTR.TP = P;}} while (! Strempty (sub)); q->a.ptr.tp = NULL;}} return 1;} Print atomic void visit (Atomtype e) {printf ("%c", e);} int main () {///Generalized table representation is, empty table: (), single atom: A, table: (A, (b), C, (d, (e))) char p[80] = {"(A, (b), C, (d, (e)))"}; Sstring T; GList l,m;initglist (&l); Initglist (&m);p rintf ("The depth of the empty generalized table L =%d\nl is empty?"). %d (1: Yes 0: NO) \ n ", Glistdepth (L), Glistempty (L)); Strassign (t, p); Createglist (&l, t);//Create Generalized table printf ("\ n the length of the generalized table L =%d\n", Glistlength (L));p rintf ("The depth of the generalized table L =%d \nl is empty?"). %d (1: Yes 0: NO) \ n ", Glistdepth (L), Glistempty (L));p rintf (" Traverse Generalized table l:\n "); TRAVERSE_GL (L, visit);p rintf ("\ n" copy generalized table m = l\n "); Copyglist (&m, L);p rintf ("The length of the generalized table m =%d\n", Glistlength (m));p rintf ("Depth of the generalized table m =%d\n", Glistdepth (m));p rintf ("Traversing the generalized table m: \ n "); TRAVERSE_GL (m,visit);D estroyglist (&m), M = GetHead (l);p rintf ("\N\NM is the table header of L, traversing generalized table m:\n"); TRAVERSE_GL (M, visit);D estroyglist (&m), M = GetTail (l);p rintf ("\n\nm is the footer of L, traversing the generalized table m:\ n "); TRAVERSE_GL (m,visit); Insertfirst_gl (&m, L);p rintf ("\ n \ nyou insert a table header with L m. Traversing generalized table m:\n "); TRAVERSE_GL (m,visit);p rintf ("\ nyou delete the table header of M. Traverse Generalized Table m:\n ");D estroyglist (&l);D eletefirst_gl (&m, &l); TRAVERSE_GL (M, visit);p rintf ("\ n");D estroyglist (&m); return 0;}
---c language of data structure to implement generalized table-tail chain table storage representation