-
Notifications
You must be signed in to change notification settings - Fork 1k
/
postfix_to_prefix.c
135 lines (123 loc) · 2.35 KB
/
postfix_to_prefix.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Infix, postfix and prefix are three different but equivalent notations of writing
algebraic expressions.
In postfix notation, operators follow their operands.
In prefix notation, operators precede their operands.
In this program, the user inputs a postfix expression and the prefix notation for that
given postfix notation is the output. Postfix notation is converted to prefix notation with
the help of stack data structure.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 30
struct stack
{
char sta[MAX];
struct stack * next;
};
struct stack *st = NULL;
char a[MAX], b[MAX], c[MAX];
//push function of stack
void push(char s[MAX])
{
struct stack *temp = (struct stack *) malloc(sizeof(struct stack));
strcpy(temp->sta, s);
temp->next = NULL;
if (temp == NULL)
printf("Memory Overflow\n");
else
{
if (st == NULL)
st = temp;
else
{
temp->next = st;
st = temp;
}
}
}
//pop function of stack
void pop(char d[])
{
struct stack *t = st;
st = t->next;
strcpy(d, t->sta);
free(t);
}
//Function to count number of elements in stack
int count()
{
int c = 0;
struct stack *t = st;
while (t != NULL)
{
c++;
t = t->next;
}
return c;
}
//Function to convert postfix expression to prefix expression
void PostfixToPrefix(char post[], char pre[])
{
int i = 0;
char op[3];
while (post[i] != '\0')
{
if (isalpha(post[i]) != 0 || isdigit(post[i] != 0))
{
op[0] = post[i];
op[1] = '\0';
push(op);
}
else
{
if (count() < 2)
{
printf("Not sufficient values in the expression\n");
exit(1);
}
else
{
op[0] = post[i];
op[1] = '\0';
pop(a);
pop(b);
c[0] = '\0';
strcat(c, op);
strcat(c, b);
strcat(c, a);
push(c);
}
}
i++;
}
if (count() == 1)
{
pop(pre);
printf("Prefix Expression: %s", pre);
}
else
{
printf("Invalid user input");
}
}
void main()
{
char pre[MAX], post[MAX];
printf("Enter a postfix expression: ");
gets(post);
PostfixToPrefix(post, pre);
}
/*
Time Compexity:
Push function - O(1)
Pop function - O(1)
Count funtion - O(n)
PostfixToPrefix function - O(n)
Space Complexity: O(n)
Sample Output:
Enter a postfix expression: ABCDE-+^*EF*-
Prefix Expression: -*A^B+C-DE*EF
*/