Stack.ppt

45 850 0
Stack.ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 6 Stacks Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives • Examine stack processing • Define a stack abstract data type • Demonstrate how a stack can be used to solve problems • Examine various stack implementations • Compare stack implementations Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-3 Stacks • A stack is a linear collection whose elements are added and removed from one end • A stack is LIFO – last in, first out • The last element to be put on the stack is the first element to be removed • A stack is usually depicted vertically, with additions and deletions occurring at the top of the stack Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-4 FIGURE 6.1 A conceptual view of a stack Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-5 FIGURE 6.2 The operations on a stack Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-6 FIGURE 6.3 The StackADT interface in UML Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-7 Listing 6.1 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-8 Using Stacks • Stacks are particularly helpful when solving certain types of problems • Consider the undo operation in an application – keeps track of the most recent operations in reverse order Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-9 Postfix Expressions • Let's examine a program that uses a stack to evaluate postfix expressions • In a postfix expression, the operator comes after its two operands • We generally use infix notation, with parentheses to force precedence: (3 + 4) * 2 • In postfix notation, this would be written 3 4 + 2 * Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-10 Postfix Expressions • To evaluate a postfix expression: – scan from left to right, determining if the next token is an operator or operand – if it is an operand, push it on the stack – if it is an operator, pop the stack twice to get the two operands, perform the operation, and push the result onto the stack • At the end, there will be one value on the stack, which is the value of the expression . Chapter 6 Stacks Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives • Examine stack processing • Define a stack abstract data type • Demonstrate how a stack can. solve problems • Examine various stack implementations • Compare stack implementations Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-3 Stacks • A stack is a linear collection. on the stack – if it is an operator, pop the stack twice to get the two operands, perform the operation, and push the result onto the stack • At the end, there will be one value on the stack,

Ngày đăng: 16/07/2014, 01:00

Từ khóa liên quan

Mục lục

  • Chapter 6

  • Chapter Objectives

  • Stacks

  • FIGURE 6.1 A conceptual view of a stack

  • FIGURE 6.2 The operations on a stack

  • FIGURE 6.3 The StackADT interface in UML

  • Listing 6.1

  • Using Stacks

  • Postfix Expressions

  • Slide 10

  • FIGURE 6.4 Using a stack to evaluate a postfix expression

  • Slide 12

  • Listing 6.2

  • Listing 6.2 (cont.)

  • Listing 6.3

  • Listing 6.3 (cont.)

  • Slide 17

  • Slide 18

  • Slide 19

  • FIGURE 6.5 A UML class diagram for the postfix expression program

Tài liệu cùng người dùng

Tài liệu liên quan