Page 73 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 73
Advanced Data Structure and Algorithms
Notes else
return 0;
}
int pred_level(char ch) /* determine precedence level */
{
if ( ch == ‘+’ || ch == ‘-’ ) return 1;
else if ( ch == ‘^’ )
return 3;
else
return 2;
}
int precedence(char operator1, char operator2) /* determine if the
precedence of operator1 is less than, equal to, greater than the precedence of
operator2 */
{
if ( pred_level(operator1) > pred_level(operator2) )
return 1;
else if ( pred_level(operator1) < pred_level(operator2) )
return -1;
else
return 0;
}
void push(stk *stack, char value) /* push a value on the stack */
{
if ( !(isFull(stack)) )
{
(stack->top)++;
stack->data[stack->top] = value;
}
}
char pop(stk *stack) /* pop a value off the stack */
{
char ch;
if ( !(isEmpty(stack)) )
{
ch = stack->data[stack->top];
(stack->top)--;
return ch;
}
else
return ‘\0’;
68 LOVELY PROFESSIONAL UNIVERSITY