Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.

Similar presentations


Presentation on theme: "Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and."— Presentation transcript:

1

2 Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added.  The last item represents the top of the stack  A stack is a dynamic structure. It changes as elements are added and removed from it.  It is also known as a LIFO (Last In First Out) structure. Example: Pile of plates, Pile of books etc. TOP

3 The common operations associated with a stack are as follows: 1.push: adds a new item on top of a stack. 2.pop: removes the item from the top of a stack 3.isEmpty: Check to see if the stack is empty 4.isFull: Check to see if stack is already full 5.returnTop: Indicate which item is at the top

4 Insert ABCDE A B C D E Delete EDC TOP A B

5 Stack can be implemented in two ways – 1.Contiguous stack – Using array 2.Linked stack – Using pointer and dynamic memory allocation or using linked list

6

7

8 Algorithm Push(STK, Item): This algorithm inserts an element into stack STK. Item is element which is to be inserted. 1.If(STK -> TOP == (SIZE – 1))\\ Check overflow condition 2. Display “Stack is full” 3. Exit 4.STK -> TOP = STK -> TOP + 1 5.STK -> array [STK ->TOP] = Item Complexity = O(1)

9

10 Algorithm Pop(STK): This algorithm returns an element from stack TOP. Item is local variable. 1.If(STK -> TOP == – 1) \\ Check underflow condition 2. Display “Stack is empty” 3. Return NULL 4.Item = STK -> array [STK ->TOP] 5.STK ->TOP = STK ->TOP - 1 6.Return Item Complexity = O(1)

11 856577569 SIZE = 8Push 8, 56, 57, 7, 56, 9 Push 89, 90 5 TOP 7 8565775698990 Push 34 Stack Overflow 4 Times Pop 3 TOP 856577 01234567

12  Size of stack must be known in advance. In real scenario, it is not possible.  Stack growth should not restricted to finite number of elements.

13

14 C code: struct Node{ int INFO; struct Node *NEXT; }STK; STK *TOP; Initialize stack: Algorithm Init_LStack(TOP): This algorithm initialize a stack by creating empty linked list. TOP represents the last item of stack and pointing to empty linked list. 1. TOP = NULL // NULL means stack is empty

15 Algorithm LPush(TOP, Item): This algorithm inserts an element into stack by inserting as first node into linked list. Item is element which is to be inserted. TOP is pointing to last inserted node i.e. first node of linked list. 1.STK Temp 2.Temp = Allocate memory 3.If(Temp == NULL)\\ Check overflow condition 4. Display “Unable to allocate memory” 5. Exit 6.Temp -> INFO = Item 7.Temp ->NEXT = TOP 8.TOP = Temp Complexity = O(1)

16 Push 8, 56, 57 8NULL 1. Push 8 TOP 2. Push 56 8NULL56 TOP 3. Push 57 8NULL 56 57 TOP

17 Algorithm LPop(TOP): This algorithm returns an element from stack TOP. Item is local variable. 1.STK Temp 2.Temp = TOP 3.If(Temp == NULL) \\ Check underflow condition 4. Display “Stack is empty” 5. Return NULL 6.Item = Temp ->INFO 7.TOP = Temp -> NEXT 8.Free Temp 9.Return Item Complexity = O(1)

18 8NULL 3. PopTOP = NULL TOP 1. Pop 8NULL 56 TOP 2. Pop 8NULL 5657 TOP 4. PopUnderflow

19 Algorithm LDispose(TOP): This algorithm deletes stack. 1.STK Temp 2.While(TOP != NULL) \\ Check underflow condition 3. Temp = TOP 4. TOP = TOP ->NEXT 5. Free Temp Complexity = O(n) – n is number of elements in stack

20 Stack is one of the most commonly used data structures.  Recursive Function – function calls itself. Parameter, local variable status is saved in stack and return address where control is to be return is also saved.  Calling Function – Pass the parameter between function and parameter and local variable are stored in stack.  Arithmetic Expression Evaluation  Expression Conversion  Tower of hanoi  Parsing

21 We assume that given expression does not contain any unary operation. For same level operations, precedence are performed from left to right. OperationsPrecedence Exponential (↑)1 Multiplication (*)2 Division (/)2 Modulus (%)2 Addition (+)3 Subtraction (-)3 Left Parenthesis 4 NOTE: Lower the number higher the precedence.

