Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One way to evaluate a prefix expression is to use a queue. To evaluate the expre

ID: 3548062 • Letter: O

Question

One way to evaluate a prefix expression is to use a queue. To evaluate the

expression, scan it repeatedly until the final expression value is known. In

each scan, read the tokens and store them in a queue. In each scan, replace an

operator followed by two operands by the calculated values. For example, the

following expression is a prefix expression, which is evaluated to 159.



- + * 9 + 2 8 * + 4 8 6 3



We scan the expression and score it in a queue. During the scan, when an

operator is followed by two operands, such as + 2 8, we put the result, 10 in

the queue.



After the first scan, we have



- + * 9 10 * 12 6 3



After the second scan, we have



- + 90 72 3



After the third scan, we have



- 162 3



After the fourth scan, we have



159



i want in simple queue program without using link list.

Explanation / Answer

THIS IS THE CODE SO FAR I TRIED.



int calculate(char a, char b, char c)

{

int n,p;

n=(int)a-48;

p=(int)b-48;

if(a=='+')

return (n+p);

  

else if(a=='-')

return (n-p);

else if(a=='*')

return (n*p);

else if(a=='/')

return (n/p);

}


int main()

{

char expr[]="-+9+28*+4863";

QUEUE *q = CreateQueue();

int i=0,j=1,k=2;

int r;

char dataout,data;

  

while((expr!=''))

{

if(ispunct(expr)&&isdigit(expr[j])&&isdigit(expr[k]))

  

{

r=calculate(expr,expr[j],expr[k]);

data='0'+r;

Enqueue(q, data);

i=i+3;j=j+3;k=k+3;

}

else

{

Enqueue(q,expr);

i++;j++;k++;

}

}

  

  

while(!EmptyQueue(q))

{

NODE *temp = q->front;

dataout=q->front->data;

printf("%c",dataout);

q->front=q->front->link;

q->count--;

free(temp);

}


return 0;

}