Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)

Similar presentations


Presentation on theme: "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)"— Presentation transcript:

1 C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)

2 C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 Linked Implementation of Stacks Array only allows fixed number of elements If number of elements to be pushed exceeds array size −Program may terminate Linked lists can dynamically organize data In a linked representation, stackTop is pointer to top element in stack

3

4

5

6

7 C++ Programming: From Problem Analysis to Program Design, Fourth Edition7 Default Constructor Initializes the stack to an empty state when a stack object is declared −Sets stackTop to NULL

8 C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Empty Stack and Full Stack In the linked implementation of stacks, the function isFullStack does not apply −Logically, the stack is never full

9 C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Initialize Stack

10 C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Push The newElement is added at the beginning of the linked list pointed to by stackTop

11 C++ Programming: From Problem Analysis to Program Design, Fourth Edition11 Push (continued)

12 C++ Programming: From Problem Analysis to Program Design, Fourth Edition12 Push (continued)

13 C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Push (continued)

14 C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Push (continued) We do not need to check whether the stack is full before we push an element onto the stack

15 C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Return the Top Element

16 C++ Programming: From Problem Analysis to Program Design, Fourth Edition16. Pop Node pointed to by stackTop is removed

17

18 C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 Pop (continued)

19 C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Copy Stack

20 C++ Programming: From Problem Analysis to Program Design, Fourth Edition20 Copy Stack (continued) Notice that this function is similar to the definition of copyList for linked lists

21 C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 Constructors and Destructors

22 C++ Programming: From Problem Analysis to Program Design, Fourth Edition22 Overloading the Assignment Operator (=)

23 C++ Programming: From Problem Analysis to Program Design, Fourth Edition23 Stack as Derived from the class unorderedLinkedList Our implementation of push is similar to insertFirst (discussed for general lists) −Other functions are similar too: initializeStack and initializeList isEmptyList and isEmptyStack linkedStackType can be derived from linkedListType − class linkedListType is abstract Must implement pop as described earlier

24 C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Stack as Derived from the class unorderedLinkedList (cont.) unorderedLinkedListType is derived from linkedListType −Provides the definitions of the abstract functions of the class linkedListType We can derive the linkedStackType from unorderedLinkedListType

25 C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 Application of Stacks: Postfix Expressions Calculator Infix notation: usual notation for writing arithmetic expressions −The operator is written between the operands −Example: a + b −The operators have precedence Parentheses can be used to override precedence

26 C++ Programming: From Problem Analysis to Program Design, Fourth Edition26 Application of Stacks: Postfix Expressions Calculator (continued) Prefix (Polish) notation: the operators are written before the operands −Introduced by the Polish mathematician Jan Lukasiewicz Early 1920s −The parentheses can be omitted −Example: + a b

27 C++ Programming: From Problem Analysis to Program Design, Fourth Edition27 Application of Stacks: Postfix Expressions Calculator (continued) Reverse Polish notation: the operators follow the operands (postfix operators) −Proposed by the Australian philosopher and early computer scientist Charles L. Hamblin Late 1950's −Advantage: the operators appear in the order required for computation −Example: a + b * c In a postfix expression: a b c * +

28 C++ Programming: From Problem Analysis to Program Design, Fourth Edition28 Application of Stacks: Postfix Expressions Calculator (continued)

29 C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 Application of Stacks: Postfix Expressions Calculator (continued) Postfix notation has important applications in computer science −Many compilers first translate arithmetic expressions into postfix notation and then translate this expression into machine code Evaluation algorithm: −Scan expression from left to right −When an operator is found, back up to get the operands, perform the operation, and continue

30 C++ Programming: From Problem Analysis to Program Design, Fourth Edition30 Application of Stacks: Postfix Expressions Calculator (continued) Example: 6 3 + 2 * = −Read first symbol 6 is a number  push it onto the stack −Read next symbol 3 is a number  push it onto the stack

