You are on page 1of 27
Stack Definition A stack is an ordered collection of elements in which new elements may be inserted or deleted at one end called top of stack A stack is an ordered collection of elements in which new element may be inserted and from which elements may be deleted at one end called top of the stack. It is a linear data structure of a particular size. The elements can be removed in the opposite order from which they were added to the stack. $0, it is also as LIFO, i.e. Last In First Out. Eg. CD's kept inside CD pack. Structure of stack Stack is a linear data structure in which we insert and delete the items from same location called as top. In’C’ language I can declare a structure for stack as follows Array representation of Stack 0 int Stack[5]; 1 int top= Stack[5] } 2 Fi 3 In this size of stack is 5 where we can store five integer values in it. 4 Initially top is assigned as -1. I can declare same array using struct ae variable Structure representation of stack 0 struct Stack{ 1 stk[s] $2 3 i oO 4 struct Stack s; ~ s.top=-1; Where s is a variable which contains stack stk of size five. And initialise top value of stack as -1. Operations on Stacl 1) Push 2)POP Inserting inside stack is called as push operation and deletion operation is called as POP operation. Every insertion inside stack increments 'top' by one (1) and every deletion from stack decrements 'top! by 1. Representing stacks in 'C' as an array: - Insert following characters inside stack -'a, b,c, d, and e.' (Maximum size of stack is 5). 1) PUSH Operation: (Before pushing items top=-1) tops [ _e_| Tops e d d d Top2 |e ¢ c c aaeeaee Tor EE) b [ob b b top-0 |_a a a fa a a ‘Push('a’) Push(’b’) Push(‘c’) Push(’) Push(e’) ‘Overflow After insertion of every element inside stack, top is now pointing to location 4 i.e. for every insertion, top value is incremented by i é& then, item is inserted at stack [top]. --2) POP Operation: topst [—e d_|™s[ a [<< Le c L > b bo [tb _| a Loa aij a Top-0 | a Pop0=e Popa Popd=>e Popg=>b Pop(=>a Tops-1Underflow After popping every element, top is decremented by 1. When you pop, all the elements from the stack, it becomes empty (top=-1). Condition occurred while operating on Stack 1) Overflow 2) Underflow oO” ) These are the two conditions related to push and pop operations. 1) Overflow: Whenever you are implementing stack as an array, then you have limitation of array size ie. the number of elements which are pushed inside stack is equal to size of stack. When, this array becomes full, and then there is no place to insert the element into the array. At that time, status of stack is called as Overflow. 2) Underflow: Whenever, there is no element inside your stack, then that condition is called as underflow. This condition may occur * You have popped alll the elements from the stack (top=-1). At the start or before pushing any element inside stack. Underflow indicates that pop operation cannot be performed on this stack. Application of stack 1 xv To count number of opening parenthesis equal to number of closing parenthesis, Different types of parenthesis can be used to define scope of expressions then also we can use stack. Stack is used for evaluation of expression, Reversing a data - We can use a stacks to reverse data. (e.g. Files or strings). Stacks are very useful for finding palindrome. Stack is used for converting decimal number to binary number. Consider the following algorithm for conversion of decimal to binary 1. Read Number 2. While (Number>0) a, Digit=Number modulo 2 (no%2) b. Print Digit c. No=no/2 (End of step 2 loop.) The problem with this code is that it will print binary number in backward direction. e.g. (19)10 =(00010011)2 But the above algorithm prints it in reverse order like (11001000) 2 To overcome this problem, instead of printing the digits, we push them in stack. We complete the whole process (push digits) until we get the last digit =0. Then we pop the digits from stack and print it until stack is empty. Operating systems uses a stack when it calls a function. void main() void A1() void A2() { 2 { 3 S { 1 a +| _ 5 + A20; 8 Implementation of Stack using Array in #include #include fidefine MAX 5 //global declaration of stack int int int int int stack [MAX]; top=-1; isFull(); isEmpty (); pop()i void push(int); _ void display (); void main() { Int ch, num; while (1) { print£("\n1.PUSH\n2. POP\n3.DISPLAY\n4.EXIT\n\nChoose one of the above option PG scant ("$d", 6ch) ¢ switch (ch) { case 1: /* If stack is not full then number is pushed into stack otherwise it stack overflow condition.*/ if ((isFull ())==0) { print£("\nEnter the number to push into stack scané ("$a", énum) 7 push (num) ; } gag else s printé("\nStack Overflow\n\n") + break; case 2: /* If stack is not empty then pop the number from stack otherwise it stack underflow condition.*/ if ((iseEmpty ())==0) ( num=pop () ; print£("\ntd number is popped from stack successfully\n", nun); ) else printf ("\nStack Underflow\n\n"); break; case 3:display()7 oO break; case d:exit (0); } » /*check the condition whether stack is full or not*/ int isFull() { if(top== MAX-1) return 1; return 0; /*check the condition whether stack is empty or not*/ int isEmpty () ‘ if (top #include #define MAX 5 int isFull(); int isEmpty (); struct stack { int stk (MAX); int top; } void push(struct stack *,int); int pop(struct stack *); int isfull(struct stack *); int isempty(struct stack *); void display(struct stack 5); int main() int ch, num; successfully\n", item); oO ) // locally declaration of struct stack struct stack s7 S.top=-1; while (1) { print£("\n1.PUSH\n2.POP\n3.DISPLAY\n4.£XIT\n\nEnter one of above option: "3 scanf ("8d", gch) ; switch (ch) 4 case 1: case case case ) } return 0; /* If stack is not full then number is pushed into stack otherwise it stack overflow condition.*/ i€(isFull(s)==0) ( print£("\nEnter Number to push in stack: "); scanf ("¢d", enum) 7 push(és,num) i print£("\nNumber td inserted into stack successfully\n", num); , else print£ ("Stack Overflow\n"); break; 2: /* If stack is not empty then pop the number from stack otherwise it stack underflow condition.*/ if (isEmpty (s)==0) { num = pop (6s) + princf("\nNumber %d removed from stack successfully\n",num) ; } else printf ("\nStack Underflow\n"); break; 3: display(s); break; 5: exit (0); 7*Push item at top location in stack*/ void push (struct stack *st,int item) { } (st->top) ++; st->stk[ (st->top) tem; /*Pop topmost item from stack*/ ‘> int pop (struct stack *st) ( int item; item=st->top; st->top--7 return (st->stk[item]); ) /*check the condition whether stack is full or not*/ int isFull(struct stack st) t if (st. top==MAX-1) return 1; else return 07 } /*check the condition whether stack is empty or not*/ int isEmpty(struct stack st) cl Ly Af(st.topso) <2 return 1; else return 07 , void display(struct stack s) { int iy printf("\nElements in stack : "); if (!isEmpty (}) t for( { printf("\ttd",s.stk(i])+ i) 07 i This inserts item e into Q 2. e=Delete (Q) > This deleted item from Q and stored it into e. 3. Empty (Q) > This checks whether queue is Empty or not. Insertion Operation (At the rear end) front o 1 2 3 4 o 12 3 4 T al ob al ble front=0. rear=1 front=0. rear=2 Insert (b) Insert (¢) 0. 1 2 3 4 2 3 4 - 7 alb| cla cidle front=0. rear=3 vear=4 Insert (d) Insert (4) Inseri(f) > Overflow Delete Operation ( At the front end ) Delete() > a a0 1023 4 After deleting all items set front and “T rear to-1 Te[e[e 0123 4 front =2. rear=4 I ‘| Delete() > b front=1. rea Fig. Array Representation of queue Procedure fo insert at element in queue Initially set front = -1 and rear = -1 Stepl- 1iQueue already filled? © TERP=N-1 then Print “Overflow” Retum; Endif Step 2: Set R=R+1 Queue [R]J=item J//Inserts a new item in queue Retum; Provedure to delete an element from queue StepI- Queue already empty fF =-i and R=-1 then Print “Underflow” Return; Endif Step 2 Item = Queue[F] Step 3: IfF=R then Set F=-landR= Else Set F=F+1 Endif Return; Overflow and Underflow condition Queue overflow results from trying to add an efement onto a full queue. Queue underflow happens when trying to remove an element from an empty queue. imitations of Linear Queue (Disadvantages) Let us consider a queue with maximum size as ‘5’; Initially, the queue is empty ive, front = rear = -1. After 5 elements are inserted in queue front and rear will be 0 and 4, (front = 0 and rear maximum value. The queue will look like.. = 4), Rear will reach to ee eee) c|dlfe front=2. rear=4 Now we have two vacant positions at the beginning of the queue, but we cannot insert new elements as rear has reached its maximum limit which is ‘Queue Full’ or ‘Overflow’ condition, When an element is removed from the linear queue, the location remains unused. Even if queue is empty, new element can not be inserted. ‘To avoid this, the concept of circular queue is introduced , having the first element immediately following the last element. Application of Queue Queue provide services in computer science, transport and operation research where various entities such as data, objects, persons or events are stored and held to be processed later. In these context, the queue performs the function of a buffer. Implementation of Queue using Array nclude include int 1_Queue(5]; int ffon int rear=-1; int isFull( int isEmpty (); void insert (int); int delete(); void display()+ int main() i int ch; _. while(1) GG print£("\n1. INSERT\n2, DELETE\n3 . DISPLAY\n4.EXIT\n\nChoose one of above option : "); nf ("8d", &ch) 7 switch (ch) ( case 1 (isFull()}==1) print£("\nOVERFLOW. .!!!\n"); else { int item; print£("\nEnter the number to insert : " scan ("&d", gitem) ; insert (item); } break; case 2: if ( (isEmpty ())==1) printé ("\nUNDERFLOW..!!!\n"); else ( int item item=delete(); printf("\ntd element is deleted from Queue successfully. \n", item); } break; case 3: display(); break; case 4: exit(0); : break; default: printf ("\nWRONG CHOICE..!!!"); } return 0; int isFull() ( if (rear: return 1; else return 0; } int isEmpty () { if (front return 1; else return 0; et rear+*; 1_Queue [rear] =iitem: print£("\ntd element is inserted into Queue successfully. \n",iitem); int delete() { int ditem; Queue [front]; frontt+; return ditem oO void display() { int temp; print£("\nElements in Queue :"); for (temp=front; temp<=rear; temp++) print£("\t%d",1_Queue(temp]) ; } Implementation of Queue using Structure #include #include #include int que[5]; Po int, int’ rear=. int isFull(); int isempty(); void insert (int); int delet (); void display(); int main() { int chy clrser(); while (1) ( printé("\nl. INSERT\n2. DELETE\n3, DISPLAY\n4.EXIT\n\nChoose one of above option : "); scant ("8d", @ch) ; switch (ch) { case 1: if(isFull()) ( printf ("\nOVERFLOW. } else ( int ins; printf("\nEnter the number to insert : scant ("td", gins) 7 insert (ins); ) break; f\n") 7 case 2: if(isempty()) { print ("\nUNDERFLOW } else t int del; del=delet () + printé("\ntd element is deleted from Queue successfully...\n",del) ; } break; case 3: display(); break; case 4: exit (0); break; default: print£("\nWRONG CHOICE..!!!\n"); } ) return 0; getch(); ) int isFull() { printf("\nREAR : $d\tFRONT : %d",rear, front); if(((reartl) & 5) == front) return 1; else return 0; ) int isempty() ( print£("\nREAR : %d\tFRONT : #d",rear, front}; if(front=*-1 &@ rear ==-1) return 1; else return 0; f void insert (int in) pee if (front { front++; } rear=((reartl) % 5); que (rear! printf ("\nNEW REAR : ¢d\tFRONT : %d", rear, front); print£("\nd element is inserted into Queue successfully...\n",in); 1) } int delet () { int de; de=que [front]; if(lisempty()) if (front==rear) ((£ront+1) 8 5)7 print£("\nNEW REAR : $d\tFRONT : %d",rear, front); return de; ) void display() { int i; i-front; do { printé(*\ted",que(i}) + i= (itl) 85; }while(i!=rear); printé("\ted",que[i]); /* i€(front < rear) ( print£("\nREAR : %@\tfRONT : 8d",rear, front); printf ("\ted", que(il); for (i=front;i<=rear;it+) printeé("\t8d", que[i]) , else 4 printf ({"\nREAR : $d\tFRONT : %d",rear, front); printf ("\nElements in Queue :"); for (i=rear. ront;i+t+) printf ("\ttd",que[i]); Ves c Circular Queue Circular Implementation of Queue In order to use vacant position we have to bring rear to the first(0") location, if itis empty. ie. If (rear = = max) rear Else Tear = rear +1 The queue is logically treated as circular queue. Example- Consider the queue with following elements o 1 2 3 4 30| 40 | 50 front =2, rear=4 Insert new element 60 in the queue 0 8 8 8 4 60 30] 40 | 50 front=2, rear=0 This is represented circularly as- Operations Performed on Circular Queue Insert Operation if (isFullQ) rear=(rear+1) % max © Queue [rear}= item; else Print ‘Overflow’; Delete Operation if (tisEmpty)) item=Queue[front]; front=(front+1) % max Print Item, else Print ‘Underflow’; Overflow and Underftow conditions for Circular Queue Odueue overtiow results from trying to add an element onto a fall queue. Queue underflow happens when trying to remove an element from an empty queue. Overflow condition int isFull () if if ((tear+1) % max = = front) then return else retrun 0; } Underflow condi int isEmpty () { if (front |Jrear return 15 -1) then else retrun 0; imitations of Circular Queue As the circular queue requires two pointers for start and end, when the queve is full both the pointers pointing at the same element and if we tried to add new elements in queue the old element may get overwritten. ‘fo overcome this problem, keep one slot unallocated in the circular queue. A full queue has at most (size-1) slots. If both pointers are pointing at the same location, the queue is empty If the old pointer plus one (rear+1) equals the start pointer (front), then the queue is full. Advantages 1. Very simple and robust 2. You need only the two pointers for implementation Disadvantages 1, You can never use the entire queue, 2. You might only be access one element at a time, since you won't easily know how many elements are next to each other in memory. COmplementation of Circular Queue #include #include #include int int int int isFull(); int isEmpty (); void insert (int); int delet (); void display(); 10 * int main() a { int chy clrser(); while (1) 4 printé(*\nlINSERT\n2. DELETE\n3.DISPLAY\n4.EXIT\n\nChoose one of above option : y3 scanf ("$d", &ch) ; switch (ch) ‘ case 1: if(isFull()) ( printé("\nOVERFLOW. .! in"); } else { int ins; print£("\nEnter the number to insert : scant ("%d", sins); insert (ins); } break; case 2: if(isEmpty()) ( print£("\nONDERELOW..!!!\n") j 1 else ( int del; deladelet () 7 print£("\ntd element is deleted from Queue oO successfully...\n",del); } break; case 3: display(); break; case 4: exit (0); breaks default: printé("\nWRONG CHOICE... \n"); 1 yeturn 0; getch(); ) int isfull() {printf ("\nREAR : &d\tFRONT : 8d", rear, front); if(((reart1) $ 5) == front) return 1; else return 0; ) int isempty () ( print€("\nREAR : &d\tFRONT : td",rear, front); if(front==-1 &@ rear ==-1) return 1; else return 0; ) void insert (int in) -1) i if (front front++; } rear=((rear+1) 8 5); que (rear]=in; print£("\nNEW REAR : $d\cFRONT : $d", rear, front); printf("\ntd element is inserted into Queue successfully...\n",in); } int delet() ‘ int de; de=que[ front}; if (/isEmpty()) front=((front+1) & 5)7 printf ("\nNEW REAR : $d\tFRONT : 4d", rear, front); return de; ) void display\) einai i=front; do 12 printf ("\t8d", que(il); i=(i41) 85; }while (i!=rear); print£("\t%a",que[i]) 7 f* if (front < rear) { printf("\nREAR : $d\tFRONT : %d",rear, front); printé("\t%d",que[i]); for (i=front;i<-rear;it+) printé("\tsd",que li); } else ( printf ("\nREAR : $d\tFRONT : %d",rear, front); printf£("\nElements in Queue :"); for (i=rear;i<-front; i++) printf ("\tsa",que(i}) yes 13 Application of Stack and Queue " Arithmetic Expressions: (Infix, Prefix, Postfix) Let Q be an arithmetic expression involving constants and operations. We find the value of Q by using postfix notation. Stack is an essential tool in this algorithm. Consider three levels of precedence for the usual five binary operations. Highest : Exponentiation ( ) Next highest : Multiplication (*) and division ( / ) Lowest : Addition (+) and subtraction (-) Infix Notation: For most common arithmetic, the operator symbol is placed between its two operand this is called as infix notation, Eg. (A+B)*C and A+B*OQ Prefix Notation: Prefix notation in which one operator symbol is piaved before its two operands. Fg. +AB -cD *EF /GH Postfix Notation: Reverse polish notation refefs to the analogous notation in which the operator symbol is placed after its two operands. Eg. AB+ c- EE* GH Evaluation of Expression Example A+B*C is a expression in standard infix notation. To evaluate this, we require knowledge of which operation + ox * is to be evaluated first. In this case, multiplication (*) is to be done Cefore addition (+) as multiplication having higher precedence than addition. To convert the expression A+B*C in postfix Applying the rules of precedence, we first convert the portion of the expression that is evaluated first that is multiplication is done before addition. To convert the infix expression to postfix we have to follow the steps A+(B*C) DEI+E2 = EL E2 + = ABC*+ 2 During conversion process the operations with highest precedence are converted first and after a portion of expression has been converted to postfix, it is to be treated as single operand. If the expression contains parenthesis, then evaluate parenthesis first. Example (A+B)*C Infix form E1=A+B E2=C As addition contains parenthesis, convert addition first, E1=A+B= AB+ ; E2=C > EIE2 = El E2* = AB+C* We consider 5 operations; addition(+); subtraction (-); multiplication (*); division (/) & exponent ($) For binary operation following is order of precedence - Exponential -Highest - Multiplication/ Division - Addition/ Subtraction -Lowest When un-parenthesised operators of the same precedence are scanned, the order is assumed to be left to right for +, - and *,/; In case of exponential, the order is assumed to be from right to left) - Evaluation of a Postfix Expression (Algorithm) oO Suppose P is an arithmetic expression written in postfix notation, The following algorithm, which uses a STACK to hold operand, evaluates P, Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in postfix notation. 1) Add aright parenthesis ”)’at the end of P. [This acts as a sentinel,] 2) Scan P from left to right and repeat Steps 4 and 4 for each element of P until the sentinel ”)”is encountered. 3) If an operand is encountered, put it on STACK. 4) Ifan operator ® is encountered, then: a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element. b) Evaluate B® A. ©) Place the result of (b) back on STACK, [End of If Structure] [End of Step 2 loop] 5) Set VALUE equal to the top element on STACK. 6) Exit We note that, when Step 6 is executed, there should be only one number on STACK. Example: Consider the following arithmetic expression P written in postfix notation: Feeney 6, 2 eee 12, 4, ip @ (Commas are used to separate the elements of P so that 5, 6, 2 is not interpreted as the number 562.) e Symbol Scanned | _ STACK . 15 5 2. 6 5,6 a) 5,62 a+ 5,8 3 40 6. 12 40,12 74 40,12, 4 8 / 40,3 9: 37, 10.) The equivalent infix expressions Q follows: oO Q@ 5*(6+2)-12/4 Note that parentheses are necessary for the infix expression Q but not for the postfix expression P. We evaluate P by simulating Algorithm. First we add a sentinel right parenthesis at the end of P to obtain. P: 5 6 2% +4 % 12% 4 ff, ~ ) y 2 3 4 5 6 FH 8 YH 10) The elements of P have been labelled from left to right for easy reference. Fig. shows the contents of STACK as each element of P is scanned. The final number in STACK, 37, which is assigned to VALUE when the sentinel “)” is scanned, is the value of P. ~ Questions: . Evaluate the following postfix expressions 1 2. 3. 4 - 6234+-382/ =*293+ . Assume A=1, C=3 Evaluate AB+C-BA+C $- AB*CBA-+* (Assume A=1, B=2, C=3) . 42$3*3-84/11+/+ Transforming Infix Expressions into Postfix Expressions:- Let Q be an arithmetic expression written in infix notation. Besides operands and operators, Q may also contain left and right parentheses, We assume that the operators in Q consist only of exponentiations (t), multiplication (*), divisions (/), additions (+) and subtractions (-) and that they have the usual three levels of precedence as given above. We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by parentheses. The following algorithm transforms the infix expressions Q into its equivalent postfix expression P. The algorithm uses a stack to temporarily hold operators and left. The pastfix expressions P will be constructed from left to right using the operands from Q and the operators which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The algorithm is completed when STACK is empty. Algorithm: POSTFIX_EXPR (Q, P) Suppose Q is an arithmetic expression written in infix notation, This algorithm finds the equivalent postfix expression P. oO 1) 2) 3) 4 5) 8) Push “(“onto STACK, and add “)’ to the end of Q. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the stack is empty: If an operand is encountered, add it to P. Ifa left parenthesis is encountered, push it onto STACK. Ifan operator ® is encountered, then * Repeatedly pop from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence then ®. * Add @ toSTACK. [End of If structure] Ifa right parentheses is encountered, then * Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered. * Remove the left parenthesis. [Do not add the left parenthesis to P.] [End of If structure] [End of Step 2 Loop] 7) Exit Example: Consider the following arithmetic infix expression Q: Q: A+(B*C-(D/ETF)*G)*H) We simulate Algorithm to transform Q into its equivalent postfix expression P. First we push “(” onte STACK, and then we add “)” to the end of Q to obtain: G)* 16 17 18 H ) Qa ( B c-( D/ Et EF) * 1 19 20 2 oe oto 6 a7 Oe teehee ui? 119) 91415) The elements of Q have now been labelled from left to right for easy reference. Fig. Shows the status __ Of STACK and the string P as each element of Qis scanned. Observe that <3 1) Each operand is simply added to P and does not change STACK. 2) The subtraction operator (-) is row 7 send * from STACK to P before it (-) is pushed onto STACK. 3) The right parenthesis in row 14 sends } and then / from STACK to P, and then removes the left parenthesis from the top of STACK. 4) The right parenthesis in row 20 sends * and then + from STACK to P, and then removes the left parenthesis form the top of STACK. After Step 20 is executed, the STACK is empty and PAB c 7 D E F t / G which is the required postfix equivalent of Q. Symbol Scanned __| STACK Expression P On A ( A 2) + (= A 3)_( (+( A 4) B C( AB 5) * GC AB 6 ¢ CC ABC 2) - GC ABC* 8) ( (exert ABC 9 dD GC ABC*D. 10) GC ABC*D tl) E CC ABC*DE 12) 7 ((-(/1 ABC*DE 13) F CHC- (7/4 _[ABC#DEF _ 14) ) (+(- ABC*DEFT/ “L45) * Gt ADBC*DEFT/ 16) G Cages ABC*DEF{/G me) ce - _ ABC*DEF1/G*- 18)_* cs ABC*DEFT/G*- 19) H (+* ABC*DEF{/G*-H ~ 20) ) t ABC*DEF1/G*-H*+ Questions Convert the following infix expression to postfix expression ASB*C . (AFB)*C . (AFB)*(C-D) . ((A+B)*C-(D-E))$(F+G) Transforming Infix Expressions into pebe Push “)” onto STACK, and add “(* to end of the A Scan A from right o left and repeat step 3 to 6 for each element of A until the STACK is empty If.an operand is encountered add it to B Ifa right parenthesis is encountered push it onto STACK If an operator is encountered then: a, Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same b. or higher precedence than the operator. c. Add operator to STACK. 6. If lett parenthesis is encountered then . a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encountered) b. Remove the left parenthesis, © 7 Et Ree ixpressions:- * yaReye Start ¥ Reverse Given Expression ¥ Apply algorithm of infix to postfix conversion ¥ Reverse the Expression ¥ End o Expression = (A+B*C)*D+E*S Step 1. Reverse the infix expression. SSE+D*)C*B+A( Step 2. Make Every ‘('as and every ')! as'( 58E+D*(C*B+A) Step 3. Convest expression to postfix form. Expression Stack Output SAE+D#(C*BHA) Empty - *E+D#(C*BHA) Empty 5 E+D*(CABHA) n 5 +D*(C*B+A) a 3E _D*(C*BHA) + SES \A(C*BHA) + SE*D (C*B+A) a SED CABEA) + SEND BHA) + SESDC BHA) +0 SESDC +A) #0 SENDCB A) orem SEDCB* ) +(e SESDCB*A End + SE*DCB*A+ End Empty SE*DCB*AH#+ Step 4. Reverse the expression. O +*+A*BCD‘ES ) Result +*+A*BCDAES: Comment Initial Print Push Push Pop And Push Print Push Push Print Push Print Pop And Push Print Pop Until '( Pop Every element

You might also like