Stack Data Structure V1.0

6,175 views
5,987 views

Published on

Penjelasan mengenai stack/tumpukan

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
  • Notations Should have more solved examples any how good attempt
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,175
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
333
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Stack Data Structure V1.0

  1. 1. Stack Data Structure By : Imam M Shofi
  2. 2. What is stack? <ul><li>A stack is a limited version of an array. </li></ul><ul><li>New elements, or nodes as they are often called, can be added to a stack and removed from a stack only from one end. </li></ul><ul><li>Access system a stack is referred to as a LIFO structure (Last-In First-Out) </li></ul><ul><li>Some illustrations: </li></ul>stack of satay stack of CDs
  3. 3. Stacks operations <ul><li>Push : adds a new node </li></ul><ul><li>Push(X,S)  add the value X to the TOP of stack </li></ul><ul><li>Pop : removes a node </li></ul><ul><li>Pop(S)  removes the TOP node and returns its value </li></ul><ul><li>IsEmpty : reports whether the stack is empty </li></ul><ul><li>IsEmpty(S)  report whether the stack S is empty </li></ul><ul><li>IsFull : reports whether the stack is full </li></ul><ul><li>IsFull(S)  report whether the stack S is full </li></ul><ul><li>Initialize : creates/initializes the stack </li></ul><ul><li>Initialize(S)  create a new empty stack named S </li></ul><ul><li>Destroy : deletes the contents of the stack (may be implemented by re-initializing the stack) </li></ul><ul><li>Destroy(S)  deletes the contents of the stack S </li></ul>
  4. 4. Illustration/example <ul><li>Operation </li></ul>Stack’s contents TOP value 1. Initialiaze(S) <empty> 0 2. Push(‘a’,S) a 1 3. Push(‘b’,S) a b 2 4. Push(‘c’,S) a b c 3 5. Pop(S) a b 2 6. Push(‘d’,S) a b d 3 7. Push(‘e’,S) a b d e 4 8. Pop(S) a b d 3 9. Pop(S) a b 2 10. Pop(S) a 1
  5. 5. Exercise <ul><li>What would the state of the stack be after the following operations: </li></ul><ul><li>create stack </li></ul><ul><li>push A onto stack </li></ul><ul><li>push F onto stack </li></ul><ul><li>pop item from stack </li></ul><ul><li>push B onto stack </li></ul><ul><li>pop item from stack </li></ul><ul><li>pop item from stack </li></ul><ul><li>Show the state of the stack and the value of each variable after execution of each of the following statements: </li></ul><ul><li>A = 5 B = 3 C = 7 </li></ul>(a) create stack push A onto stack push C*C onto stack pop item from stack and store in B push B+A onto stack pop item from stack and store in A pop item from stack and store in B (b) create stack push B onto stack push C onto stack push A onto stack A=B*C push A+C onto stack pop item from stack and store in A pop item from stack and store in B pop item from stack and store in C
  6. 6. Converting between notations <ul><li>INFIX to PREFIX : (A+B) – (C*D) </li></ul><ul><ul><li>Do the first brace: (A+B), the PREFIX is +AB </li></ul></ul><ul><ul><li>Do the second brace: (C*D), the PREFIX is *CD </li></ul></ul><ul><ul><li>The end is operator -: +AB - *CD , </li></ul></ul><ul><ul><li>the PREFIX is -+AB*CD </li></ul></ul><ul><li>INFIX to POSTFIX : (A+B) – (C*D) </li></ul><ul><ul><li>Do the first brace: (A+B), the POSTFIX is AB+ </li></ul></ul><ul><ul><li>Do the second brace: (C*D), the POSTFIX is CD* </li></ul></ul><ul><ul><li>The end is operator -: AB+ - CD* , </li></ul></ul><ul><ul><li>the PREFIX is AB+CD*- </li></ul></ul><ul><li>PREFIX to INFIX : +/*A B C D </li></ul><ul><ul><li>Find the first operator: *, take 2 operands before the operator ( A and B ), the INFIX is (A*B) </li></ul></ul><ul><ul><li>Find the second operator: /, take 2 operands before the operator ( A*B and C ), the INFIX is ((A*B)/C) </li></ul></ul><ul><ul><li>Find the third operator: +, take 2 operands before the operator ( ((A*B)/C) and D ), the INFIX is ((A*B)/C)+D </li></ul></ul>
  7. 7. Converting between notations(2) <ul><li>PREFIX to POSTFIX : +/*A B C D </li></ul><ul><ul><li>Find the first operator: *, take 2 operands before the operator ( A and B ), the POSTFIX is AB* </li></ul></ul><ul><ul><li>Find the second operator: /, take 2 operands before the operator ( AB* and C ), the POSTFIX is AB*C/ </li></ul></ul><ul><ul><li>Find the third operator: +, take 2 operands before the operator ( AB*C/ and D ), the POSTFIX is AB*C/D+ </li></ul></ul><ul><li>POSTFIX to INFIX : ABCD*/- </li></ul><ul><ul><li>Find the first operator: *, take 2 operands before the operator ( C and D ), the INFIX is (C*D) </li></ul></ul><ul><ul><li>Find the second operator: /, take 2 operands before the operator ( (C*D) and B ), the INFIX is (B/(C*D) </li></ul></ul><ul><ul><li>Find the third operator: -, take 2 operands before the operator ( (B/(C*D) and A ), the INFIX is A – (B/(C*D) </li></ul></ul><ul><li>POSTFIX to PREFIX : ABCD*/- </li></ul><ul><ul><li>Find the first operator: *, take 2 operands before the operator ( C and D ), the PREFIX is *CD </li></ul></ul><ul><ul><li>Find the second operator: /, take 2 operands before the operator ( *CD and B ), the PREFIX is /B*CD </li></ul></ul><ul><ul><li>Find the third operator: -, take 2 operands before the operator ( /B*CD and A ), the PREFIX is -A /B*CD </li></ul></ul>
  8. 8. Exercise: Converting <ul><li>Convert these INFIX to PREFIX and POSTFIX : </li></ul><ul><ul><li>A / B – C / D </li></ul></ul><ul><ul><li>(A + B) ^ 3 – C * D </li></ul></ul><ul><ul><li>A ^ (B + C) </li></ul></ul><ul><li>Convert these PREFIX to INFIX and POSTFIX : </li></ul><ul><ul><li>+ – / A B C ^ D E </li></ul></ul><ul><ul><li>– + D E / X Y </li></ul></ul><ul><ul><li>^ + 2 3 – C D </li></ul></ul><ul><li>Convert these POSTFIX to INFIX and PREFIX : </li></ul><ul><ul><li>A B C + – </li></ul></ul><ul><ul><li>G H + I J / * </li></ul></ul><ul><ul><li>A B ^ C D + – </li></ul></ul>

×