Page 95 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 95
Advanced Data Structure and Algorithms
Notes
Case Study Convert an infix expression to prefi x form.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 80
typedef enum {FALSE, TRUE} bool;
#include “stack.h”
#include “queue.h”
#define NOPS 7
char operators [] = “()^/*+-”;
int priorities[] = {4,4,3,2,2,1,1};
char associates[] = “ RLLLL”;
char t[N]; char *tptr = t; // this is where prefix will be saved.
int getIndex( char op ) {
/*
* returns index of op in operators.
*/
int i;
for( i=0; i<NOPS; ++i )
if( operators[i] == op )
return i;
return -1;
}
int getPriority( char op ) {
/*
* returns priority of op.
*/
return priorities[ getIndex(op) ];
}
char getAssociativity( char op ) {
/*
* returns associativity of op.
*/
return associates[ getIndex(op) ];
}
void processOp( char op, queue *q, stack *s ) {
/*
Contd...
90 LOVELY PROFESSIONAL UNIVERSITY