用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

广义表操作算法(广义表初始化、长度、深度、建立、输出)

2012-09-23 作者: 神马举报

[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;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...