Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,

Similar presentations


Presentation on theme: "Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,"— Presentation transcript:

1 Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

2 Stacks An ordered collection of items which items are inserted (pushed) into and are deleted (popped) from the stack at one end called the top of the stack. LIFO List. (last in first out)

3 Stacks ปลายด้านบนของกอง ที่เรียกว่า ท๊อปของสแตก (top of stack) คือกองซ้อนทับของ สิ่งของ ของในกองมีลำดับ การเอาของใส่กอง (push) และ การเอาของออกจากกอง (pop) ทำที่ Last In First Out List

4 Stack Data Structure กองทับซ้อน ของของ ของในกองมี ลำดับ  Type อะไรที่เก็บของ หลายชิ้นได้ โดยของที่เก็บมีลำดับ class stack { book item[10]; int top; }; template class stack { T item[MAX]; int top; }; stack s1; stack s2;

5 Basic Operations on stack s C++/C s.push(i) push(s, i) push i onto s i = s.pop() i = pop(s) pop i out off s stack s / init(s) Initialize empty s s.empty() / empty(s) empty s ? i = s.top() i = top(s) look at the top without poping

6 s.push(i) / push(s, i) Check Overflow CBACBA top = 2 1 0 DCBADCBA 32103210 ++ top; item[top] = i; item[++top] = i; template class stack { T item[MAX]; int top; } D top = 3

7 i = s.pop() / i = pop(s) Check Underflow CBACBA top = 2 1 0 BABA 1010 return item[top--]; template class stack { T item[MAX]; int top; } top = 1 popC

8 i = s.top() / i = top(s) CBACBA top = 2 1 0 return item[top]; template class stack { T item[MAX]; int top; }

9 s.empty() ? / empty(s) ? CBACBA top = 2 1 0 1 0 top = -1 return top == -1; template class stack { T item[MAX]; int top; }

10 stack s; / init(s) ? CBACBA top = 2 1 0 1 0 top = -1 stack() : top(-1) { } template class stack { T item[MAX]; int top; }

11 Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (in recursion)

12 Parenthesis Matching Are the parenthesis correct? (a+b-c)*[d+e]/{f*(g+h)} [(a+b-c}*[d+e]/{f*(g+h)} (a+b-c *[d+e]/{f*(g+h)}

13 Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty() ch = non_paren match(open,ch)

14 Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty()

15 Evaluate Postfix Notation Infix Form Prefix Form Postfix Form (Polish notation) a + b + a b a b + a + b * c + a * b ca b c * +

16 6 5 2 3 + 8 * - 3 + * = ? input: 6523 input: + s.push(6) o2=s.pop() =3 s.push(5) o1=s.pop() =25 s.push(2) s.push( 2+3 ) 5 s.push(3)6 SS Evaluate Postfix Notation What ‘s next ? 6523+ 5

17 6 5 2 3 + 8 * - 3 + * = ? input: 6523input: + s.push(6)33=s.pop() s.push(5)22=s.pop()5 s.push(2)5s.push(2+3)5 s.push(3)66 ss input: 88input: * s.push(8)58= s.pop()40 55= s.pop()5 6s.push(5*8)6 input: -input: 3 40= s.pop()s.push(3)3 5= s.pop()-35 s.push(5-40)66 input: +input: * 3= s.pop()-32= s.pop() -35= s.pop()-326= s.pop() s.push(-35+3)6s.push(6*-32) -192

18 Infix to Postfix Conversion a*b+c ===> ab*c+a output stack b c+ *

19 Infix to Postfix Conversion a*b+c ---- > input: stack: output: a*b+ca a*b+c *a a*b+c *ab a*b+c +ab* a*b+c +ab*c a*b+cab*c+ ab*c+

20 Infix to Postfix Conversion a+b*c ---- > input: stack: output: a+b*ca a+b*c +a a+b*c +ab * * a+b*c +abc a+b*cabc*+ abc*+

21 Infix to Postfix Conversion a+b*c-d => input:stack: output: a+b*c-da a+b*c-d +a a+b*c-d +ab * * a+b*c-d +abc a+b*c-d -abc*+ a+b*c-d -abc*+d a+b*c-dabc*+d- abc*+d-

22 Infix to Postfix Conversion a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g +ab * a+b*c-(d/e+f)*g +abc a+b*c-(d/e+f)*g -abc*+ ( a+b*c-(d/e+f)*g -abc*+d / ( a+b*c-(d/e+f)*g -abc*+de abc*+de/f+g*-

23 Infix to Postfix Conversion (cont.) a+b*c-(d/e+f)*g => / ( a+b*c-(d/e+f)*g -abc*+de + ( a+b*c-(d/e+f)*g -abc*+de/f a+b*c-(d/e+f)*g -abc*+de/f+ * a+b*c-(d/e+f)*g -abc*+de/f+g a+b*c-(d/e+f)*g -abc*+de/f+g*- abc*+de/f+g*-


Download ppt "Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,"

Similar presentations


Ads by Google