广义表的创建,遍历,求深度

#include <stdio.h>
typedef char AtomType;
typedef enum {ATOM, LIST} ElemTag; /* ATOM=0,表示原子;LIST=1,表示子表*/
typedef struct GLNode
{ElemTag tag; /*标志位tag用来区别原子结点和表结点*/
union
{
    AtomType atom; /*原子结点的值域atom*/
    struct { struct GLNode * hp, *tp;} htp; /*表结点的指针域htp, 包括
                                            表头指针域hp和表尾指针域tp*/
   }atom_htp; /* atom_htp 是原子结点的值域atom和表结点的指针域htp的联合体域*/
}*GList;
/**********用于遍历************/
struct{
GList find[100];
int posint;
}Find;
static int num = 0;
void GList_View(GList);/*遍历*/
/**********用于遍历结束************/
/**********用于创建************/
typedef struct{
char near[100];
GList p;
}Near;
typedef struct next{
   Near lv;
   struct next *ne;
}Next;
Next *strN;
char Lin[10];
GList getGeneralized_list(GList,char*);/*创建*/
/**********用于创建结束************/
int GetDepth(GList);
main(){
/*创建初始化*/
char strList[100]="(((a,b,(h),c),d),e,((f),g))";
GList Glnode;
Glnode = (GList)malloc(sizeof(struct GLNode));
    Glnode->tag = LIST;
    Glnode->atom_htp.htp.hp = NULL;
    Glnode->atom_htp.htp.tp = NULL;
   
    strN = (Next*)malloc(sizeof(Next));
    strN->ne = NULL;
    getGeneralized_list(Glnode,strList);
/*遍历初始化*/
    Find.posint = -1;
GList_View(Glnode);
printf("广义表深度为:%d/n",GetDepth(Glnode));
    getch();
}
GList getGeneralized_list(GList Pre,char *p)
{
    GList Glnode,Glnode1;
    Next *LV;
    int i,j,isNUM=0;
    char *lik,*head = p+1;
   for(i=0;*p;p++)
   {
   if(*p=='('){
       Lin[i]='(';
       i++;
       Lin[i]='/0';
   }
   if(*p==')')
   {
   i--;
   Lin[i]='/0';
   }
   if(*p==',')
   {
   if(i==1 && Lin[0]=='(')
   {
     
      isNUM = 1;
            Glnode = (GList)malloc(sizeof(struct GLNode));
            Glnode->tag = LIST;
            Glnode->atom_htp.htp.hp = NULL;
            Glnode->atom_htp.htp.tp = NULL;
      lik=p+1;
      LV = (Next*)malloc(sizeof(Next));
            LV->ne = strN;
    strN = LV;
    strN->lv.near[0]='(';
    for(j=1;*lik;lik++,j++)
    strN->lv.near[j]=*lik;
    strN->lv.near[j]='/0';
    strN->lv.p = Glnode;
    Pre->atom_htp.htp.tp = Glnode;
    printf("找到 %c,%c   这时预留:%s尾:%x",*(p-1),*(p+1),strN->lv.near,Pre->atom_htp.htp.tp);
    if(*head != '(')
    {
     Glnode1=(GList)malloc(sizeof(struct GLNode));
              Glnode1->tag = ATOM;
           Glnode1->atom_htp.atom = *head;
       Pre->atom_htp.htp.hp = Glnode1;
       LV = strN;
       strN = LV->ne;
       printf(" 头:%c,next:%s/n",*head,LV->lv.near);
     getGeneralized_list(LV->lv.p,LV->lv.near);
    
    }
    else
    {
     Glnode1=(GList)malloc(sizeof(struct GLNode));
              Glnode1->tag = LIST;
           Glnode1->atom_htp.htp.hp = NULL;
              Glnode1->atom_htp.htp.tp = NULL;
       Pre->atom_htp.htp.hp = Glnode1;
     *p='/0';
     p=head;
     printf(" 头:%x,next:%s/n",Glnode1,p);
     getGeneralized_list(Glnode1,p);
    
    }
   }
   }
  
   }
  
   if(*p=='/0' && isNUM==0)
   {
   if(*head=='(')
   {
   Glnode1=(GList)malloc(sizeof(struct GLNode));
              Glnode1->tag = LIST;
           Glnode1->atom_htp.htp.hp = NULL;
              Glnode1->atom_htp.htp.tp = NULL;
       Pre->atom_htp.htp.hp = Glnode1;
       *(p-1)='/0';
     p=head;
     printf("头:%x,next:%s/n",Glnode1,p);
     getGeneralized_list(Glnode1,p);
   }else{
   Glnode1=(GList)malloc(sizeof(struct GLNode));
              Glnode1->tag = ATOM;
           Glnode1->atom_htp.atom = *head;
       Pre->atom_htp.htp.hp = Glnode1;
    if(strN->ne!=NULL)
     {
     LV = strN;
       strN = LV->ne;
       printf("头:%c,next:%s/n",*head,LV->lv.near);
     getGeneralized_list(LV->lv.p,LV->lv.near);
     }else{
     return;
     }
   }
   }
  
}
/*打印广义表*/   
void GList_View(GList p)
{
if(p->tag)
{
       if(p->atom_htp.htp.tp)
    {
    Find.posint++;
       Find.find[Find.posint]=p->atom_htp.htp.tp;
    }
    num++;
    GList_View(p->atom_htp.htp.hp);
}else{
   printf("%c(%d)   这个时候%c出来num是:%d Find.posint是%d/n",p->atom_htp.atom,num,p->atom_htp.atom,Find.posint+1,Find.posint-1);
   num=0;
   if(Find.posint >= 0)
   {
     Find.posint--;
     GList_View(Find.find[Find.posint+1]);
   }
}
}
/*求广义表深度*/
int GetDepth(GList p)
{  
    int max,lmax=0;
if(p->tag==ATOM)
{
   return 0;
}
if(p==NULL){
return 1;
}
for(;p;p=p->atom_htp.htp.tp)
{
max = GetDepth(p->atom_htp.htp.hp);
    if(max>lmax)lmax=max;
}
return lmax+1;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值