evaluation of postfix expression using stack in c
JavaScript
// C program to evaluate value of a postfix expression
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
if (!stack) return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
if (!stack->array) return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
// The main function that returns value of a given postfix expression
int evaluatePostfix(char* exp)
{
// Create a stack of capacity equal to expression size
struct Stack* stack = createStack(strlen(exp));
int i;
// See if stack was created successfully
if (!stack) return -1;
// Scan all characters one by one
for (i = 0; exp[i]; ++i)
{
// If the scanned character is an operand (number here),
// push it to the stack.
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}
// Driver program to test above functions
int main()
{
char exp[] = "231*+9-";
printf ("postfix evaluation: %d", evaluatePostfix(exp));
return 0;
}
Also in JavaScript:
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- is var is not blank then display value in javascript
- Category
- JavaScript
- Title
- how to concatenate strings javascript
- Category
- JavaScript
- Title
- javascript anagram check
- Category
- JavaScript
- Title
- how to check if a number is even or odd in javascript
- Category
- JavaScript
- Title
- command to create react app
- Category
- JavaScript
- Title
- How do you call a functional component in React
- Category
- JavaScript
- Title
- javascript check if function exists
- Category
- JavaScript
- Title
- A cookie associated with a cross-site resource at http://placeholder.com/ was set without the `SameSite` attribute.
- Category
- JavaScript
- Title
- how to get mouse coordinates in javascript
- Category
- JavaScript
- Title
- angular reactive forms
- Category
- JavaScript
- Title
- eslint ignore javascript
- Category
- JavaScript
- Title
- how to see if a web site is useing react
- Category
- JavaScript
- Title
- add a slash to string in javascript
- Category
- JavaScript
- Title
- insert into specific array index that has a value existing on that index
- Category
- JavaScript
- Title
- angular onchange event
- Category
- JavaScript
- Title
- credit card mask js
- Category
- JavaScript
- Title
- how to deep copy object in javascript
- Category
- JavaScript
- Title
- how to add object to list in javascript
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- import reactdom
- Category
- JavaScript
- Title
- ajax laravel example
- Category
- JavaScript
- Title
- how to read json file in python
- Category
- JavaScript
- Title
- how to create seeds in sequelise
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- javascript are arrays equal
- Category
- JavaScript
- Title
- how to do a classname variable and string react
- Category
- JavaScript
- Title
- columndefs in datatable not working while setting width jquery
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- getelementbyid
- Category
- JavaScript
- Title
- compress string javascript
- Category
- JavaScript
- Title
- foreach and replace item based on condition
- Category
- JavaScript
- Title
- async await catch error
- Category
- JavaScript
- Title
- .shift js
- Category
- JavaScript
- Title
- angular bootstrap not working
- Category
- JavaScript
- Title
- function inside a class component react
- Category
- JavaScript
- Title
- check if number appears odd number of times in array javascript
- Category
- JavaScript
- Title
- express bodyparser deprecated
- Category
- JavaScript
- Title
- html to jsx
- Category
- JavaScript
- Title
- create react component class
- Category
- JavaScript
- Title
- format a date moment
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- how to access array datat in class component react
- Category
- JavaScript
- Title
- javascript change attribute
- Category
- JavaScript
- Title
- app listen with all error handling
- Category
- JavaScript
- Title
- .join javascript
- Category
- JavaScript
- Title
- convert jquery to string
- Category
- JavaScript
- Title
- bootstrap javascript cdn
- Category
- JavaScript
- Title
- how to assign same value of a key to 2 different keys in output JSON jolt
- Category
- JavaScript
- Title
- discord js bot leave voice channel
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- find substring in string javascript
- Category
- JavaScript
- Title
- adding delay in javascript foreach loop
- Category
- JavaScript
- Title
- alternate color to table row jquery
- Category
- JavaScript
- Title
- how to add script before </body> in magento 2
- Category
- JavaScript
- Title
- how to make a calculator websiteJS
- Category
- JavaScript
- Title
- how to update angular version
- Category
- JavaScript
- Title
- array javascript
- Category
- JavaScript
- Title
- extend react.component
- Category
- JavaScript
- Title
- debug node
- Category
- JavaScript
- Title
- how to host react app on heroku
- Category
- JavaScript
- Title
- expo create react native app
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- cb() never called! npm ERR! This is an error with npm itself.
- Category
- JavaScript
- Title
- const{}=
- Category
- JavaScript
- Title
- flutter vs react native
- Category
- JavaScript
- Title
- How to change htm h1 from nodejs
- Category
- JavaScript
- Title
- audio get current time
- Category
- JavaScript
- Title
- ISS proxy express
- Category
- JavaScript
- Title
- how to add button in alert box in react native
- Category
- JavaScript
- Title
- bootstrap js cdn
- Category
- JavaScript
- Title
- how to create response time router node js
- Category
- JavaScript
- Title
- chart.js line chart
- Category
- JavaScript
- Title
- ellipsis javascript
- Category
- JavaScript
- Title
- find index of object in array javascript
- Category
- JavaScript
- Title
- How to check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- create a react native project
- Category
- JavaScript
- Title
- how to make fizzbuzz in javascript
- Category
- JavaScript
- Title
- chrome add bookmark that prefixes text
- Category
- JavaScript
- Title
- delete js
- Category
- JavaScript
- Title
- how to make text come up with javascript
- Category
- JavaScript
- Title
- how to access a database in express
- Category
- JavaScript
- Title
- get value of checked radio button jquery
- Category
- JavaScript
- Title
- go to another page javascript
- Category
- JavaScript
- Title
- html inline javascript
- Category
- JavaScript
- Title
- basic javascript
- Category
- JavaScript
- Title
- assign array to another array javascript
- Category
- JavaScript
- Title
- destruct e.target.value param
- Category
- JavaScript
- Title
- dynamic forms in react
- Category
- JavaScript
- Title
- angular http call caching issue even after no-cache
- Category
- JavaScript
- Title
- how to compare a string with its ending in javascript
- Category
- JavaScript
- Title
- cache buster in angular application
- Category
- JavaScript
- Title
- javascript after split then trim whitespace
- Category
- JavaScript
- Title
- how to insert a value into an array javascript
- Category
- JavaScript
- Title
- date picker type react
- Category
- JavaScript
- Title
- javascript clear all intervals
- Category
- JavaScript
- Title
- discord.js wait seconds
- Category
- JavaScript
- Title
- check the doc name in javascript
- Category
- JavaScript
- Title
- get selected text js
- Category
- JavaScript
- Title
- angular directive example
- Category
- JavaScript