This document discusses evaluating postfix expressions using a stack, queues, and machine shop simulations. It provides examples of evaluating postfix expressions step-by-step using a stack. It describes the first-in first-out nature of queues and different implementations including linked lists, double ended queues, and circular arrays. It also outlines the logic of a machine shop simulation where jobs require processing by multiple machines in a specified order.
This document discusses evaluating postfix expressions using a stack, queues, and machine shop simulations. It provides examples of evaluating postfix expressions step-by-step using a stack. It describes the first-in first-out nature of queues and different implementations including linked lists, double ended queues, and circular arrays. It also outlines the logic of a machine shop simulation where jobs require processing by multiple machines in a specified order.
This document discusses evaluating postfix expressions using a stack, queues, and machine shop simulations. It provides examples of evaluating postfix expressions step-by-step using a stack. It describes the first-in first-out nature of queues and different implementations including linked lists, double ended queues, and circular arrays. It also outlines the logic of a machine shop simulation where jobs require processing by multiple machines in a specified order.
This document discusses evaluating postfix expressions using a stack, queues, and machine shop simulations. It provides examples of evaluating postfix expressions step-by-step using a stack. It describes the first-in first-out nature of queues and different implementations including linked lists, double ended queues, and circular arrays. It also outlines the logic of a machine shop simulation where jobs require processing by multiple machines in a specified order.
Download as DOC, PDF, TXT or read online from Scribd
Download as doc, pdf, or txt
You are on page 1of 3
Evaluating a postfix expression using a stack
while there are tokens
Get the token If token is operand then stack.Add(token) else stack.Delete(second) stack.Delete(first) result = first token second // token is the operator stack.Add(result) return stack.op() !" ! # $ % &' ( / ) * + # % !" and ! go on top the stack (!", !) when we co-e across # we pop two ite-s fro- the stack second = !, first = !". result = !" # ! = '" goes on stack $ goes on top of the stack ('",$) % pop $ and '" second = $, first = '". result = '" % $ = '$ goes on stack ('$) &' and ( go on stack ('$,&',() for division second = (, first = &' result = &' / ( = ! goes on stack ('$,!) ) operator/ second = !, first = '$, result = '$ ) ! = '( goes on stack ('() * and + on stack ('(,*,+) #/ second = +, first = *, result = &* goes on stack ('(,&*) %/ second = &*, first = '(, result = 0! (0!) no -ore tokens, stack has the answer/ 0! Given an infix expression/ 1riorities for the operators and parentheses. he2 -ake things co-plicated we use stack of operators instead of operands 3perators have different priorities in stack and outside the stack Operator In stack priority In-coming priority ) ) ) 4 + + #,/ ! ! %, ) & & ( " ' while true do if we are at the end of expression then print the stack and 5uit else if x is an operand then print x else if x = 6)6 then while top of the stack is not e5ual to 6(6 do Delete an ite- fro- the stack and print the ite- deleted pop 6(6 fro- the stack else while the priorit2 of the ite- on top of the stack is not less than priorit2 of x do Delete an ite- fro- the stack and print the ite- retrieved push x on the top of the stack end 7while8 ($#+)/((%(*#')) % ($ %(!)/+4+ 3utput/ $ + # ( * ' # % / $ (! % + + 4 / % ( goes on stack 9(: $ gets printed, # icp = !goes on top of ( ;ecause in stack priorit2 isp of ( is ". 9(,#: + gets printed, ) prints out stack as long as (, pops ( without printing it, 9: / goes on stack 9/:, ( icp=' goes on stack 9/,(: ( print, % goes on top of stack isp of ( is " 9/,(,%: ( isp = ' 9/,(,%,(: * print, # on stack 9/,(,%,(,#: ' print, ) print out until (, repeat for next ) 9/: % can<t go on top / pop and print / 9%: see the ;oard for the rest of the details =ee how the resulting expression was evaluated to ' Queues >I>3. >irst in >irst 3ut Insertion and Deletions are all done at the end of the list =pecial list/ can ;e derived fro- ?ist AD =ince the operations are at the end, we should ;e a;le to -anage the- in constant ti-e. Deletion is fro- the head and insertion is to the tail. @hain/ Delete(A)/ Delete(&,x) 3(&) Add(x)/ Insert(?ength(),x) 3(n) Add is -ore expensive than it needs to ;e, so we need to rewrite it, ;2 adding another pointer called rear that points to the last ele-ent. 3nce 2ou -odif2 the data -e-;ers, 2ou cannot use the Insert function. Be -a2 have to -odif2 the rear pointer when we Delete. =ee the l5ueue.h if 2ou want, we will ;e using our Dou;leCueue Dou;leCueue Delete(A)/ Delete(&,x) 3(&) Add(x)/ Insert(?ength(),x) 3(&) >or-ula ;ased i-ple-entation of the list If we -ake front to ;e the first ele-ent, and rear to ;e the last ele-ent. Delete(x)/ Delete(&,x) 3(n) Add(x)/ Insert(?ength(),x). 3(&) >or stack, we fixed this situation ;2 flipping the ends. Be can tr2 the sa-e thing Delete(x)/ Delete(?ength(),x) 3(&) Add(x)/ Insert(&,x). 3(n) Be need a different for-ula);ased i-ple-entation @ircular Arra2s ?ogicall2, these arra2s wrap around 1h2sicall2, we still have to use the sa-e arra2s that we used ;efore. @ircular arra2 i-ple-entation in @hapter 0. Atleast one e-pt2 space is left in the arra2, ;etween first and the last ite-. his helps in distinguishing ;etween 5ueue full and 5ueue e-pt2 situation. he code is si-plified at the expense of a wasted space. Another i-ple-entation could keep front at the first ele-ent and rear at the last ele-ent. he isfull and ise-pt2 are taken care of ;2 another varia;le called siDe. he code for this i-ple-entation will not ;e as s2--etric as the one in the ;ook, ;ut it would take less -e-or2 and will ;e easier to read. Eachine =hop Fo;s that need to ;e processed ;2 several -achines he progra- in the ;ook assu-es that we have all the Go;s in ;efore the shop opens. Ho new Go;s are taken in during the operation Evaluate how efficient the s2ste- is Each Go; has a list of tasks ask has a -achine nu-;er and how -uch ti-e will ;e re5uired he order is i-portant Each -achine has a 5ueue and -achine needs so-eti-e to changeover fro- one Go; to the next