assignment6a

1 answer heres one possible implementation only

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ressions of your choosing) to demonstrate it works properly. 1 Answer: Here’s one possible implementation: (only functions infix to postfix() and evaluate postfix() shown) / ∗ c r e a t e s a q ueue o f t o k e n s i n p o s t f i x o r d e r f rom a q ueue o f t o k e n s i n i n f i x o r d e r ∗ / / ∗ p o s t c o n d i t i o n : r e t u r n e d q ueue c o n t a i n s a l l t h e t o k e n s , a nd p q u e u e i n f i x s h o u l d b e e mpty ∗ / s truct t o k e n q u e u e i n f i x t o p o s t f i x ( s truct t o k e n q u e u e ∗ p q u e u e i n f i x ) { / ∗ TODO: c o n s t r u c t p o s t f i x −o r d e r e d q ueue f rom i n f i x −o r d e r e d q ueue ; a l l t o k e n s f rom i n f i x q ueue s h o u l d b e a dded t o p o s t f i x q ueue o r f r e e d ∗ / p e x p r t o k e n s t a c k t o p = NULL, p token ; struct t o k e n q u e u e q u e u e p o s t f i x ; q u e u e p o s t f i x . f r o n t = q u e u e p o s t f i x . back = NULL; f o r ( ptoken = d equeue ( p q u e u e i n f i x ) ; p token ; p token = d equeue ( p q u e u e i n f i x ) ) { switch ( ptoken −>type ) { case OPERAND: / ∗ o p e r a n d s a dded d i r e c t l y t o p o s t f i x q ueue ∗ / e nqueue(& q u e u e p o s t f i x , p token ) ; break ; case OPERATOR: / ∗ o p e r a t o r a dded t o s t a c k , a f t e r o p e r a t o r s o f h i g h e r p r e c e d e n c e a r e moved t o q ueue ∗ / while ( s t a c k t o p && ( o p p r e c e d e n c e s [ s t a c k t o p −>v a l u e . o p c o d e ] > o p p r e c e d e n c e s [ ptoken −>v a l u e . o p c o d e ] | | ( o p p r e c e d e n c e s [ s t a c k t o p −>v a l u e . o p c o d e ] == o p p r e c e d e n c e s [ ptoken −>v a l u e . o p c o d e ] && o p a s s o c i a t i v i t y [ o p p r e c e d e n c e s [ ptoken −>v a l u e . o p c o d e ] ] == LEFT ) ) ) e nqueue(& q u e u e p o s t f i x , p op(& s t a c k t o p ) ) ; push(& s t a c k t o p , p token ) ; break ; default : / ∗ o t h e r t o k e n s i g n o r e d ∗ / f r e e ( ptoken ) ; break ; } } while ( s t a c k t o p ) / ∗ p op r e m a i n i n g o p e r a t o r s o f f s t a c k ∗ / enqueue(& q u e u e p o s t f i x , p op(& s t a c k t o p ) ) ; return q u e u e p o s t f i x ; } / ∗ e v a l u t e s t h e p o s t f i x e x p r e s s i o n s t o r e d i n t h e q ueue ∗ / / ∗ p o s t c o n d i t i o n : r e t u r n e d v a l u e i s f i n a l a nswer , a...
View Full Document

Ask a homework question - tutors are online