广义表的创建及求其深度

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum{
atom,list
}ElemTag;
struct node{
ElemTag  tag;
union{
char s;
struct node *head;
};
struct node *tail;
};
char a[100];
int len,time;
int max=0;
struct node* dfs(int step)
{
struct node*t;
if(step>max)
{
max=step;
}
t=(struct node*)malloc(sizeof(struct node));
if(time==len-1)
{
return NULL;
}
if(a[time]=='(') //当遇到“(”时,便创建一个表头,然后去寻找它的头指针和尾指针指向的地方
  {
t->tag=list;
time++;
while(a[time]==',')
{
time++;
}
t->head=dfs(step+1);
time++;
while(a[time]==',')
{
time++;
}
if(a[time]==')')    //无论是表头还是原子,只要遇到“)”便指向NULL
{
t->tail=NULL;
//time++;
//while(a[time]==',')
//{
// time++;
//}
return t;
}
if(a[time]>='a'&&a[time]<='z')
{
t->tail=dfs(step);
//time++;
//while(a[time]==',')
//{
// time++;
   // }
return t;
}
if(a[time]=='(')   //如果表头的尾指针是"(",则他的尾指针指向的地方是表头
{
t->tail=dfs(step);
}
}
if(a[time]>='a'&&a[time]<='z') //这种情况他的尾指针指向的位置是一个原子
{
t->tag=atom;
time++;
   while(a[time]==',')
   {
      time++;
   }
   t->tail=dfs(step);
   return t;
}
if(a[time]==')')
{
return NULL;
}
}


int main()
{
struct node *head1;
gets(a);
head1=(struct node*)malloc(sizeof(struct node));
len=strlen(a);
head1->tag=list; //第一个位置肯定是表头,且该表头尾部肯定为空
time++;
while(a[time]==',')
{
time++;
}
    head1->head=dfs(1);
    head1->tail=NULL;
printf("%d\n%d",max,max);
return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值