-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.c
More file actions
77 lines (73 loc) · 1.01 KB
/
expr.c
File metadata and controls
77 lines (73 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
struct node
{
char data;
struct node *l;
struct node *r;
}*root;
void display()
{
struct node *p=stack[top];
while(p!=NULL)
{
printf("%d->",p->data);
}
}
struct node *stack[10];
int top=-1;
void push(char num)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->l=NULL;
temp->r=NULL;
if(top<10)
stack[++top]=temp;
//printf("push done");
}
struct node* pop()
{
struct node *p;
p=stack[top--];
// printf("pop done");
return p;
}
void createnode(char oper)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=oper;
temp->l=pop();
temp->r=pop();
stack[++top]=temp;
}
void inorder(struct node *p)
{
if(p==NULL)
return ;
else
{
inorder(p->l);
printf("%c ",p->data);
inorder(p->r);
}
}
int main()
{
int i=0;
char expr[20];
scanf("%s",expr);
for(i=0;expr[i]!='\0';i++)
{
if(isalnum(expr[i]))
{
push(expr[i]);
}
else
{
createnode(expr[i]);
}
inorder(stack[top]);
}
}