广义表输入
#include<bits/stdc++.h>
using namespace std;
typedef char ElementType;
typedef struct Node
{
ElementType data;
struct Node *lchild;
struct Node *rchild;
}BTNode,*BTree;
BTree createTree(char s[]);
int main()
{
char a[1010]={};
gets(a);
createTree(a);
return 0;
}
/*你的提交的代码将被添加在此处,请完成题目所要求的函数的定义*/
BTree createTree(char s[]) {
BTNode *p;
BTNode *rt;
stack <BTree> S;
p = (BTNode * )malloc(sizeof(BTNode));
p->lchild = NULL;
p->rchild = NULL;
p->data = s[0];
S.push(p);
rt = p;
for(int i = 1; s[i] != '\0'; i++) {
if('A'<=s[i] && s[i] <= 'Z') {
p->data = s[i];
}
if(s[i] == '(') {
p = p->lchild;
p->lchild = NULL;
p->rchild = NULL;
S.push(p);
}
if(s[i] == ',') {
S.pop();
p = S.top()->rchild;
p->lchild = NULL;
p->rchild = NULL;
S.push(p);
}
if(s[i] == ')') {
S.pop();
}
}
return rt;
}
创建二叉树卡了我好几天了,求求大佬帮帮忙