1. Trang chủ
  2. » Giáo án - Bài giảng

Bài giảng Cấu trúc dữ liệu_Phần Stack

45 470 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Bài giảng Cấu trúc dữ liệu_Phần Stack
Thể loại Bài giảng
Năm xuất bản 2005
Định dạng
Số trang 45
Dung lượng 1,81 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Copyright © 2005 Pearson Addison-Wesley.. Copyright © 2005 Pearson Addison-Wesley.. All rights reserved.6-3 Stacks • A stack is a linear collection whose elements are added and removed

Trang 1

Chapter 6

Stacks

Trang 2

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-2

Chapter Objectives

• Define a stack abstract data type

solve problems

Trang 3

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

Trang 4

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-4

FIGURE 6.1

A conceptual view of a stack

Trang 5

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-5

FIGURE 6.2

The operations on a stack

Trang 6

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-6

FIGURE 6.3

The StackADT interface in UML

Trang 7

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-7

Listing 6.1

Trang 8

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

Trang 9

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 *

Trang 10

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

Trang 11

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-11

FIGURE 6.4 Using a stack to

evaluate a postfix expression

Trang 12

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-12

Postfix Expressions

• To simplify the example, let's assume the

operands to the expressions are integer

literals

any implementation of a stack would suffice

Trang 13

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-13

Listing 6.2

Trang 14

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-14

Listing 6.2 (cont.)

Trang 15

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-15

Listing 6.3

Trang 16

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-16

Listing 6.3 (cont.)

Trang 17

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-17

Listing 6.3 (cont.)

Trang 18

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-18

Listing 6.3 (cont.)

Trang 19

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-19

Listing 6.3 (cont.)

Trang 20

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-20

FIGURE 6.5 A UML class diagram

for the postfix expression program

Trang 21

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-21

Using Stacks - Traversing a

Maze

• A classic use of a stack is to keep track of

alternatives in maze traversal or other trial

and error algorithms

• Using a stack in this way simulates recursion

– Recursion is when a method calls itself

either directly or indirectly

Trang 22

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-22

Using Stacks - Traversing a

Maze

calls by placing an activation record for each called method on the run-time stack

• When a method completes execution, it is

popped from the stack and control returns to the method that called it

– Which is now the activation record on the top of the stack

Trang 23

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-23

Using Stacks - Traversing a

Maze

• In this manner, we can traverse a maze by

trial and error by using a stack to keep track

of moves that have not yet been tried

Trang 24

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-24

Listing 6.4

Trang 25

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-25

Listing 6.4 (cont.)

Trang 26

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-26

Listing 6.4 (cont.)

Trang 27

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-27

Listing 6.4 (cont.)

Trang 28

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-28

Listing 6.4 (cont.)

Trang 29

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-29

Listing 6.5

Trang 30

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-30

Listing 6.5 (cont.)

Trang 31

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-31

The LinkedStack Class

• Now let's examine a linked implementation of

a stack

used in Chapter 3 to define the linked

implementation of a set collection

• Internally, a stack is represented as a linked list of nodes, with a reference to the top of

the stack and an integer count of the number

of nodes in the stack

Trang 32

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-32

FIGURE 6.6 A linked

implementation of a stack

Trang 33

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-33

LinkedStack - the push

Operation

Trang 34

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-34

FIGURE 6.7 The stack after

pushing element E

Trang 35

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-35

LinkedStack - the pop Operation

Trang 36

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-36

FIGURE 6.8

The stack after a pop operation

Trang 37

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-37

The ArrayStack Class

• Now let's examine an array-based

implementation of a stack

• We'll make the following design decisions:

– maintain an array of Object references

– the bottom of the stack is at index 0

– the elements of the stack are in order and contiguous

– an integer variable top stores the index of the next available slot in the array

• This approach allows the stack to grow and

shrink at the higher indexes

Trang 38

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-38

FIGURE 6.9 An array

implementation of a stack

Trang 39

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-39

ArrayStack - the push Operation

// Adds the specified element to the top of the stack, expanding // the capacity of the stack array if necessary.

Trang 40

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-40

FIGURE 6.10

The stack after pushing element E

Trang 41

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-41

ArrayStack - the pop Operation

Trang 42

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-42

FIGURE 6.11 The stack after

popping the top element

Trang 43

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-43

The java.util.Stack Class

• The Java Collections framework defines a

Stack class with similar operations

• It is derived from the Vector class and

therefore has some characteristics that are not appropriate for a pure stack

around since the original version of Java,

and has been retrofitted to meld with the

Collections framework

Trang 44

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-44

FIGURE 6.12 A UML description

of the java.util.Stack class

Trang 45

Copyright © 2005 Pearson Addison-Wesley All rights reserved.

6-45

Analysis of Stack Operations

of the collection, they are generally efficient

and array implementations, are O(1)

implementations are O(1)

have that characteristic) aren't as efficient for all operations

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

TỪ KHÓA LIÊN QUAN

w