Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1471 from MHamiid/patch-8
Browse files Browse the repository at this point in the history
Create 150-Evaluate-Reverse-Polish-Notation.c
  • Loading branch information
Ahmad-A0 authored Nov 19, 2022
2 parents 20ed2d3 + a3b54d3 commit e1bb274
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions c/150-Evaluate-Reverse-Polish-Notation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
int evalRPN(char ** tokens, int tokensSize){
long int stk[tokensSize];

int stkIndex = -1;

for(int i = 0; i < tokensSize; i++)
{
if(strcmp(tokens[i], "+") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = first + second;
}
else if(strcmp(tokens[i], "-") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = second - first;
}
else if(strcmp(tokens[i], "*") == 0)
{
long first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = first * second;

}
else if(strcmp(tokens[i], "/") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = second / first;
}
else
{
stkIndex++;
stk[stkIndex] = atoi(tokens[i]);
}

}


return stk[stkIndex];
}

0 comments on commit e1bb274

Please sign in to comment.