22 An arithmetic expression may be in following forms – 1.Infix – Operator symbol is placed between its two operands like X + Y 2.Prefix – Operator symbol is placed before its two operands like + X Y 3.Postfix – Operator symbol is placed after its two operands like X Y +  In our usual practice, we solve the expressions in infix form. But, for computer program it is very complex to solve an expression in infix form. Therefore, to evaluate arithmetic, first we convert it into prefix or postfix form.

23 Following conversions are possible – 1.Infix to postfix 2.Infix to prefix 3.Postfix to infix 4.Prefix to infix

24 Algorithm Infix_To_Post (InFix) – This algorithm takes infix expression and returns equivalent postfix expression. 1.Create empty stack, say STK 2.While (!(InFix END)) 3. pick up the element from InFix and say it is Current 4. If (Current == left Parenthesis) 5. Push(STK, Current) 6. Else If (Current == right Parenthesis) 7. While (!(isEmpty(STK)) && STK -> TOP != left Parenthesis) 8. Operator = Pop(STK) 9. AppendTo(PostFix, Operator) 10. Pop(STK) // remove the left parenthesis and do not append to PostFix 11. Else If(Current == Operand) 12. AppendTo(PostFix, Current)

25 13. Else If (Current == Operator) 14. While (!(isEmpty(STK)) && Current TOP)) 15. Operator=Pop(STK) 16. AppendTo(PostFix, Operator) 17. Push(STK, Current) 18. Else 19. Display “Incorrect Expression” 20. Exit 21. While (!(isEmpty(STK)) && STK -> TOP != left Parenthesis) 22. Operator = Pop(STK) 23. AppendTo(PostFix, Operator) 24. If (STK -> TOP == left Parenthesis) 25. Display “Incorrect Expression” 26. Exit

