#include<iostream>
#include<stdlib.h>
#define MaxSize 50
typedef int ElemType;
typedef struct Node *Position;
typedef Position BTNode;
struct Node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}b;
void CreateBTree(BTNode * &b, char*str)
{
BTNode *St[MaxSize], *p;
int top = -1, k, j = 0;
char ch;
b = NULL;
ch = str[j];
while (ch != '\0')
{
switch (ch)
{
case '(':top++; St[top] = p; k = 1; break;
case')':top--; break;
case',':k = 2; break;
default:p = (BTNode *)malloc(sizeof(BTNode));
p->data = ch;
p->lchild = p->rchild = NULL;
if (b == NULL)
b = p;
else
{
switch (k)
{
case 1:St[top]->lchild = p; break;
case 2:St[top]->rchild = p; break;
}
}
}
j++;
ch = str[j];
}
}
其中p St b报错“表达式必须包含指向类的指针类型”
请问大佬们这是为什么呀?
typedef int ElemType;
typedef struct Node *Position;
typedef Position BTNode;
struct Node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}b;
粗略看了一下上面的应该等价于下面的
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}*Position, *BTNode, Node;
Node b;
这个
所以BTNode * p; 相当于Node **p,所以你就知道了(Node *)不是一个类,而是一个指针类型,你非要这么写的话可以试试(*p)->
或者 BTNode p,建议“}*Position, *BTNode, Node;”改为“}*Position, BTNode, Node;”
另外建议指针数组啥的该加的括号加上