正在学习数据结构广义表
自己按书上摸索写了几个函数,请大家给点意见
#ifndef _GLIST_H#define _GLIST_H#include <stdlib.h>#include <stdarg.h>typedef int AtomType;typedef enum { ATOM, LIST } ElemTag;typedef struct glnode{ ElemTag tag; union { AtomType atom; struct { struct glnode *head,*tail; } ptr; };} GLIST,*GLP;GLP MakeGNode(ElemTag tag,...);int CreateGList(GLP gp,const char *table);int GetGListDepth(const GLP gp);int CopyGList(const GLP gsrc,GLP gdst);int GListTraverse(const GLP gp,void visit(const GLP));int ExportGList(const GLP gp,char *cp);#endif//_GLIST_H#include <stdio.h>#include <stdlib.h>#include <string.h>#include "glist.h"void visit(const GLP gp);void ExportGLP(GLP gp,char *cp,int from);int main(){ char *cp="((),((2,4),(5,3,(1,2))),(1),((2,(3,4,5))),(2,(3,4,5)))"; puts(cp); GLIST g1,g2; CreateGList(&g1,cp); GListTraverse(&g1,visit); CopyGList(&g1,&g2); char ch[100]; memset(ch,0,100); ExportGList(&g2,ch); puts(ch); return 0;}GLP MakeGNode(ElemTag tag,...){ GLP gp=(GLP)malloc(sizeof(GLIST)); if (!gp) { printf("not enough memory\n"); exit(2); } gp->tag=tag; va_list ap; va_start(ap,tag); if (tag==ATOM) gp->atom=va_arg(ap,AtomType); va_end(ap); return gp;}int CreateGList(GLP gp,const char *cp){ if (strchr(cp,'(')==NULL)//原子 { gp->tag=ATOM; gp->atom=atoi(cp); return 0; } gp->tag=LIST; char ht[100],tt[100]; GetFirstTable(cp,ht,tt); if (strcmp(ht,"()")!=0) { gp->ptr.head=(GLP)malloc(sizeof(GLIST)); CreateGList(gp->ptr.head,ht); } else gp->ptr.head=NULL; if (strcmp(tt,"()")!=0) { gp->ptr.tail=(GLP)malloc(sizeof(GLIST)); CreateGList(gp->ptr.tail,tt); } else gp->ptr.tail=NULL;}int GetFirstTable(const char *cp,char *ht,char *tt){ int pt,pos,len=strlen(cp); if (cp[1]=='(') { pt=1; pos=2; while (pt!=0) { if (cp[pos]=='(') pt++; if (cp[pos]==')') pt--; pos++; } } else { pos=1; while (cp[pos]!=',' && pos<len-1) pos++; } strncpy(ht,cp+1,pos-1); ht[pos-1]='\0'; tt[0]='('; strncpy(tt+1,cp+pos+1,len); if (tt[1]=='\0') tt[1]=')'; return 0;}int CopyGList(const GLP gsrc,GLP gp){ gp->tag=gsrc->tag; if (gsrc->tag==ATOM) { gp->atom=gsrc->atom; return 0; } if (gsrc->ptr.head!=NULL) { gp->ptr.head=(GLP)malloc(sizeof(GLIST)); CopyGList(gsrc->ptr.head,gp->ptr.head); } else gp->ptr.head=NULL; if (gsrc->ptr.tail!=NULL) { gp->ptr.tail=(GLP)malloc(sizeof(GLIST)); CopyGList(gsrc->ptr.tail,gp->ptr.tail); } else gp->ptr.tail=NULL;}int GListTraverse(const GLP gp,void visit(const GLP gpp)){ visit(gp); if (gp) { if (gp->tag==LIST) { printf("head:"); GListTraverse(gp->ptr.head,visit); printf("tail:"); GListTraverse(gp->ptr.tail,visit); } }}void visit(const GLP gp){ if (!gp) { printf("NULL\n"); return; } if (gp->tag==LIST) { printf("LIST->"); return; } printf("ATOM(%d)\n",gp->atom);}int ExportGList(const GLP gp,char *cp){ ExportGLP(gp,cp,0); return 0;}void ExportGLP(GLP gp,char *cp,int from){ static pos=0; if (!gp) { if (from==0) { cp[pos++]='('; cp[pos++]=')'; } return; } if (from==0) { if (gp->tag==LIST) { cp[pos++]='('; ExportGLP(gp->ptr.head,cp,0); if (gp->ptr.tail==NULL) cp[pos++]=')'; else cp[pos++]=','; ExportGLP(gp->ptr.tail,cp,1); } else { cp[pos++]=gp->atom+'0'; return; } } else { ExportGLP(gp->ptr.head,cp,0); if (gp->ptr.tail==NULL) cp[pos++]=')'; else cp[pos++]=','; ExportGLP(gp->ptr.tail,cp,1); }}