7. 广义表反序(中文班,10分)

逆置广义表的递归模型如下:
F(LS) = null              若 LS 为空
F(LS) = LS              若 LS 为原子,且 tail(LS) 为空
F(LS) = append( F(tail(LS)), head(LS) )  若 LS->tag=0 ,且 LS->tp!=null
F(LS) = append( F(tail(LS), F(head(LS)) )  若 LS->tag=1

其中 append(a,b) 的功能是将广义表 a 和 b 作为元素的广义表连接起来。

请根据以上定义和给定的程序框架,编写函数:GLNode * reverse( GLNode * )。

特别说明:以下的预设代码并不是一个好的程序,大家先凑合吧。

预设代码

前置代码

view plainprint?

  1. /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */  
  2.  
  3. #include "stdio.h"  
  4. #include "string.h"  
  5. #include "stdlib.h"  
  6.   
  7. typedef enum { ATOM, LIST } ListTag;  
  8.   
  9. typedef struct node {  
  10.     ListTag  tag;  
  11.     union {  
  12.         char  data;  
  13.         struct node *hp;  
  14.     } ptr;  
  15.     struct node *tp;  
  16. } GLNode;  
  17.   
  18. GLNode * reverse( GLNode * );  
  19.   
  20. int count;  
  21.   
  22. void Substring( char *sub, char *s, int pos, int len )  
  23. {  
  24.     s = s + pos;  
  25.     while ( len > 0 )  
  26.     {   *sub = *s;  
  27.         sub++;  
  28.         s++;  
  29.         len--;  
  30.     }  
  31.     *sub = '\0';  
  32. }  
  33.   
  34. void sever( char *str, char *hstr )  
  35. {   int n, i, k;  
  36.     char ch[50];  
  37.     n = strlen(str);  
  38.     i = k = 0;  
  39.     do  
  40.     {   Substring( ch, str, i++, 1 );  
  41.         if ( *ch=='(' )  
  42.             k ++;  
  43.         else if ( *ch==')' )  
  44.             k --;  
  45.     } while ( i<n && ( *ch!=',' || k!=0 ) );  
  46.   
  47.     if ( i<n )  
  48.     {   Substring( hstr, str, 0, i-1 );  
  49.         Substring( str, str, i, n-i );  
  50.     }  
  51.     else  
  52.     {   strcpy( hstr, str );  
  53.         str[0] = '\0';  
  54.     }  
  55. }  /* sever */  
  56.   
  57. int PrintGList( GLNode * T )  
  58. {  
  59.     GLNode *p=T, *q;  
  60.   
  61.     if ( p==NULL )  
  62.         printf( ")" );  
  63.     else  
  64.     {   if ( p->tag==ATOM )  
  65.         {   if ( count > 0 )  
  66.                 printf( "," );  
  67.             printf( "%c", p->ptr.data );  
  68.             count ++;  
  69.         }  
  70.         else  
  71.         {   q = p->ptr.hp;  
  72.             if ( q == NULL )  
  73.             {   if ( count > 0 )  
  74.                     printf(",");  
  75.                 printf("(");  
  76.             }  
  77.             else if ( q->tag == LIST )  
  78.             {   if ( count > 0 )  
  79.                     printf( "," );  
  80.                 printf( "(" );  
  81.                 count = 0;  
  82.             }  
  83.             PrintGList( q );  
  84.             PrintGList( p->tp );  
  85.         }  
  86.     }  
  87.     return 1;  
  88. }  
  89.   
  90. void print( GLNode * L )  
  91. {  
  92.     if ( L == NULL )  
  93.         printf( "()" );  
  94.     else  
  95.     {  
  96.         if ( L->tag == LIST )  
  97.             printf( "(" );  
  98.         if ( L->ptr.hp != NULL )  
  99.             PrintGList( L );  
  100.         else  
  101.         {  
  102.             printf( "()" );  
  103.             if ( L->tp == NULL )  
  104.                 printf( ")" );  
  105.         }  
  106.     }  
  107.     printf( "\n" );  
  108. }  
  109.   
  110. int CreateGList( GLNode **L,  char *s )  
  111. {  
  112.     GLNode *p, *q;  
  113.     char sub[100],  hsub[100];  
  114.   
  115.     p = *L;  
  116.     if ( strcmp(s, "()" )==0 )  
  117.         *L = NULL;    /* 创建空表 */  
  118.     else  
  119.     {  
  120.         *L = ( GLNode * ) malloc( sizeof( GLNode ) );  
  121.         if ( strlen(s)==1 )  
  122.         {   (*L)->tag = ATOM;  
  123.             (*L)->ptr.data = s[0];  
  124.         }  
  125.         else  
  126.         {   (*L)->tag = LIST;  
  127.             p = *L;  
  128.             Substring( sub, s, 1, strlen(s)-2 );  
  129.             do  
  130.             {   sever( sub, hsub );  
  131.                 CreateGList( &p->ptr.hp, hsub );  
  132.                 q = p;  
  133.                 if ( strlen(sub) > 0 )  
  134.                 {   p = (GLNode *) malloc( sizeof(GLNode) );  
  135.                     p->tag = LIST;  
  136.                     q->tp = p;  
  137.                 }  
  138.             } while ( strlen(sub)>0 );  
  139.             q->tp = NULL;  
  140.         }   /* else */  
  141.     }  /* else */  
  142.     return 1;  
  143. }  
  144.   
  145. /********** 
  146. 这是你要实现的函数。 
  147. ***********/  
  148. GLNode * reverse( GLNode *p );  
  149. /*******************/  
  150.   
  151. int main( )  
  152. {  
  153.     char list[100];  
  154.     GLNode *L, *G;  
  155.     int d;  
  156.   
  157.     count = 0;  
  158.     scanf("%s", list);  
  159.     CreateGList( &L, list );  
  160.   
  161. /*  print( L );   */  
  162.     G = reverse( L );  
  163.     count = 0;  
  164.     print( G );  
  165.     return 0;  
  166. }  
  167.   
  168. /* PRESET CODE END - NEVER TOUCH CODE ABOVE */  
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef enum
{
    ATOM,
    LIST
} ListTag;

typedef struct node
{
    ListTag tag;
    union {
        char data;
        struct node *hp;
    } ptr;
    struct node *tp;
} GLNode;

GLNode *reverse(GLNode *);

int count;

void Substring(char *sub, char *s, int pos, int len)
{
    s = s + pos;
    while (len > 0)
    {
        *sub = *s;
        sub++;
        s++;
        len--;
    }
    *sub = '\0';
}

void sever(char *str, char *hstr)
{
    int n, i, k;
    char ch[50];
    n = strlen(str);
    i = k = 0;
    do
    {
        Substring(ch, str, i++, 1);
        if (*ch == '(')
            k++;
        else if (*ch == ')')
            k--;
    } while (i < n && (*ch != ',' || k != 0));

    if (i < n)
    {
        Substring(hstr, str, 0, i - 1);
        Substring(str, str, i, n - i);
    }
    else
    {
        strcpy(hstr, str);
        str[0] = '\0';
    }
} /* sever */

int PrintGList(GLNode *p)
{
    GLNode *FL = p, *q;

    if (FL == NULL)
        printf(")");
    else
    {
        if (FL->tag == ATOM)
        {
            if (count > 0)
                printf(",");
            printf("%c", FL->ptr.data);
            count++;
        }
        else
        {
            q = FL->ptr.hp;
            if (q == NULL)
            {
                if (count > 0)
                    printf(",");
                printf("(");
            }
            else if (q->tag == LIST)
            {
                if (count > 0)
                    printf(",");
                printf("(");
                count = 0;
            }
            PrintGList(q);
            PrintGList(FL->tp);
        }
    }
    return 1;
}

void print(GLNode *L)
{
    if (L == NULL)
        printf("()");
    else
    {
        if (L->tag == LIST)
            printf("(");
        if (L->ptr.hp != NULL)
            PrintGList(L);
        else
        {
            printf("()");
            if (L->tp == NULL)
                printf(")");
        }
    }
    printf("\n");
}

int CreateGList(GLNode **L, char *s)
{
    GLNode *p, *q;
    char sub[100], hsub[100];

    p = *L;
    if (strcmp(s, "()") == 0)
        *L = NULL; /* 创建空表 */
    else
    {
        *L = (GLNode *)malloc(sizeof(GLNode));
        if (strlen(s) == 1)
        {
            (*L)->tag = ATOM;
            (*L)->ptr.data = s[0];
        }
        else
        {
            (*L)->tag = LIST;
            p = *L;
            Substring(sub, s, 1, strlen(s) - 2);
            do
            {
                sever(sub, hsub);
                CreateGList(&p->ptr.hp, hsub);
                q = p;
                if (strlen(sub) > 0)
                {
                    p = (GLNode *)malloc(sizeof(GLNode));
                    p->tag = LIST;
                    q->tp = p;
                }
            } while (strlen(sub) > 0);
            q->tp = NULL;
        } /* else */
    }     /* else */
    return 1;
}

GLNode *reverse(GLNode *p)
{
    GLNode *FL = p;
    if (p != NULL && p->tag == LIST)
    {
        for (p->tp = reverse(p->tp); p->tp != NULL; p = p->tp)
            ;
        p->tp = (GLNode *)malloc(sizeof(GLNode));
        GLNode *q = p->tp;
        q->tp = NULL;
        q->tag = FL->tag;
        q->ptr.hp = reverse(FL->ptr.hp);
        p->tp = q;
        FL = FL->tp;
    }
    //如果是ATOM就直接返回该值
    else return FL;
    return FL;
}

int main()
{
    char list[100];
    GLNode *L, *G;
    int d;

    count = 0;
    scanf("%s", list);
    CreateGList(&L, list);

    /*  print( L );   */
    G = reverse(L);
    count = 0;
    print(G);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值