使用双指针创建树
Creating trees using double pointer
我正在尝试创建一个树。基本思想是,每当我看到"+"、"-"、"/"或"*"时,该变量就会成为主树的父级。例如,如果用户输入1+1,树应该看起来像
+
1 1
请看一下我的插入函数。我已经写了一个sudo代码,但我不知道如何编码。我对双指针感到不舒服。如果可以的话,请一行一行地解释,这样我就可以跟上了。感谢
这是我的代码
%{
#include <stdio.h>
#define true 1
#define false 0
int lookahead;
int lookahead1;
int error;
int count;
%}
// LEX codes
%%
[(] {return(111);}
[)] {return(222);}
[0-9] {return(333);}
[a-zA-Z] {return(444);}
[+] {return(33);}
[*] {return(44);}
[/] {return(55);}
[-] {return(66);}
[ nt] {return(0);}
%%
// C code
struct node
{
char a;
struct node * left;
struct node * right;
};
struct node *root, * current;
// inserting into tree
void insert( struct node ** tree, struct node *item)
{
printf("%c", item -> a);
if (!(*tree))
{
*tree = item;
return;
}
// this is where I need help
if ( item-> a == '+' || item-> a == '-' || item->a == '*' || item->a == '/' )
{
item -> left = tree;
break;
/* struct node ** temp;
temp= tree;
*tree = item;
(*tree) -> left = *temp;
*/
//insert (&(*tree) -> left, item);
}
else if ( (*tree) -> left == NULL )
{
insert (&(*tree) -> left, item);
}
else
{
insert (&(*tree) -> right, item);
}
}
// c code ends here
// lex code
Goal ()
{
return Expr();
}
Expr()
{
if ( Term() )
return ExprP();
else
return false;
}
ExprP()
{
if ( lookahead == 33 || lookahead == 66 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
if ( Term() )
return ExprP();
else
return false;
}
else if ( lookahead == 222 || lookahead == 0 )
return true;
else
return false;
}
Term()
{
if (Factor())
{
return TermP();
}
else
return false;
}
TermP()
{
if ( lookahead == 44 || lookahead == 55 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
if ( Factor() )
{
return TermP();
}
else
return false;
}
else if ( lookahead == 33 || lookahead == 222 || lookahead == 0 || lookahead == 66 )
return true;
else
return false;
}
Factor()
{
if (lookahead == 111 )
{
lookahead = yylex();
if ( !Expr() )
return false;
if ( lookahead != 222 )
return false;
lookahead = yylex();
return true;
}
else if ( lookahead == 333 )
{
current = malloc(sizeof(struct node));
current -> a = yytext[0];
current -> left = NULL;
current -> right = NULL;
insert(&root, current);
lookahead = yylex();
return true;
}
else
return false;
}
int yywrap(void)
{
return 1;
}
void printout(struct node * tree, int h) {
if(tree->right) printout(tree->right, h+1);
int i ;
for ( i = 0; i < h ; i++)
printf(" ");
printf("%cn",tree->a);
if(tree->left) printout(tree->left, h+1);
}
// maing in c code
int main()
{
root = NULL;
lookahead = yylex();
if ( Goal() )
printf ("nnThe string you entered successfully acceptednn");
else
{
printf("nnThe string you entered failed to acceptnn");
printf("%sn", yytext);
}
printout(root, 0);
return 0;
}
以下代码不正确:
item -> left = tree
也就是说,为CCD_ 1分配作为CCD_ 2的值。应该是:
item -> left = *tree
您正在传递**tree
,以便可以修改树参数的值,但它的所有用途都需要取消引用,这样您就只有一个指针。
相关文章:
- 如何创建指针链?我想创建具有指向更多对象的指针的对象,该对象具有指针.这可能吗?
- c++ 获取非语法使用"&"创建指针
- 在自定义构造函数中的堆上创建指针
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 创建指针会占用大量资源吗?
- 创建指针时是否没有构造函数调用
- 以不寻常的方式创建指针
- 在类中创建指针到另一个对象
- Visual Studio 2015"非标准语法;使用"&"为成员创建指针"
- 创建指针 C++ 时获取段错误
- 当我在 c++ 中创建指针数组时无法读取的内存
- 创建指针列表的正确范例是什么
- 如何在C 11中创建指针映射以静态成员功能
- 奇怪的错误:当没有真正创建指针时,使用已删除的函数'std::unique_ptr<_Tp,_Dp>::unique_ptr
- 如何使用双指针创建指针数组?
- 创建指针数组
- C++:创建指针的副本
- 使用new创建指针数组,这样指针就不会初始化为NULL
- 如何在不从参数列表中创建对象的情况下创建指针
- 如何在定义后创建指针和结构实例