c语言-广义表

#pragma once //防止重复编译

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define AtomType int

typedef enum
{
    HEAD,
    ATOM,
    CHILDLIST
} ElemTag;

typedef struct GLNode
{
    ElemTag tag; //标记 HEAD,ATOM,CHILDLIST
    union
    {
        AtomType atom; //元组数据
        struct GLNode *hp;
    };
    struct GLNode *tp;
} GLNode;

typedef GLNode *GENList;

void InitGenList(GENList &gl);
void GreateGenList(GENList &gl, char *str);
bool sever(char *sub, char *hsub);
void ShowGenList(GENList &gl);
bool GenListEmpty(GENList &gl);

int  GenListLength(GENList &gl);
int GenListDepth(GENList &gl);
#include "GenList.h"

void InitGenList(GENList &gl)
{
    gl = NULL;
}

void GreateGenList(GENList &gl, char *str)
{
    /*
    char *ga="(1,2,3)";
    char *gb="(1,(2,3))";
    char *gc="(1,(2),3)";
    char *gd="((1,2),3)";
    char *ge="((1,2,3))";
    char *gf="()";
    char *gg="(1,(1,(3,4)),5)"
    */
    int n = strlen(str);
    char *sub = (char *)malloc(sizeof(char) * (n - 2));
    //表头
    char *hsub = (char *)malloc(sizeof(char) * (n - 2));
    assert(sub != NULL && hsub != NULL);
    //"1,2,3"
    strncpy(sub, str + 1, n - 2); //拷贝时去掉括号
    sub[n - 2] = '\0';
    //头结点
    if (gl== NULL)
    {
        gl = (GLNode *)malloc(sizeof(GLNode));
        assert(gl != NULL);

        gl->tag = HEAD;
        gl->hp = gl->tp = NULL;
    }

    GLNode *p = gl;
    while (strlen(sub) != 0)
    {
        p = p->tp = (GLNode *)malloc(sizeof(GLNode));
        assert(p != NULL);
        p->hp = p->tp = NULL;

        //"1,2,3" ==>hsub="1" ,sub="2,2"
        //"(1,2),3,4"==>hsub="(1,2)",sub="3,4"  嵌套调用
        if (sever(sub, hsub))
        {
            if (hsub[0] == '(')
            {
                p->tag = CHILDLIST;
                GreateGenList(p->hp, hsub);
            }
            else
            {
                p->tag = ATOM;
                p->atom = atoi(hsub);
            }
        } 
    }
}

bool sever(char *sub, char *hsub)
{
    // " " 和 "()"
    if (*sub == '\0' || strcmp(sub, "()") == 0)
    {
        hsub['0'] = '\0';
        return true;
    }
    //"1,2,3"
    int n = strlen(sub);
    int i = 0;
    char ch = sub[0];
    int k = 0;
    while (i < n && (ch != ',' || k != 0))
    {
        if (ch == '(')
            k++;
        else if (ch == ')')
            k--;
        i++;
        ch = sub[i];
    }
    if (i < n)
    {
        sub[i] = '\0';
        strcpy(hsub, sub);
        ;                         //赋头结点
        strcpy(sub, sub + i + 1); //赋尾结点
        
    }
    //匹配失败
    else if (k != 0)
        return false;
    //"(1,2)"
    else
    {
        strcpy(hsub, sub);
        sub[0] = '\0';
    }
    return true;
}

void ShowGenList(GENList &gl)
{
    GLNode *p = gl->tp;
    printf("(");
    while (p != NULL)
    {
        if (p->tag == ATOM)
        {
            printf("%d", p->atom);
            if (p->tp != NULL)
                printf(",");
            p=p->tp;
        }
        else if (p->tag == CHILDLIST)
        {
            ShowGenList(p->hp);
            p = p->tp;
        }
    }
    printf("),");
}

bool GenListEmpty(GENList &gl){
    return gl->tp==NULL;
}

int  GenListLength(GENList &gl){
    int length=0;
    GLNode *p=gl->tp;
    while(p!=NULL){
        length++;
        p=p->tp;
    }
    return length;
}
int GenListDepth(GENList &gl){
    if(gl->tp==NULL)
        return 1;
    GLNode *p=gl->tp;
    int maxdepth=0;
    int dep;
    while(p!=NULL){
        if(p->tag==CHILDLIST){
            dep=GenListDepth(p->hp->tp);
            if(dep>maxdepth)
                maxdepth=dep;
        }
        p=p->tp;
    }
    return maxdepth+1;
}
#include "GenList.h"

void main()
{
    GENList gl;
    InitGenList(gl);

    char *ga = "(1,2,3)";
    char *gb = "(1,(2,3))";
    char *gc = "(1,(2),3)";
    char *gd = "((1,2),3)";
    char *ge = "((1,2,3))";
    char *gf = "()";
    char *gg = "(1,(1,(3,4)),5)";
    GreateGenList(gl,gg);
    ShowGenList(gl);
    int length=GenListDepth(gl);
    printf("%d",length);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值