-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22_InfixToPostFix.c
More file actions
116 lines (104 loc) · 2.31 KB
/
22_InfixToPostFix.c
File metadata and controls
116 lines (104 loc) · 2.31 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
22
Infix to Postfix Conversion
Name: Sayooj K
Roll no: 45
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char value;
struct node *link;
};
int issy(char x)
{
if(x=='+' || x=='-'||x=='*'||x=='/'||x=='('||x==')')
{
return 1;
}
else
{
return 0;
}
}
int j=0;
char post[25];
void postfix(struct node *y,struct node *head_op)
{
if(y->link!=head_op)
{
postfix(y->link,head_op);
post[j]=y->value;
j++;
}
else
{
post[j]=y->value;
j++;
}
}
void main()
{
struct node *head_op,*head_sy,*top_op,*top_sy,*temp;
char infix[25],x;
int i;
head_op=(struct node*)malloc(sizeof(struct node));
head_op->value='\0';
head_op->link=NULL;
top_op=head_op;
head_sy=(struct node*)malloc(sizeof(struct node));
head_sy->value='\0';
head_sy->link=NULL;
top_sy=head_sy;
printf("Enter the infix form: ");
scanf("%s",infix);
for(i=0;i<strlen(infix);i++)
{
if(issy(infix[i])==1)
{
if(infix[i]==')')
{
while(top_sy->value!='(')
{
x=top_sy->value;
temp=top_sy;
top_sy=top_sy->link;
free(temp);
temp=(struct node*)malloc(sizeof(struct node));
temp->value=x;
temp->link=top_op;
top_op=temp;
}
if(top_sy->value=='(')
{
temp=top_sy;
top_sy=top_sy->link;
free(temp);
}
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->value=infix[i];
temp->link=top_sy;
top_sy=temp;
}
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->value=infix[i];
temp->link=top_op;
top_op=temp;
}
}
postfix(top_op,head_op);
printf("%s",post);
}
/*
OUTPUT:
Enter the infix form: ((A+B)*(C^D))
AB+C^D*
*/