8
Stack Data Stack Data Structure Structure By : Imam M Shofi By : Imam M Shofi

Stack Data Structure v10

Embed Size (px)

DESCRIPTION

stackk

Citation preview

Page 1: Stack Data Structure v10

Stack Data StructureStack Data Structure

By : Imam M ShofiBy : Imam M Shofi

Page 2: Stack Data Structure v10

What is stack?What is stack? A stack is a limited version of an A stack is a limited version of an

array. array. New elements, or nodes as they are New elements, or nodes as they are

often called, can be added to a stack often called, can be added to a stack and removed from a stack only from and removed from a stack only from one end. one end.

Access system a stack is referred to Access system a stack is referred to as a LIFO structure (Last-In First-as a LIFO structure (Last-In First-Out)Out)

Some illustrations:Some illustrations:

stack of sataystack of satay stack of CDsstack of CDs

Page 3: Stack Data Structure v10

Stacks operationsStacks operations PushPush : adds a new node: adds a new node

Push(X,S) Push(X,S) add the value X to the TOP of stack add the value X to the TOP of stack PopPop : removes a node: removes a node

Pop(S) Pop(S) removes the TOP node and returns its removes the TOP node and returns its valuevalue

IsEmptyIsEmpty : reports whether the stack is : reports whether the stack is emptyemptyIsEmpty(S) IsEmpty(S) report whether the stack S is empty report whether the stack S is empty

IsFullIsFull : reports whether the stack is full: reports whether the stack is fullIsFull(S) IsFull(S) report whether the stack S is full report whether the stack S is full

InitializeInitialize : creates/initializes the stack: creates/initializes the stackInitialize(S) Initialize(S) create a new empty stack named S create a new empty stack named S

DestroyDestroy : deletes the contents of the : deletes the contents of the stack (may be implemented by re-stack (may be implemented by re-initializing the stack)initializing the stack)Destroy(S) Destroy(S) deletes the contents of the stack S deletes the contents of the stack S

Page 4: Stack Data Structure v10

Illustration/exampleIllustration/exampleOperationOperation Stack’s contentsStack’s contents TOP valueTOP value

1. Initialiaze(S)1. Initialiaze(S) <empty><empty> 00

2. Push(‘a’,S)2. Push(‘a’,S) aa 11

3. Push(‘b’,S)3. Push(‘b’,S) a ba b 224. Push(‘c’,S)4. Push(‘c’,S) a b ca b c 335. Pop(S)5. Pop(S) a ba b 226. Push(‘d’,S)6. Push(‘d’,S) a b da b d 337. Push(‘e’,S)7. Push(‘e’,S) a b d ea b d e 448. Pop(S)8. Pop(S) a b da b d 339. Pop(S)9. Pop(S) a ba b 2210. Pop(S)10. Pop(S) aa 11

Page 5: Stack Data Structure v10

ExerciseExercise What would the state of the stack be after the What would the state of the stack be after the

following operations:following operations:create stackcreate stackpush A onto stackpush A onto stackpush F onto stackpush F onto stackpop item from stackpop item from stackpush B onto stackpush B onto stackpop item from stackpop item from stackpop item from stackpop item from stack

Show the state of the stack and the value of each Show the state of the stack and the value of each variable after execution of each of the following variable after execution of each of the following statements:statements:

A = 5A = 5 B = 3B = 3 C = 7C = 7

(a)(a)create stackcreate stackpush A onto stackpush A onto stackpush C*C onto stackpush C*C onto stackpop item from stack and store in Bpop item from stack and store in Bpush B+A onto stackpush B+A onto stackpop item from stack and store in Apop item from stack and store in Apop item from stack and store in Bpop item from stack and store in B

(b)(b)create stackcreate stackpush B onto stackpush B onto stackpush C onto stackpush C onto stackpush A onto stackpush A onto stackA=B*CA=B*Cpush A+C onto stackpush A+C onto stackpop item from stack and store in Apop item from stack and store in Apop item from stack and store in Bpop item from stack and store in Bpop item from stack and store in Cpop item from stack and store in C

Page 6: Stack Data Structure v10

Converting between notationsConverting between notations INFIX to PREFIX : (A+B) – (C*D)INFIX to PREFIX : (A+B) – (C*D)

Do the first brace: (A+B), the PREFIX is +ABDo the first brace: (A+B), the PREFIX is +AB Do the second brace: (C*D), the PREFIX is *CDDo the second brace: (C*D), the PREFIX is *CD The end is operator -: The end is operator -: +AB+AB - - *CD*CD, ,

