广义表逆置

/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */


#include "stdio.h"
#include "string.h"
#include "stdlib.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 * T)
{
GLNode *p = T, *q;


if (p == NULL)
printf(")");
else
{
if (p->tag == ATOM)
{
if (count > 0)
printf(",");
printf("%c", p->ptr.data);
count++;
}
else
{
q = p->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(p->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 *q;
GLNode *h;
GLNode *t;
GLNode *s;


if (p == NULL)
q = NULL;
else if (p->tag == ATOM)
{


q = (GLNode *)malloc(sizeof(GLNode));
q->tag == ATOM;
q->ptr.data = p->ptr.data;
}
else
{
h = reverse(p->ptr.hp);
if (p->tp)
{
t = reverse(p->tp);
s = t;
while (s->tp != NULL)
s = s->tp;
s->tp= (GLNode *)malloc(sizeof(GLNode));
s = s->tp;
s->tag = LIST;
s->tp = NULL;
s->ptr.hp = h;
s->tp = t;


}
else
{
q= (GLNode *)malloc(sizeof(GLNode));
q->tag = LIST;
q->tp = NULL;
q->ptr.hp = h;


}
}
return (q);


}




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


/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值