Skip to content

Commit 3ca6725

Browse files
authored
Add files via upload
1 parent 8e1a144 commit 3ca6725

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

Stack/04 infix to postfix.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
struct Node
5+
{
6+
int data;
7+
struct Node* next;
8+
}*top=NULL;
9+
10+
void push(char x)
11+
{
12+
struct Node *t;
13+
t = (struct Node*)malloc(sizeof(struct Node));
14+
if (t == NULL)
15+
{
16+
printf("Stack overflow");
17+
}
18+
else
19+
{
20+
t->data = x;
21+
t->next = top;
22+
top = t;
23+
}
24+
25+
}
26+
int pop()
27+
{
28+
struct Node* p;
29+
int x = -1;
30+
if (top == NULL)
31+
{
32+
printf("Stack underflow");
33+
}
34+
else
35+
{
36+
p = top;
37+
top = top->next;
38+
x = p->data;
39+
free(p);
40+
41+
}
42+
return x;
43+
44+
}
45+
/********** Function to return precedence of the operators ********/
46+
int precedence(char x)
47+
{
48+
if (x == '+' || x == '-') //if character (ie operator) is + or - return precedence as 1
49+
return 1;
50+
else if (x == '*' || x == '/') //if character is * or / return precedence as 2
51+
return 2;
52+
else //else return precedence as 0
53+
return 0;
54+
}
55+
56+
/********** Function to check if a character in infix notation is an operand or not **********/
57+
int isOperand(char x)
58+
{
59+
if (x == '+' || x == '-' || x == '*' || x == '/') //if character is not an operand return false as 0
60+
return 0;
61+
else //if character is an operand return true as 1
62+
return 1;
63+
}
64+
65+
/****** Infix to Postfix Function*******/
66+
char* InfixToPostfix(char* infix) //function which takes pointer to a character array as parameter and returns pointer to a character array
67+
{
68+
int i = 0, j = 0; //i and j initialised with 0 which will help in accessing and scanning the character array
69+
char* postfix; //pointer to a character array //it will hold the postfix notation created by the function
70+
int len = strlen(infix); //len is assigned with the string length of the infix array that has been passed as an argument //it is done to allocate same size of memory for postfix array in the HEAP
71+
postfix = (char*)malloc((len + 1) * sizeof(char)); //memory is allocated for the postfix array inside the HEAP of size equal to (len+1) where len is string length of the infix array // +1 has been added to len to store '\0' to make it a string
72+
73+
while (infix[i] != '\0') //Using while loop instead of for loop as i is not incrementing continuosly after every loop
74+
{
75+
if (isOperand(infix[i])) //if the character in the infix array is an operand it must be copied directly into the postfix array
76+
postfix[j++] = infix[i++]; //postfix copies character of infix //i and j both are incremented
77+
else
78+
{
79+
if (precedence(infix[i]) > precedence(top->data)) //if precedence of the character of infix array is greater than the precedence of the character in the top of the stack //CAUTION: as there is a chance of the top of stack getting NULL and to avoid accessing NULL->data (error), '#' is pushed in the stack before hand to avoid this problem
80+
push(infix[i++]); //push the character of the infix array inside the stack //i is incremented
81+
else //if precedence of the character of infix array is less or equal than the precedence of the character in the top of the stack
82+
{
83+
postfix[j++] = pop(); //pop the character from the stack and copy the popped character in the postfix array //j is incremented
84+
}
85+
}
86+
}
87+
88+
//If any operators are left inside the stack, all must pe popped out and copied into the postfix array
89+
while (top->data != '#') //Terminating condition for popping //'#' has been used to avoid the error of NULL->data (error)
90+
postfix[j++] = pop(); //characters are popped out of the stack one by one and copied in the postfix array
91+
92+
postfix[j] = '\0'; //last index of postfix array is assigned with '\0' to make it a complete string
93+
94+
return postfix; //postfix array is returned as char *
95+
}
96+
97+
int main()
98+
{
99+
char infix [] = "a+b*c-d/e"; //infix expression
100+
push('#'); //'#' is pushed inside stack before hand to avoid the error of accessing NULL->data (error)
101+
102+
char* postfix = InfixToPostfix(infix); //calling the function for conversion
103+
104+
printf("%s", postfix);
105+
return 0;
106+
}

0 commit comments

Comments
 (0)