c/c++实现广义表及其深度查找

c/c++实现广义表及其深度查找,深度查找使用递归即可。


#inlcude
int dep=0;


typedef struct GeneralNode{
int tag;
union{
Datatype data;
struct{
struct GeneralNode *hp,*tp;
}ptr;
};
}*GList;


GList GetHead(GList p)
{
if(!p)
{
printf("empty GList");
return NULL;
}
return (p->ptr.hp)
}


GList GetTail(GList p)
{
if(!p||!p->tag)
{
printf("empty GList or just an unit");
return NULL;
}
return (p->ptr.hp)
}


int depth(GList ls)
{
int max;
GList tmp=ls;
if(tmp=NULL)
 return 1;
if(tmp->tag==0)
 return 0
while(tmp!=NULL)
{
dep=depth(tmp->ptr.hp);//use recursion to get the depth of the sublist
if(dep>max)max=dep;
tmp=tmp->ptr.tp; 
}
return (max+1);
}

你可能感兴趣的:(C/C++)