1 / 44

Stack

Stack. Any Other Data Structure. Array. Linked List. Elements are stored in continuous / contiguous memory locations. Elements are stored in non-continuous / discontinuous memory locations. Is there any other option to store elements in memory?. No.

lrickey
Télécharger la présentation

Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack Any Other Data Structure Array Linked List Elements are stored in continuous / contiguous memory locations. Elements are stored in non-continuous / discontinuous memory locations. Is there any other option to store elements in memory? No. Any other data structure can be implemented: 1) Either as an Array, (If it stores elements in continuous memory locations) 2) Or as a Linked List. (If it stores elements in discontinuous memory locations)

  2. Stack Examples Stack of Plates Stack of Books Box of Tennis Balls (Closed at 1 end) Stack of Clothes. Trains in a railway yard. Goods in a cargo. Type: LIFO / FILO Stack of Rings / Discs Stack of Chairs

  3. Stack Stack: Collection of, Homogeneous data elements where, Insertion and deletion take place, At one end only. Also called, LIFO structure. Last In First Out. FILO structure. First In Last Out. Stack can be implemented, Either as an Array, Stores the elements in continuous memory locations and, Has all advantages/disadvantages of array. Or as a Linked List. Stores the elements in discontinuous memory locations and, Has all advantages/disadvantages of linked list.

  4. Implemented using an Array Stack PUSH operation Insert an Element (Stack Overflow) 15 35 25 55 45 75 POP operation (Stack Underflow) Delete an Element 45 5 45 Delete an Element 55 4 55 Delete an Element 25 3 25 2 15 Delete an Element 15 1 35 75 Delete an Element 35 Delete an Element NULL Array A

  5. Implemented using a Linked List Stack 35 PUSH 25 45 45 Stack_Head 25 35 NULL NULL NULL 1st solution 2nd solution 45 POP PUSH: InsertFront PUSH: InsertEnd POP: DeleteFront POP: DeleteEnd 25 POP 35 POP (Stack Underflow) NULL POP

  6. Stack Stack: Insertion operation in a Stack is called: PUSH When PUSH is not possible, situation is called: Stack Overflow Deletion operation in a Stack is called: POP When POP is not possible, situation is called: Stack Underflow

  7. Stack using Array PUSH an element 25 15 35 65 55 Array A POP 55 5 55 POP 65 4 65 POP 35 POP 15 3 35 2 15 Is this logic efficient? No 1 25 Inefficient for a bigger array.

  8. Stack using Array PUSH an element Item 25 15 35 65 55 75 Array A Steps: U If(Top >= 5) Top 5 55 print “Stack overflow.” 4 65 Top Else 3 35 Top Top = Top + 1 2 15 Top Item A[Top] = 25 EndIf 1 25 Top Top = 0

  9. Stack using Array POP POP POP POP POP POP 55 65 35 15 25 NULL Steps: Array A Item = Null L If(Top < 1) Top 5 55 print “Stack underflow.” 4 65 Top Else 3 35 Top Item = A[Top] // 55 65 35 15 25 2 15 Top A[Top] = Null Top = Top - 1 1 25 Top EndIf Top = 0 Return(Item)

  10. Stack using Array POP an element PUSH an element Steps: Steps: Item = Null If(Top >= U) If(Top < L) print “Stack underflow.” print “Stack overflow.” Else Else Item = A[Top] Top = Top + 1 A[Top] = Null A[Top] = Item Top = Top - 1 EndIf EndIf Return(Item)

  11. Tracing of PUSH and POP on Stack using Array. PUSH 3 If(Top >= U) 2 1) POP() print “Stack overflow.” 1 Else 0 Top = Top + 1 Top = -1 A[Top] = Item Stack underflow. Item = NULL EndIf Array A POP Item = Null 3 2) PUSH(20) 3 2 If(Top < L) 1 print “Stack underflow.” 2 20 Top 0 Else 1 Item = A[Top] Top 0 20 3) PUSH(10) A[Top] = Null 4) PUSH(50) Top = -1 Top = Top - 1 5) PUSH(70) EndIf L = 0, U = 3 6) PUSH(80) Return(Item) 7) POP()

  12. Stack using Array Algorithm: Push_Array Input: Top: Pointer to the Top of Stack. Item: Data to be pushed onto the Stack. Output: Stack with newly pushed item at Top if successful, message otherwise. Data Structure: Stack implemented using Array A[L...U] with TOP as the pointer.

  13. Algorithm: Push_Array Steps: If(Top >= U) print “Stack overflow.” Else Top = Top + 1 A[Top] = Item EndIf Stop

  14. Stack using Array Algorithm: Pop_Array Input: Top: Pointer to the Top of Stack. Output: Item: Data removed (popped) from Top of Stack if successful, NULL and message otherwise. Data Structure: Stack implemented using Array A[L...U] with Top as the pointer.

  15. Steps: Item = Null If(Top < L) print “Stack underflow.” Else Item = A[Top] A[Top] = Null Top = Top - 1 EndIf Return(Item) Stop Algorithm: Pop_Array

  16. Stack using Array Just look and tell the value of element on Top of Stack. Do not POP the element. PEEP PEEP PEEP POP 35 65 65 65 Steps: Array A Item = Null If(Top < L) 5 print “Stack underflow.” 4 65 Top Else 3 35 Top Item = A[Top] // 65 35 2 15 A[Top] = Null Top = Top - 1 1 25 EndIf Return(Item)

  17. Stack using Array Algorithm: Peep_Array Input: Top: Pointer to the Top of Stack. Output: Item: Data returned (not deleted) from Top of Stack if successful, message otherwise. Data Structure: Stack implemented using Array A[L...U] with Top as the pointer.

  18. Steps: Item = Null If(Top < L) print “Stack underflow.” Else Item = A[Top] EndIf Return(Item)Stop Algorithm: Peep_Array

  19. PUSH an element Stack using Linked List Top Item 55 65 35 new 35 3000 4000 Top Top Stack_Head 65 2000 55 NULL NULL NULL 3000 4000 3000 Top 2000 1000 NULL Steps: Steps: (cntd) new->Data = 35 new = GetNode(NODE) new->Link = Top If(new == NULL) Stack_Head->Link = new print “Memory Insufficient. PUSH not possible.” Top = new EndIf Else Stop

  20. POP an element Stack using Linked List 35 Top Stack_Head Top ptr 65 55 2000 35 NULL 3000 NULL 4000 3000 3000 2000 1000 4000 Steps: Item = Top->Data ptr = Top->Link Stack_Head->Link = ptr ReturnNode(Top) Top = ptr Return(Item) Stop

  21. POP POP Stack using Linked List Steps: 55 NULL Item = NULL If(Top == NULL) print “Stack Underflow.” Top Stack_Head ptr Else 55 NULL NULL NULL 2000 NULL Item = Top->Data 2000 1000 Top ptr = Top->Link NULL Stack_Head->Link = ptr ReturnNode(Top) Top = ptr EndIf Return(Item) Stop

  22. PUSH an element POP an element Stack using Linked List Trace the following steps on an empty Stack implemented using a Linked List: Steps: Steps: new = GetNode(NODE) Item = NULL If(new == NULL) If(Top == NULL) print “Memory Insufficient. PUSH not possible.” print “Stack Underflow.” Else Else • PUSH(20) • POP() • PUSH(10) • PUSH(30) • POP() • POP() • POP() Item = Top->Data new->Data = Item ptr = Top->Link new->Link = Top Stack_Head->Link = ptr Stack_Head->Link = new ReturnNode(Top) Top = new Top = ptr EndIf EndIf Convert into a program. Return(Item) Stop Stop

  23. Stack using Linked List Algorithm: Push_LL Input: Stack_Head: Pointer to the starting of linked list. Top: Pointer to the Top of Stack. Item: Data to be pushed onto the Stack. Output: Stack with newly pushed item at Top if successful, message otherwise. Data Structure: Stack implemented using Single Linked List with, Stack_Head as pointer to the starting of linked list and, Top as the pointer to the Top of Stack.

  24. Algorithm: Push_LL Steps: new = GetNode(NODE) If(new == NULL) print “Memory Insufficient. Push not possible.” Else new->Data = Item new->Link = Top Stack_Head->Link = new Top = new EndIf Stop

  25. Stack using Linked List Algorithm: Pop_LL Input: Stack_Head: Pointer to the starting of linked list. Top: Pointer to the Top of Stack. Output: Item: Element removed (popped) from the Top of Stack if successful, message otherwise. Data Structure: Stack implemented using Single Linked List with, Stack_Head as pointer to the starting of linked list and, Top as the pointer to the Top of Stack.

  26. Algorithm: Pop_LL Steps: Item = NULL If(Top == NULL) print “Stack Underflow.” Else Item = Top->Data ptr = Top->Link Stack_Head->Link = ptr ReturnNode(Top) Top = ptr EndIf Return(Item) Stop

  27. Applications of Stack Uses of Stack: Main uses of Stack are: Evaluation of Arithmetic Expression Example: ( A + B ) * C or ( 1 + 2 ) * 3 Execute recursive function calls / Recursion.

  28. Applications of Stack Evaluation of Arithmetic Expression using a Stack How can it be solved by a computer? A + B E = 2 + 3 [A = 2, B = 3] Scanning the input once from Left to Right and doing appropriate processing. A + B - C E = 1 + 2 - 3 [A = 1, B = 2, C = 3] Scanning the input once from Left to Right and doing appropriate processing. A + B * C E = 1 + 2 * 3 [A = 1, B = 2, C = 3] Requires scanning of the input multiple times from Left to Right. A – ( ( B + C ) * D ) E = 1 – ( ( 2 + 3 ) * 4 ) [A = 1, B = 2, C = 3, D = 4] Requires scanning of the input several times from Left to Right.

  29. Applications of Stack Evaluation of Arithmetic Expression using a Stack Right Parenthesis / Bracket Symbols Operators A – ( ( B + C ) * D ) E = Evaluation depends on, Precedence / Priority: Left Parenthesis / Bracket 1) Operators in Brackets () Operands 2) ^ (Power / Exponentiation) 3) *, / 4) +, -

  30. Applications of Stack Evaluation of Arithmetic Expression using a Stack: Process consist of 2 steps: 1) Convert the given arithmetic expression into a special notation/representation using a Stack. Algorithm would be required for this step. 2) Evaluate (Find the value of) that special notation (obtained from step-1) again using a Stack. Algorithm would be required for this step.

  31. Evaluation of Arithmetic Expression using a Stack Step-1: Convert expression into special notation using a Stack. Input Expression: A + B Enclose / Put the input in Parentheses (if not already enclosed) So input Expression becomes: Infix Expression ( A + B ) Step Symbol Stack Output Expression T T ( ( 1 T A A 2 ( T A 3 + ( + Postfix Expression T B A ( 4 B + A B + 5 ) -

  32. Stack should be empty at the end of the conversion. Postfix expression never contains parentheses. Infix Expression: A * B + C Infix to Postfix Infix Expression enclosed in parentheses: ( A * B + C ) Step Symbol Stack Output Expression T T ( 1 ( T A A 2 ( T A ( 3 * * T B 4 B A ( * T A 5 + B * ( + T ( A C + B * 6 C A B * ) C + 7

  33. Infix to Postfix Step Symbol Stack Output Expression Infix Expression ( 1 ( ((A + B) * (C – D)) ( 2 ( ( Already enclosed in parentheses. A 3 ( ( A + 4 ( ( + A B 5 ( ( + A B ) A B + 6 ( * 7 ( * A B + ( 8 ( * ( A B + C 9 ( * ( A B + C - 10 ( * ( - A B + C A B + C D D 11 ( * ( - A B + C D - ) 12 ( * ) 13 A B + C D - *

  34. Infix to Postfix Infix Expression A + B * C ^ D Infix Expression enclosed in parentheses (A + B * C ^ D) Step Symbol Stack Output Expression ( 1 ( A 2 ( A + 3 ( + A B 4 ( + A B * 5 ( + * A B C A B C 6 ( + * ^ 7 ( + * ^ A B C D 8 ( + * ^ A B C D ) 9 A B C D ^ * +

  35. Infix to Postfix Infix Expression A + B / C - D Infix Expression enclosed in parentheses (A + B / C - D) Step Symbol Stack Output Expression ( 1 ( A 2 ( A + 3 ( + A B 4 ( + A B / 5 ( + / A B C A B C 6 ( + / - 7 ( - A B C / + D 8 ( - A B C / + D ) 9 A B C / + D -

  36. Infix to Postfix Convert the following arithmetic expressions from Infix to Postfix. ( A + B ) ^ C – ( D * E ) / F ( A + ( ( B ^ C ) – D ) ) * ( E – ( A / C ) ) A + ( B * C ) / D ( A + B ) * C A * B ^ C + D

  37. Evaluation of Arithmetic Expression using a Stack Step-2: Evaluate (Find the value of) special notation (Postfix) using Stack. Step-1 Infix Expression: A + B Postfix Expression: A B + Values: A = 3, B = 5 Postfix Expression: 3 5 + A B + 3 5 + Operator Operands 3 5 + Push(3) Push(5) y = Pop() // y = 5 1) 2) 3) x = Pop() // x = 3 5 result = x operator y // result = 3 + 5 = 8 Push(result) 3 3 8

  38. Evaluation of Arithmetic Expression using a Stack Step-2: Evaluate (Find the value of) special notation (Postfix) using Stack. 3 5 + 3 5 + Push(3) Push(5) y = Pop() // y = 5 1) 2) 3) x = Pop() // x = 3 5 result = x operator y // result = 3 + 5 = 8 Push(result) 3 3 8 Step Symbol Stack Operation 3 Push(3) 1 3 3, 5 Push(5) 2 5 y = Pop() // 5, x = Pop() // 3, 3 + 8 result = x operator y // 3+5, Push(result)

  39. Step-1 Infix Expression: A * B + C Postfix Expression: A B * C + Evaluate Postfix Values: A=3, B=5, C=5 Postfix Expression: 3 5 * 5 + A B * C + 3 5 * 5 + Step Symbol Stack Operation 3 Push(3) 1 3 3, 5 Push(5) 2 5 y = Pop() // 5, x = Pop() // 3, 3 * 15 Push(result) result = x operator y // 3*5, 4 15, 5 Push(5) 5 y = Pop() // 5, x = Pop() // 15, 5 + 20 Push(result) result = x operator y // 15+5,

  40. Step-1 Infix: ( (A+B) * (C-D) ) Postfix: A B + C D -* [A=1, B=2, C=5, D=3] Evaluate Postfix 1 2 + 5 3 - * Step Symbol Stack Operation 1 Push(1) 1 1 1, 2 Push(2) 2 2 y = Pop() // 2, x = Pop() // 1, 3 + 3 Push(result) result = x operator y // 1+2, 4 3, 5 Push(5) 5 5 3, 5, 3 Push(3) 3 y = Pop() // 3, x = Pop() // 5, 6 - 3, 2 Push(result) result = x operator y // 5-3, y = Pop() // 2, x = Pop() // 3, 7 * 6 Push(result) result = x operator y // 3*2,

  41. Evaluation of Arithmetic Expression using a Stack Exercise: Evaluate the following Arithmetic Expression using a Stack: ( A * B ) - ( C / D ) [ A = 1, B = 3, C = 8, D = 2 ] Steps: 1) Infix to Postfix A B * C D / - -1 2) Evaluate Postfix

  42. Algorithm: InfixToPostfix Algorithm-InfixToPostfix: Enclose the entire expression in parentheses (brackets) if not already enclosed. Add ‘(‘ at the beginning and ‘)’ at the end of the expression. Scan 1 symbol at a time from left to right and do the following things depending on the type of symbol scanned: If symbol is ‘(‘: Push it onto the stack. If symbol is ‘Operand’: Add/Append it to the Output Expression/Output String/Postfix Expression. If symbol is ‘Operator’: Pop all the operators one by one from TOP of stack which have, The same or higher precedence than the symbol and go on, Adding them to the Output String. Then push the incoming operator onto the stack. If symbol is ‘)’: Pop all the operators one by one from TOP of stack and go on adding them to the Output String until, ‘(‘ is encountered / obtained. Remove that ‘(‘ from the Stack and do not do anything with ‘)’.

  43. Steps: symbol = E.ReadSymbol() While(symbol != NULL) case: symbol = ‘(‘ case: symbol = operand case: symbol = operator case: symbol = ‘)’ case: otherwise print “Invalid input.” symbol = E.ReadSymbol() EndWhile Push(symbol) Output(symbol) Algorithm: InfixToPostfix item = Pop() While( item is operator ) AND (PREC(item) >= PREC(symbol)) Output(item) item = Pop() EndWhile Push(item) Push(symbol) item = Pop() While( item != ‘(’ ) Output(item) item = Pop() EndWhile

  44. Steps: symbol = E.ReadSymbol() While(symbol != NULL) case: symbol = operand Push(symbol) case: symbol = operator y = Pop() x = Pop() result = x symbol y Push(result) case: otherwise print “Invalid input.” symbol = E.ReadSymbol() EndWhile value = Pop() Return(value) Stop Algorithm: EvaluatePostfix

More Related