stack adt
play

Stack ADT Tiziana Ligorio 1 Todays Plan Questons? Stack ADT 2 - PowerPoint PPT Presentation

Stack ADT Tiziana Ligorio 1 Todays Plan Questons? Stack ADT 2 Abstract Data Types Bag List Stack 3 Stack 34 A data structure representing a stack of things Objects can be pushed onto the stack or popped from the stack


  1. Evaluating Postfix Expressions for(char ch : st) 
 { if ch is an operand 
 push it on the stack else // ch is an operator op 
 { 
 //evaluate and push the result operand2 = pop stack 
 operand1 = pop stack 
 result = operand1 op operand2 
 push result on stack } 
 } � 81

  2. Lecture Activity Describe an algorithm that translates the infix expression below into postfix (you can use drawings to explain): Infix: Postfix: 2 * (3 + 4) 2 3 4 + * Hint : use 2 stacks, one for operators and parentheses another one for the operands and postfix expression. Once converted use the empty stack to invert the order � 82

  3. Infix: Postfix: 2 * (3 + 4) 2 3 4 + * 3. Push everything onto empty 1. Read characters onto 2. Pop operator stack and push stack to invert it onto postfix stack ignoring ‘(‘ corresponding stack until ‘)’ Then read pop and print. 2 4 + * ( 3 + 3 4 * 4 2 3 + * 2 Postfix Operator Postfix Operator Postfix Operator Stack Stack Stack Stack Stack Stack � 83

  4. Infix: Postfix: (2 * 3 )+ 4 2 3 *4 + 2. If reading a ‘)’, move operators 3. Keep reading 4. Move operators to 1. Read characters to Postfix Stack until a ‘(‘ until ‘)’ -> 2. onto corr. stack until ‘)’ Postfix Stack discard it and continue reading string or end of string -> 4. or end of string + + * 3 * 4 4 ( 3 2 * * 3 2 3 2 2 Operator Postfix Operator Postfix Operator Postfix Operator Postfix Stack Stack Stack Stack Stack Stack Stack Stack � 84

  5. Infix: Postfix: (2 * 3 )+ 4 2 3 *4 + 4. Move operators to 5. Pop and push onto empty stack to invert, then print Postfix Stack + 2 4 3 * * 3 4 2 + Operator Postfix Operator Postfix Stack Stack Stack Stack

  6. Search a Flight Map Fly from Origin to Destination following map 1. Reach destination 2. Reach city with no departing flights (dead end) 3. Go in circles forever � 86

  7. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P � 87

  8. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R � 88

  9. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 89

  10. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 90

  11. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 91

  12. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R X � 92

  13. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X � 93

  14. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S � 94

  15. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 95

  16. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 96

  17. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 97

  18. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S T � 98

  19. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S Y T � 99

  20. Backtracking C = visited Avoid dead end by backtracking C = backtracked Avoid traveling in circles by marking visited cities Origin = P , Destination = Z P R W X S Y T Z � 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend