Presentation is loading. Please wait.

Presentation is loading. Please wait.

2016-06-081Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.

Similar presentations


Presentation on theme: "2016-06-081Prof. I. J. Chung Data Structure #5 Professor I. J. Chung."— Presentation transcript:

1 2016-06-081Prof. I. J. Chung Data Structure #5 Professor I. J. Chung

2 2016-06-082Prof. I. J. Chung Data Structure Stack : LIFO(Last-In-First-Out) linear data structure ; entries are deleted from the stack in the opposite order of their insertion. Entries should be inserted (pushed) and deleted at only the end, called the Top position. examples of stack : interrupt handling (AR), procedure (function) calls, PSN, evaluation of arithmetic expression, check of balanced parentheses, reversing the words, … (nodes; entries; items) books dishes Note :insertion of a node into stack : push deletion of a node from stack : pop

3 2016-06-083Prof. I. J. Chung Data Structure Note stack underflow : the condition trying to delete(access) a node from an empty-stack Stack overflow : the condition trying to insert a node to a full stack e.g. function to check a balanced parentheses boolis_balanced(const string& expression) // post condition : true if the given exp. are balanced parentheses // false otherwise {const char left_parentheses = ‘ ( ‘ ; const char right_parentheses = ‘ ) ’ ; stack store ;// stack to keep the left parentheses as they occur

4 2016-06-084Prof. I. J. Chung Data Structure string ::size_type i ; // index of the string char next;// the next char from the string bool failed = false;// is true if a needed parentheses is not found for ( i = 0 ; !failed && (i<expression.length()) ; ++i) {next = expression[i]; if (next == left_parentheses) store.push(next); else if ( ( next == right_parentheses) && (!store.empty())) store.pop(); else if ( ( next == right_parentheses) && (store.empty())) failed = true; } // end of for-loop return (store.empty() && !failed); } // end of function

5 2016-06-085Prof. I. J. Chung Data Structure stack : a linear list for which all insertions/deletions are made at one end of the list Note :1) LIFO structure 2) Imagine a pile of dishes or books 3) insertion (adding) / deletion (removing) are always done from the head (top) of the structure top bottom

6 2016-06-086Prof. I. J. Chung Data Structure A B A DBADBA A : empty P ← T P ← B P ← D P ← Ω BABA P ← d visit NODE(D)P ← RLINK(D) = Ω

7 2016-06-087Prof. I. J. Chung Data Structure e.g. EDCBAEDCBA top bottom stack o/f : attempting to push a node (item) into a full stack stack u/f : attempting to pop a node (item) from an empty stack empty -stack A Push (A) BABA Push (B) CBACBA Push (C) DCBADCBA Push (D) CBACBA Pop () BABA EBAEBA Push (E) stack implementation : ① array ② linked list EBA top = head_ptr

8 2016-06-088Prof. I. J. Chung Data Structure Applications of stack 1)interrupt handling 2)procedure (function : subroutine) processing 3)PSN (Polish String Notation) conversion 4)evaluation of arithmetic exp. 5)checking the matching of parentheses e.g. ( ( x + y ) + 2 ) – ( ( x + y ) / z )

9 2016-06-089Prof. I. J. Chung Data Structure PSN (Polish String Notation): arithmetic notation in which the operator is placed before(after) its 2 operands e.g.+AB-CD*EF/6H AB+CD-EF*6H- (merits of PSN) : 1)The order in which the operations are to be performed is completely decided by the position of the operators and operands in polish notation 2)There is no need for parentheses 3)There is no need to scan the entire exp. Computer : 1)infix → postfix 2)evaluate the postfix exp. with stack

10 2016-06-0810Prof. I. J. Chung Data Structure PSN of arithmetic exp. precedence of op. max :↑ *, / min :+, - infix notation : the operator is placed between operands (disadvantage of infix notation) 1) we need parentheses 2) we need to scan the entire expression 3) we need to consider the precedence of operators (*, / have higher precedence over + and -)

11 2016-06-0811Prof. I. J. Chung Data Structure evaluation of arithmetic exp. e.g. ABCD + E * + F + * ↑ ↑ ↑ ↑ ↑ ↑ ↑ ② ③ ④ ⑤ ⑥ ⑦ ⑧ DCBADCBA ① T 1 (C+D) B A ② + ET1BAET1BA ③ T 2 (E 1 *T 1 ) B A ④ T 2 (E 2+ B) A ⑤ FT3AFT3A ⑥ T 4 (F+T 3 ) A ⑦ T 5 (T 4 *A) ⑧

12 2016-06-0812Prof. I. J. Chung Data Structure 40 (#(# 2121 2121 * / + - 43 )↑)↑ ICPISPoperator symbol ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ e.g. A / (B + C ) * D # S ABC ABC+ /#/# A 1- f 2 ISP(#) ≥ ICP(/) /#/# 2 f 4 ISP(/) ≥ ICP(C) /#/# -1 t 2 ISP(#) ≥ ICP(*) 2 t 2 ISP(/) ≥ ICP(*) (/#(/# *#*# A B A B C + / D 0 f 1 ISP(() ≥ ICP(+) A B C + / D * +(/#+(/#

13 2016-06-0813Prof. I. J. Chung Evaluation of postfix notation (Fig. 7-10 pp.380) algo. ① add ‘#’ at the end of postfix notation ② scan the input symbol α from the input string. ③ while (α ≠ ‘ # ’ ) do { if (α = operand), put α onto stack S if (α = operator) { pop up 2 items from stack S; evaluate these two popped items with the operator α; // top item would be the 2 nd operand // next top item would be the 1 st operand put the evaluation result onto stack S; } // end of if-loop } // end of while loop

14 2016-06-0814Prof. I. J. Chung Converting a fully parenthesized infix expression into a postfix expression stack S ← { ‘ # ’ } read a symbol α α ≠ ‘ Ω ’ check α print α pop S and print this elt until ‘ ( ’ pop ‘ ( ’ from S pop S and print this elt pop S and print the popped elt until ‘ # ’ END A push α onto S A A ISP(top( S )) ≥ ICP(α) A operand while-loop f operator or ‘ ( ’ f t conversion of IN to PN ‘)’‘)’

15 2016-06-0815Prof. I. J. Chung Data Structure + / C↑ DEA B * f = A * B + C / D ↑ E postorder : AB * C DE ↑ / +


Download ppt "2016-06-081Prof. I. J. Chung Data Structure #5 Professor I. J. Chung."

Similar presentations


Ads by Google