26 InFix Expression – (A – B) * (C / D) Current CharacterPostFix ExpressionStack (( AA( -A( - BA B( - )A B -Empty *A B -* ( * ( CA B – C* ( /A B – C* ( / DA B – C D* ( / )A B – C D /* End of expressionA B – C D / *Empty

27 InFix Expression – 1. A + B * C - D / E 2. A * B - ( C + D ) + E 3. A + (B * C – (D / E ↑ F) * G) * H PostFix Expression – 1. A B C * + D E / - 2. A B * C D + - E + 3. A B C * D E F ↑ / G * - H * +

28 Algorithm Infix_To_Pre (InFix) – This algorithm takes infix expression and returns equivalent prefix expression. 1.Create empty stack, say STK 2.RInFix = Reverse(InFix) 3.While (!(RInFix END)) 4. pick up the element from RInFix and say it is Current 5. If (Current == right Parenthesis) 6. Push(STK, Current) 7. Else If (Current == left Parenthesis) 8. While (!(isEmpty(STK)) && STK -> TOP != right Parenthesis) 9. Operator = Pop(STK) 10. AppendTo(PreFix, Operator) 11. Pop(STK) // remove the right parenthesis and do not append to PreFix 12. Else If(Current == Operand) 13. AppendTo(PreFix, Current)

29 13. Else If (Current == Operator) 14. While (!(isEmpty(STK)) && Current TOP)) 15. Operator=Pop(STK) 16. AppendTo(PreFix, Operator) 17. Push(STK, Current) 18. Else 19. Display “Incorrect Expression” 20. Exit 21. While (!(isEmpty(STK)) && STK -> TOP != right Parenthesis) 22. Operator = Pop(STK) 23. AppendTo(PreFix, Operator) 24. If (STK -> TOP == right Parenthesis) 25. Display “Incorrect Expression” 26. Exit 27. PreFix = Reverse(PreFix)

30 InFix Expression – (A – B) * (C / D) = Reverse of ) D / C ( * ) B – A ( Current CharacterPreFix ExpressionStack )) DD) /D) / CD C) / (D C /Empty *D C /* ) * ) BD C / B* ) -D C / B* ) - AD C / B A* ) - (D C / B A -* End of ExpressionD C / B A - *Empty Final PreFix * - A B / C D

31 InFix Expression – 1. A + B * C - D / E 2. A * B - ( C + D ) + E 3. A + (B * C – (D / E ↑ F) * G) * H PreFix Expression – 1. - + A * B C / D E 2. + - * A B + C D E 3. + A * - * B C * / D ↑ E F G H

32 Algorithm Postfix_To_Infix (PostFix) – This algorithm takes postfix expression and returns equivalent infix expression. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.While (!(PostFix END)) 3. pick up the element from PostFix and say it is Current 4. If(Current == Oprator) 5. str2 = Pop(STK) 6. str3 = Pop(STK) 7. str1 = str3 8. Concatenate str1 and Current 9. Concatenate str1 and str2 10. Push(STK, str1) 11. Else 12. Push(STK, Current) 13. PostFix = Pop(STK)

33 PostFix Expression - A B C * D E / - + A BABA CBACBA B * C A D B * C A E D B * C A D / E B * C A B * C - D / E AA + B * C - D / E InFix Expression - A + B * C – D / E

34 Algorithm Prefix_To_Infix (PreFix) – This algorithm takes prefix expression and returns equivalent infix expression. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.RPreFix = Reverse (PreFix) 3.While (!(RPreFix END)) 4. pick up the element from PreFix and say it is Current 5. If(Current == Oprator) 6. str2 = Pop(STK) 7. str3 = Pop(STK) 8. str1 = str3 9. Concatenate str1 and Current 10. Concatenate str1 and str2 11. Push(STK, str1) 12. Else 13. Push(STK, Current) 14.RInFix = Pop(STK) 15.InFix = Reverse(RInFix)

35 PreFix Expression - Reverse of * - A B / C D = D C / B A - * D CDCD D / C B D / C A B D / C B - A D / C D / C * B - A InFix Expression - A – B * C / D

36  We follow same algorithm as for Postfix to infix.  Here, we compute each step value and then push into stack. Example – 2 3 – 1 + 4 3 * + 2 – 3 + 1 + ( 4 * 3) =12 3232 2 – 3 = -1 1 0 4040 340340 12 012

37 PostFix 1.9 9 * 81 2.2 2 * 3 – 3.7 6 2 ^ * 4 / 8 - Infix 9 * 9 – 81 = 0 (2 * 2 ) – 3 = 1 7 * 6 ^ 2 / 4 – 8 = 55

38 Algorithm Prefix_Eval (PreFix) – This algorithm takes prefix expression and returns evaluated result. In this case, we push and pop the strings. str1, str2 and str3 are the temporary variables holding strings. 1.Create empty stack, say STK 2.RPreFix = Reverse (PreFix) 3.While (!(RPreFix END)) 4. pick up the element from PreFix and say it is Current 5. If(Current == Oprator) 6. str2 = Pop(STK) 7. str3 = Pop(STK) 8. str1 = str3 9. Concatenate str2and Current 10. Concatenate str2and str1 11. Push(STK, str1) 12. Else 13. Push(STK, Current) 14.InFix = Pop(STK)

39  Here, we compute each step value and then push into stack. Example – + * 2 2 3 3 2 2 * + 223223 2 * 2 =4 4343 Reverse Infix: (2 * 2) + 3 = 7 7

40 PreFix 1.- * 9 9 81 2.7 6 2 ^ * 4 / 8 – 3.+ + - 2 3 1 * 4 3 Infix 9 * 9 – 81 = 0 7 * 6 ^ 2 / 4 – 8 = 55 2 – 3 + 1 + ( 4 * 3) =12

41  Its is mathematical puzzle.  It consists of three disks or plates (in standard case) of different sizes.  Disks or plates slide round the one rod.  The puzzle was invented by the French mathematician Édouard Lucas in 1883 Task: Shift all the disks from one rod another with following constraint – 1.Only one disk can be move at a time. 2.Only top most disk can moved. 3.No disk can be placed over the smaller disk. Peg A Peg B

42

43

44

45

46

47

48

49

50

51

52

53 Recurrence relation for Tower of Hanoi – T(n) = 1if n = 1 = 2 T(n-1) + 1 n >1 By solving it, Time complexity = O(2 n )

54  Basic operation on stack – 1. Push 2. Pop 3. Dispose  Applications of stack – 1. Evaluation of mathematical expression 2. Conversion of expression from one form to another (Polish Notation) 3. Tower of Hanoi problem


Download ppt "Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and."

Similar presentations


Ads by Google