[c++]代码库
#include<iostream.h>
#include<stdlib.h>
typedef char elemtype;
struct glnode
{
int tag;
union
{
elemtype data;
glnode *sublist;
};
glnode *next;
};
int lenth ( glnode *gl )
{
if ( gl!=NULL )
return 1+lenth ( gl->next );
else
return 0;
}
int depth ( glnode *gl )
{
int max=0;
while ( gl!=NULL )
{
if ( gl->tag==1 )
{
int dep=depth ( gl->sublist );
if ( dep>max )
max=dep;
}
gl=gl->next;
}
return max+1;
}
void create ( glnode *&gl )
{
char ch;
cin>>ch;
if ( ch=='#' )
gl=NULL;
else if ( ch=='(' )
{
gl=new glnode;
gl->tag=1;
create ( gl->sublist );
}
else
{
gl=new glnode;
gl->tag=0;
gl->data=ch;
}
cin>>ch;
if ( gl==NULL )
;
else if ( ch==',' )
create ( gl->next );
else if ( ( ch==')' ) || ( ch==';' ) )
gl->next=NULL;
}
void print ( glnode *&gl )
{
if ( gl->tag==1 )
{
cout<<'(';
if ( gl->sublist==NULL )
cout<<'#';
else
print ( gl->sublist );
cout<<')';
}
else
cout<<gl->data;
if ( gl->next!=NULL )
{
cout<<',';
print ( gl->next );
}
}
void main( )
{
glnode *g;
create ( g );
print ( g );
cout<<endl;
cout<<"gyb length: "
<<lenth ( g->sublist ) <<endl;
cout<<"gyb depth: "
<<depth ( g->sublist ) <<endl;
}
by: 发表于:2018-01-25 09:40:24 顶(0) | 踩(0) 回复
??
回复评论