the PREFIX is -the PREFIX is -+AB*CD+AB*CD

INFIX to POSTFIX : (A+B) – (C*D)INFIX to POSTFIX : (A+B) – (C*D) Do the first brace: (A+B), the POSTFIX is AB+Do the first brace: (A+B), the POSTFIX is AB+ Do the second brace: (C*D), the POSTFIX is CD*Do the second brace: (C*D), the POSTFIX is CD* The end is operator -: The end is operator -: AB+AB+ - - CD*CD*, ,

the PREFIX is the PREFIX is AB+CD*-AB+CD*-

PREFIX to INFIX : +/*A B C DPREFIX to INFIX : +/*A B C D Find the first operator: *, take 2 operands before the Find the first operator: *, take 2 operands before the

operator (operator (A A andand B B), the INFIX is (A*B)), the INFIX is (A*B) Find the second operator: /, take 2 operands before the Find the second operator: /, take 2 operands before the

operator (operator (A*BA*B and and CC), the INFIX is ((A*B)/C)), the INFIX is ((A*B)/C) Find the third operator: +, take 2 operands before the Find the third operator: +, take 2 operands before the

operator (operator (((A*B)/C)((A*B)/C) and and DD), the INFIX is ((A*B)/C)+D), the INFIX is ((A*B)/C)+D

Page 7: Stack Data Structure v10

Converting between notations(2)Converting between notations(2) PREFIX to POSTFIX : +/*A B C DPREFIX to POSTFIX : +/*A B C D

Find the first operator: *, take 2 operands before the Find the first operator: *, take 2 operands before the operator (operator (A A andand B B), the POSTFIX is AB*), the POSTFIX is AB*

Find the second operator: /, take 2 operands before the Find the second operator: /, take 2 operands before the operator (operator (AB*AB* and and CC), the POSTFIX is AB*C/), the POSTFIX is AB*C/

Find the third operator: +, take 2 operands before the Find the third operator: +, take 2 operands before the operator (operator (AB*C/ AB*C/ and and DD), the POSTFIX is AB*C/D+), the POSTFIX is AB*C/D+

POSTFIX to INFIX : ABCD*/-POSTFIX to INFIX : ABCD*/- Find the first operator: *, take 2 operands before the Find the first operator: *, take 2 operands before the

operator (operator (C C andand D D), the INFIX is (C*D)), the INFIX is (C*D) Find the second operator: /, take 2 operands before the Find the second operator: /, take 2 operands before the

operator (operator ((C*D)(C*D) and and BB), the INFIX is (B/(C*D)), the INFIX is (B/(C*D) Find the third operator: -, take 2 operands before the Find the third operator: -, take 2 operands before the

operator (operator ((B/(C*D)(B/(C*D) and and AA), the INFIX is A – (B/(C*D)), the INFIX is A – (B/(C*D) POSTFIX to PREFIX : ABCD*/-POSTFIX to PREFIX : ABCD*/-

Find the first operator: *, take 2 operands before the Find the first operator: *, take 2 operands before the operator (operator (C C andand D D), the PREFIX is *CD), the PREFIX is *CD

Find the second operator: /, take 2 operands before the Find the second operator: /, take 2 operands before the operator (operator (*CD*CD and and BB), the PREFIX is /B*CD), the PREFIX is /B*CD

Find the third operator: -, take 2 operands before the Find the third operator: -, take 2 operands before the operator (operator (/B*CD/B*CD and and AA), the PREFIX is -A /B*CD), the PREFIX is -A /B*CD

Page 8: Stack Data Structure v10

Exercise: ConvertingExercise: Converting

1.1. Convert these INFIX to PREFIX and POSTFIX :Convert these INFIX to PREFIX and POSTFIX :a)a) A / B – C / DA / B – C / Db)b) (A + B) ^ 3 – C * D(A + B) ^ 3 – C * Dc)c) A ^ (B + C)A ^ (B + C)

2.2. Convert these PREFIX to INFIX and POSTFIX :Convert these PREFIX to INFIX and POSTFIX :a)a) + – / A B C ^ D E+ – / A B C ^ D Eb)b) – – + D E / X Y+ D E / X Yc)c) ^ + 2 3 – C D^ + 2 3 – C D

3.3. Convert these POSTFIX to INFIX and PREFIX :Convert these POSTFIX to INFIX and PREFIX :a)a) A B C + – A B C + – b)b) G H + I J / *G H + I J / *c)c) A B ^ C D + – A B ^ C D + –