31 C++ Programming: From Problem Analysis to Program Design, Fourth Edition31 Application of Stacks: Postfix Expressions Calculator (continued) Example: 6 3 + 2 * = −Read next symbol + is an operator (two operands)  pop stack twice, perform operation, put result back onto stack

32 C++ Programming: From Problem Analysis to Program Design, Fourth Edition32 Example: 6 3 + 2 * = −Read next symbol 2 is a number  push it onto the stack Application of Stacks: Postfix Expressions Calculator (continued)

33 C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 Example: 6 3 + 2 * = −Read next symbol * is an operator  pop stack twice, perform operation, put result back onto stack Application of Stacks: Postfix Expressions Calculator (continued)

34 C++ Programming: From Problem Analysis to Program Design, Fourth Edition34 Example: 6 3 + 2 * = −Read next symbol = is an operator  indicates end of expression Print the result (pop stack first) Application of Stacks: Postfix Expressions Calculator (continued)

35 C++ Programming: From Problem Analysis to Program Design, Fourth Edition35 Application of Stacks: Postfix Expressions Calculator (continued) Symbols can be numbers or anything else: −+, -, * and / are operators Pop stack twice and evaluate expression If stack has less than two elements  error −If symbol is =, the expression ends Pop and print answer from stack If stack has more than one element  error −If symbol is anything else Expression contains an illegal operator

36 C++ Programming: From Problem Analysis to Program Design, Fourth Edition36 Application of Stacks: Postfix Expressions Calculator (continued) Examples: 7 6 + 3 ; 6 - = ; is an illegal operator 14 + 2 3 * = Does not have enough operands for + 14 2 3 + = Error: stack will have two elements when we encounter equal ( = ) sign

37 C++ Programming: From Problem Analysis to Program Design, Fourth Edition37 Application of Stacks: Postfix Expressions Calculator (continued) We assume that the postfix expressions are in the following form: #6 #3 + #2 * = −If symbol scanned is #, next input is a number −If the symbol scanned is not #, then it is: An operator (may be illegal) or An equal sign (end of expression) We assume expressions contain only +, -, *, and / operators

38 C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Main Algorithm Pseudocode: We will write four functions: − evaluateExpression, evaluateOpr, discardExp, and printResult

39 C++ Programming: From Problem Analysis to Program Design, Fourth Edition39 Function evaluateExpression

40 C++ Programming: From Problem Analysis to Program Design, Fourth Edition40 Function evaluateOpr

41 C++ Programming: From Problem Analysis to Program Design, Fourth Edition41

42 C++ Programming: From Problem Analysis to Program Design, Fourth Edition42 Function discardExp This function is called whenever an error is discovered in the expression

43 C++ Programming: From Problem Analysis to Program Design, Fourth Edition43 Function printResult If the postfix expression contains no errors, the function printResult prints the result −Otherwise, it outputs an appropriate message The result of the expression is in the stack and the output is sent to a file

44 C++ Programming: From Problem Analysis to Program Design, Fourth Edition44

45 C++ Programming: From Problem Analysis to Program Design, Fourth Edition45 Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward To print the list backward, first we need to get to the last node of the list −Problem: how do we get back to previous node? Links go in only one direction −Solution: save a pointer to each of the nodes with info 5, 10, and 15 Use a stack (LIFO)

46 C++ Programming: From Problem Analysis to Program Design, Fourth Edition46

47 C++ Programming: From Problem Analysis to Program Design, Fourth Edition47

48 C++ Programming: From Problem Analysis to Program Design, Fourth Edition48 Let us now execute the following statements: Output: 20 15 10 5 Removing Recursion (continued) Linked Implementation of Stacks

49 C++ Programming: From Problem Analysis to Program Design, Fourth Edition49 Queues (This topic continues in file 02096_PPT_ch19-3.ppt )


Download ppt "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)"

Similar presentations


Ads by Google