1 / 80

CSCS-200 Data Structure and Algorithms

CSCS-200 Data Structure and Algorithms. Lecture-14-15. Stacks. Stacks in real life: stack of books, stack of plates Add new items at the top Remove an item at the top Stack data structure similar to real life: collection of elements arranged in a linear order.

droger
Download Presentation

CSCS-200 Data Structure and Algorithms

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. CSCS-200 Data Structure and Algorithms Lecture-14-15

  2. Stacks • Stacks in real life: stack of books, stack of plates • Add new items at the top • Remove an item at the top • Stack data structure similar to real life: collection of elements arranged in a linear order. • Can only access element at the top

  3. Stack Operations • Push(X) – insert X as the top element of the stack • Pop() – remove the top element of the stack and return it. • Top() – return the top element without removing it from the stack.

  4. Stack Operations top 1 top 7 7 top 5 5 5 top 2 2 2 2 push(7) push(2) push(5) push(1) top 21 top top 7 7 7 top 5 5 5 5 2 top 2 2 2 2 21 pop() 1 pop() push(21) 7 pop() 5 pop()

  5. Stack Operation • The last element to go into the stack is the first to come out: LIFO – Last In First Out. • What happens if we call pop() and there is no element? • Have IsEmpty() boolean function that returns true if stack is empty, false otherwise. • Throw StackEmpty exception: advanced C++ concept.

  6. Stack Implementation: Array • Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. • Best case for insert and delete is at the end of the array – no need to shift any elements. • Implement push() and pop() by inserting and deleting at the end of an array.

  7. Stack using an Array top 1 2 5 7 1 7 0 1 2 3 4 5 2 top = 3

  8. Stack using an Array • In case of an array, it is possible that the array may “fill-up” if we push enough elements. • Have a boolean function IsFull() which returns true is stack (array) is full, false otherwise. • We would call this function before calling push(x).

  9. Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; }

  10. Stack Operations with Array int top() { return A[current]; } int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); } • A quick examination shows that all five operations take constant time.

  11. Use of Stack • Example of use: prefix, infix, postfix expressions. • Consider the expression A+B: we think of applying the operator “+” to the operands A and B. • “+” is termed a binary operator: it takes two operands. • Writing the sum as A+B is called the infix form of the expression.

  12. Prefix, Infix, Postfix • Two other ways of writing the expression are + A B prefix A B + postfix • The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands.

  13. Prefix, Infix, Postfix • Consider the infix expression A + B * C • We “know” that multiplication is done before addition. • The expression is interpreted as A + ( B * C ) • Multiplication has precedence over addition.

  14. Prefix, Infix, Postfix • Conversion to postfix A + ( B * C ) infix form

  15. Prefix, Infix, Postfix • Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication

  16. Prefix, Infix, Postfix • Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition

  17. Prefix, Infix, Postfix • Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form

  18. Prefix, Infix, Postfix • Conversion to postfix (A + B ) * C infix form

  19. Prefix, Infix, Postfix • Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition

  20. Prefix, Infix, Postfix • Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication

  21. Prefix, Infix, Postfix • Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form

  22. Precedence of Operators • The five binary operators are: addition, subtraction, multiplication, division and exponentiation. • The order of precedence is (highest to lowest) • Exponentiation  • Multiplication/division *, / • Addition/subtraction +, -

  23. Precedence of Operators • For operators of same precedence, the left-to-right rule applies: A+B+C means (A+B)+C. • For exponentiation, the right-to-left rule applies A  B  C means A  ( B  C )

  24. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * A  B * C – D + E/F A B  C*D – E F/+

  25. Infix to Postfix • Note that the postfix form an expression does not require parenthesis. • Consider ‘4+3*5’ and ‘(4+3)*5’. The parenthesis are not needed in the first but they are necessary in the second. • The postfix forms are: 4+3*5 435*+ (4+3)*5 43+5*

  26. Evaluating Postfix • Each operator in a postfix expression refers to the previous two operands. • Each time we read an operand, we push it on a stack. • When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.

  27. Evaluating Postfix Stack s; while( not end of input ) { e = get next element of input if( e is an operand ) s.push( e ); else { op2 = s.pop(); op1 = s.pop(); value = result of applying operator ‘e’ to op1 and op2; s.push( value ); } } finalresult = s.pop();

  28. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6

  29. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2

  30. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3

  31. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5

  32. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1

  33. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3

  34. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8

  35. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2

  36. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4

  37. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7

  38. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7

  39. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2

  40. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49

  41. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3

  42. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52

  43. Evaluating Postfix Evaluate 6 2 3 + - 3 8 2 / + * 2  3 + Input op1 op2 value stack 6 6 2 6,2 3 6,2,3 + 2 3 5 6,5 - 6 5 1 1 3 6 5 1 1,3 8 6 5 1 1,3,8 2 6 5 1 1,3,8,2 / 8 2 4 1,3,4 + 3 4 7 1,7 * 1 7 7 7 2 1 7 7 7,2  7 2 49 49 3 7 2 49 49,3 + 49 3 52 52

  44. Converting Infix to Postfix • Consider the infix expressions ‘A+B*C’ and ‘ (A+B)*C’. • The postfix versions are ‘ABC*+’ and ‘AB+C*’. • The order of operands in postfix is the same as the infix. • In scanning from left to right, the operand ‘A’ can be inserted into postfix expression.

  45. Converting Infix to Postfix • The ‘+’ cannot be inserted until its second operand has been scanned and inserted. • The ‘+’ has to be stored away until its proper position is found. • When ‘B’ is seen, it is immediately inserted into the postfix expression. • Can the ‘+’ be inserted now? In the case of ‘A+B*C’ cannot because * has precedence.

  46. Converting Infix to Postfix • In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first. • Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators. • Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.

  47. Converting Infix to Postfix • prcd(‘*’,’+’) is TRUE • prcd(‘+’,’+’) is TRUE • prcd(‘+’,’*’) is FALSE • Here is the algorithm that converts infix expression to its postfix form. • The infix expression is without parenthesis.

  48. Converting Infix to Postfix • Stack s; • While( not end of input ) { • c = next input character; • if( c is an operand ) • add c to postfix string; • else { • while( !s.empty() && prcd(s.top(),c) ){ • op = s.pop(); • add op to the postfix string; • } • s.push( c ); • } • while( !s.empty() ) { • op = s.pop(); • add op to postfix string; • }

  49. Converting Infix to Postfix • Example: A + B * C symb postfix stack A A

  50. Converting Infix to Postfix • Example: A + B * C symb postfix stack A A + A +

More Related