GList.h #ifndef GLIST_H_H #define OK 1 #define ERROR 0 typedef SString ElemType; typedef enum {ATOM, LIST} ElemTag; typedef struct GLNode { ElemTag tag; //区分原子结点和表结点 union { ElemType atom; //原子的类型 struct GLNode *hp; //表结点的头指针 }; struct GLNode *tp; //下一个结点指针,相当于单链表的next指针 }*GList; int init_glist(GList*); int make_node(GLNode**); int sever(SString, SString); int create_glist(GList*, SString); int print_glist(GList); int destroy_glist(GList*); int getLength_glist(GList); int empty_glist(GList); int copy_glist(GList*, GList); int general_length(GList); int depth_glist(GList); int get_head(GList*, GList); int get_tail(GList*, GList); int insert_first(GList, GList); int delete_first(GList, GList*); #endif GList.cpp #include<stdio.h> #include<stdlib.h> #include"SString.h" #include"GList.h" int init_glist(GList *glist) { (*glist) = NULL; return OK; } int make_node(GLNode **node) { if(!((*node) = (GLNode*)malloc(sizeof(GLNode)))) return ERROR; return OK; } int create_glist(GList *glist, SString str) { SString emp; sstr_assign(emp, "()"); if(sstr_compare(str, emp) == 0) (*glist) = NULL; else { //GLNode *node; if(!make_node(glist)) return ERROR; if(sstr_length(str) == 1) { (*glist)->tag = ATOM; //如果只有一个原子,就直接复制。 sstr_copy((*glist)->atom, str); (*glist)->tp = NULL; } else { (*glist)->tag = LIST; //tmp指向表头结点,方便递归操作。 GLNode *tmp = (*glist); //脱掉最外层括号 SString sub; SString hsub; sstr_subString(sub, str, 2, sstr_length(str) - 2); //脱掉括号之后就可以进行重复建表 do { sever(hsub, sub); //递归建立表结点 create_glist(&tmp->hp, hsub); GLNode *tmp1 = tmp; //如果表尾不空,就生成表尾结点 if(!sstr_empty(sub)) { //GLNode *tail = NULL; if(!make_node(&tmp)) return ERROR; tmp->tag = LIST; tmp1->tp = tmp; } }while(!sstr_empty(sub)); tmp->tp = NULL; } } return OK; } int sever(SString sub, SString str) { if(str[0] == 0) return ERROR; int n = sstr_length(str); int i = 0; int k = 0; //k尚未匹配的左括号数 SString ch; do { i++; sstr_subString(ch, str, i, 1); if(ch[1] == '(') k++; else if(ch[1] == ')') k--; }while(i < n && (ch[1] != ',' || k != 0)); if(i < n) { sstr_subString(sub, str, 1, i - 1); //str = str - sub sstr_subString(str, str, i + 1, n - i); } else { sstr_copy(sub, str); clear_sstring(str); } return OK; } /*int destroy_glist(GList *glist) { if(glist == NULL) return ERROR; GLNode *tmp = *glist; if(tmp->tag == LIST && tmp->hp) { destroy_glist(&tmp->hp); if(tmp->tp) destroy_glist(&tmp->tp); free(tmp); return OK; } else { if(tmp->tag == ATOM) { free(tmp); return OK; } if(tmp->tp) destroy_glist(&tmp->tp); free(tmp); } (*glist) = NULL; return OK; }*/ int destroy_glist(GList *glist) { GLNode *hp, *tp; if((*glist)) { if((*glist)->tag == LIST) { hp = (*glist)->hp; } else { hp = NULL; } tp = (*glist)->tp; free((*glist)); (*glist) = NULL; destroy_glist(&hp); destroy_glist(&tp); } return OK; } int copy_glist(GList *glist, GList glist1) { if(glist1 == NULL) { (*glist) = NULL; } else { if(!make_node(glist)) return ERROR; (*glist)->tag = glist1->tag; if(glist1->tag == ATOM) { sstr_copy((*glist)->atom, glist1->atom); (*glist)->tp = NULL; } else { copy_glist(&(*glist)->hp, glist1->hp); copy_glist(&(*glist)->tp, glist1->tp); } } return OK; } //返回元素的个数,即广义表的长度(只计算子表的长度) //如果广义表的元素全部都是子表,如(D = (A, B, C)) //则这个函数不能计算元素个数 int getLength_glist(GList glist) { if(glist == NULL) return ERROR - 1; int len = 0; //判断是否为空表 if(glist->tag == LIST && !glist->hp) return 0; //判断是否为单原子表 else if(glist->tag == ATOM) return 1; else { GLNode *tmp = glist->hp; do { len++; tmp = tmp->tp; }while(tmp); } return len; } int general_length(GList glist) { if(glist == NULL) return ERROR - 1; GLNode *tmp = glist; int len = 0; while(tmp) { len++; tmp = tmp->tp; } return len; } int depth_glist(GList glist) { if(glist == NULL) return 1; if(glist->tag == ATOM) return 0; int max, dep; GLNode *tmp; for(max = 0, tmp = glist ; tmp ; tmp = tmp->tp) { dep = depth_glist(tmp->hp); if(dep > max) max = dep; } return max + 1; } int empty_glist(GList glist) { if(!glist || glist->tag == LIST || !glist->hp) return true; return false; } //获取表头 int get_head(GList *head, GList glist) { if(glist == NULL || (glist->tag == LIST && !glist->hp)) { return ERROR; } if(!make_node(head)) return ERROR; (*head)->tag = glist->tag; (*head)->tp = NULL; if(glist->tag == ATOM) { sstr_copy((*head)->atom, glist->atom); } else { copy_glist(head, glist->hp->hp); } return OK; } //获取表尾 int get_tail(GList *tail, GList glist) { if(glist == NULL || !glist->hp) { return ERROR; } if(!make_node(tail)) return ERROR; (*tail)->tag = LIST; (*tail)->tp = NULL; copy_glist(tail, glist->hp->tp); return OK; } int insert_first(GList glist, GList node) { if(!glist) return ERROR; GLNode *tmp = glist->hp; glist->hp = node; node->tp = tmp; return OK; } int delete_first(GList glist, GList *node) { if(!glist) return ERROR; if(glist->hp) { (*node) = glist->hp; glist->hp = (*node)->tp; (*node)->tp = NULL; } else { (*node) = glist; } return OK; } /*int print_glist(GList glist) { if(glist == NULL) return ERROR; GLNode *tmp = glist; if(tmp->tag == LIST && tmp->hp) { print_glist(tmp->hp); if(tmp->tp) print_glist(tmp->tp); } else { if(tmp->tag == ATOM) sstr_print(tmp->atom); if(tmp->tp) print_glist(tmp->tp); } return OK; }*/ int print_glist(GList glist) { GLNode *hp, *tp; if(glist) { if(glist->tag == LIST) { hp = glist->hp; } else { sstr_print(glist->atom); hp = NULL; } tp = glist->tp; print_glist(hp); print_glist(tp); } return OK; } SString.h #ifndef SSTRING_H_H #define OK 0 #define ERROR -1 #define MAX_LENGTH 255 typedef unsigned char SString[MAX_LENGTH + 1]; //第0个位置存放串的长度 int sstr_assign(SString, const char*); int sstr_print(SString); int sstr_copy(SString, SString); int sstr_empty(SString); int sstr_compare(SString, SString); int clear_sstring(SString); int sstr_strcat(SString, SString, SString); int sstr_subString(SString, SString, int, int); int sstr_index(SString, SString, int); int sstr_replace(SString, SString, SString); int sstr_insert(SString, SString, int); int sstr_delete(SString, int, int); int sstr_length(SString); int clear_sstring(SString); #endif SString.cpp #include<stdio.h> #include<stdlib.h> #include"SString.h" //产生一个SString类型的串,其值等于sstr int sstr_assign(SString T, const char *sstr) { int ix = 0; while(sstr[ix] != '/0') { T[ix + 1] = sstr[ix++]; //如果空间耗尽,则不再复制。 if(ix == MAX_LENGTH) break; } //保存串的长度 T[0] = ix; return OK; } int sstr_copy(SString T, SString S) { if(0 == S[0]) return ERROR; int len = S[0]; int ix = 1; while(len-- > 0) { T[ix] = S[ix++]; } T[0] = S[0]; return OK; } int sstr_empty(SString T) { if(0 == T[0]) return true; return false; } int sstr_subString(SString sub, SString T, int pos, int len) { if(0 == T[0] || pos < 1 || len > MAX_LENGTH || len + pos > T[0] + 1) return ERROR; int ix = 1; while(len-- > 0) { sub[ix++] = T[pos++]; if(ix > MAX_LENGTH) break; } sub[0] = ix - 1; return OK; } int sstr_compare(SString T, SString S) { if(0 == T[0] && 0 == S[0]) return ERROR; int ix = 1; int T_len = T[0]; int S_len = S[0]; while(T_len > 0 && S_len > 0) { if((int)T[ix] > (int)S[ix]) { return 1; } else if((int)T[ix] < (int)S[ix]) { return -1; } else { T_len--; S_len--; ix++; } } if(T_len == S_len) return 0; else if(T_len == 0) return -1; else return 1; return OK; } int clear_sstring(SString T) { if(T[0] == 0) return ERROR; T[0] = 0; return OK; } int sstr_strcat(SString T, SString S1, SString S2) { if(0 == S1[0] && 0 == S2[0]) { T[0] = 0; return ERROR; } int S1_len = S1[0]; int S2_len = S2[0]; int total_len = S1_len + S2_len; int ix = 1; int i = 1; while(ix <= total_len) { while(S1_len-- > 0) { T[ix++] = S1[i++]; if(ix > MAX_LENGTH) { T[0] = ix - 1; return OK; } } i = 1; while(S2_len-- > 0) { T[ix++] = S2[i++]; if(ix > MAX_LENGTH) { T[0] = ix - 1; return OK; } } } T[0] = ix - 1; return OK; } //返回从pos位置开始,返回第一次与T中的子串相同的首位置。 int sstr_index(SString S, SString T, int pos) { if(0 == S[0] || 0 == T[0] || pos > S[0]) return ERROR; SString sub; while(pos != S[0] + 1) { int len = T[0]; sstr_subString(sub,S,pos,len); if(0 == sstr_compare(sub,T)) return pos; pos++; } return OK; } int sstr_replace(SString S, SString T, SString V) { if(0 == S[0] || 0 == T[0] || 0 == V[0]) return ERROR; int ix = 1; int S_pos = 1; while(1) { int V_len = V[0]; int pos = sstr_index(S,T,S_pos); if(pos == -1 || 0 == pos) return OK; int s = 0; s = T[0] < V[0] ? V[0] - T[0] : T[0] - V[0]; //如果替换字符串跟被替换字符串的长度不一,就需要移位 if(T[0] - V[0] > 0) { while(V_len-- > 0) { S[pos++] = V[ix++]; } S_pos = pos; ix = 1; int times = S[0] - S_pos; while(times-- > 0) { S[pos] = S[pos + s]; pos++; } S[0] -= s; } else if(T[0] - V[0] < 0) { int tmp = S[0] - pos - T[0] + 1; int i = S[0]; while(tmp-- > 0) { if(S[0] + 1 > MAX_LENGTH) break; S[i + 1] = S[i--]; } while(V_len-- > 0) { S[pos++] = V[ix++]; } S_pos = pos; ix = 1; S[0] += s; } else { while(V_len-- > 0) { S[pos++] = V[ix++]; } S_pos = pos; ix = 1; } } return OK; } //在pos的位置插入T int sstr_insert(SString S, SString T, int pos) { if(0 == S[0] || 0 == T[0] || pos > S[0] + 1 || pos < 1) return ERROR; int T_len = T[0]; int p = S[0]; int ix = 1; int times = S[0] - pos + 1; while(times-- > 0) { if(p + 1 > MAX_LENGTH) return ERROR; S[p + T_len] = S[p--]; } S[0] += T_len; while(T_len-- > 0) S[pos++] = T[ix++]; return OK; } int sstr_delete(SString S, int pos, int len) { if(0 == S[0] || pos < 1 || pos + len > S[0] + 1 || pos > S[0] || len < 1 || len > S[0] - pos + 1) return ERROR; int times = S[0] - pos; S[0] -= len; while(times-- > 0) { S[pos] = S[pos + len]; pos++; } return OK; } int sstr_print(SString T) { int cnt = T[0]; int ix = 1; while(cnt-- > 0) { printf("%c",T[ix++]); } printf("/n"); return OK; } int sstr_length(SString S) { return S[0]; }