59
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS

DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Embed Size (px)

Citation preview

Page 1: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

DATA STRUCTURE & ALGORITHMS

CHAPTER 3: STACKS

Page 2: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

2

ObjectivesIn this chapter, you will:• Learn about stacks• Examine various stack operations• Discover stack applications

Page 3: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

3

Stacks• Stack: list of homogenous elements

– Addition and deletion occur only at one end, called the top of the stack• Example: in a cafeteria, the second tray can be

removed only if first tray has been removed

– Last in first out (LIFO) data structure• Operations:

– Push: to add an element onto the stack– Pop: to remove an element from the stack

Page 4: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

4

Stacks (continued)

Page 5: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 6: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

6

Stack Operations• In the abstract class stackADT:

– initializeStack– isEmptyStack– isFullStack– push– top– pop

Page 7: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Stack ApplicationStack applications classified into 4 broad categories: • Reversing data – e.g. reverse a list & convert decimal

to binary.– Eg. 26 = 110102

• Parsing data – e.g. translate a source program to machine language.

• Postponing data usage – e.g. evaluation, transformation.

• Backtracking – e.g. computer gaming, decision analysis, expert systems.

Page 8: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

8

Implementation of Stacks as Arrays

• First element can go in first array position, the second in the second position, etc.

• The top of the stack is the index of the last element added to the stack

• Stack elements are stored in an array• Stack element is accessed only through top• To keep track of the top position, use a

variable called stackTop

Page 9: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

9

Implementation of Stacks as Arrays

• Because stack is homogeneous– You can use an array to implement a stack

• Can dynamically allocate array– Enables user to specify size of the array

• The class stackType implements the functions of the abstract class stackADT

Page 10: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

10

Implementation of Stacks as Arrays

• C++ arrays begin with the index 0– Must distinguish between:

• The value of stackTop• The array position indicated by stackTop

• If stackTop is 0, the stack is empty• If stackTop is nonzero, the stack is not

empty– The top element is given by stackTop - 1

Page 11: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

11

Implementation of Stacks as Arrays

Page 12: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

12

Initialize Stack

Page 13: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Empty Stack• If stackTop is 0, the stack is empty

Page 14: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

14

Full Stack• The stack is full if stackTop is equal to maxStackSize

Page 15: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

15

Push• Store the newItem in the array component

indicated by stackTop• Increment stackTop• Must avoid an overflow

Page 16: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

16

Push

Page 17: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

17

Return the Top Element

Page 18: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

18

Pop• Simply decrement stackTop by 1• Must check for underflow condition

Page 19: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 20: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Stacks Example• Example Suppose the following 6 elements are pushed, in order, onto

an empty stacks.

A, B, C, D, E, F We write the stack: STACK: A, B, C, D, E, F

Page 21: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Pseudocode for Array of Stacks

Procedure 3.1 PUSH(STACK, TOP, MAXSTK, ITEM)

This procedure pushes an ITEM onto a stack.

1. [Stack already filled ?]

If TOP = MAXSTK, then: Print:OVERFLOW, and

Return.

2. Set TOP := TOP + 1. [Increses TOP by 1]

3. Set STACK[TOP] :=ITEM. [Inserts ITEM in new TOP

position.

4. Return.

Page 22: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Pseudocode for Array of Stacks

• Procedure 3.2 POP(STACK, TOP, ITEM)

This procedures deletes the top element of STACK

and assigns it to the variable ITEM.

1. [Stacks has an item to be removed?]

If TOP = 0, the Print: UNDERFLOW, and RETURN.

2. Set ITEM := STACK[TOP].[Assigns TOP elements

to ITEM.]

3. Set TOP := TOP – 1.[ Decreases TOP by 1]

4. Return.

Page 23: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Stack ExerciseExercise 1:Consider the following stack of characters, where STACK is allocated N = 8

memory cells :

STACK : A,C,D, F, K, _, _ , _

( For notational convenience, we use “_” to denote an empty memory cell).

Describe the stack as the following operations take place :

(a)POP (STACK, ITEM ) (e) POP (STACK, ITEM)

(b)POP (STACK,ITEM) (f) PUSH(STACK, R)

(c) PUSH (STACK, L) (g) PUSH(STACK, S)

(d) PUSH (STACK, P) (h) POP(STACK, ITEM)

Page 24: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

Stack ExerciseExercise 2:Consider the following stack, where STACK is allocated N = 6 memory

cells :

STACK : A, D, E, F, G, _______.

(a) PUSH(STACK, K)

(b) POP(STACK, ITEM )

(c) PUSH(STACK, L)

(d) PUSH(STACK,S)

(e) POP(STACK, ITEM)

(f) PUSH(STACK, M)

Page 25: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

25

Copy Stack

Page 26: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

26

Stack Header File• Place definitions of class and functions

(stack operations) together in a file

Page 27: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 28: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 29: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

29

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

Page 30: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 31: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

31

Default Constructor• Initializes the stack to an empty state when a

stack object is declared– Sets stackTop to NULL

Page 32: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

32

Empty Stack and Full Stack• In the linked implementation of stacks, the

function isFullStack does not apply– Logically, the stack is never full

Page 33: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

33

Initialize Stack

Page 34: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

34

Push• The newElement is added at the beginning

of the linked list pointed to by stackTop

Page 35: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

35

Push (continued)

Page 36: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

36

Push (continued)

Page 37: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

37

Push (continued)

Page 38: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

38

Push (continued)• We do not need to check whether the stack

is full before we push an element onto the stack

Page 39: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 39

Return the Top Element

Page 40: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

40

.Pop

• Node pointed to by stackTop is removed

Page 41: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack
Page 42: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

C++ Programming: Program Design Including Data Structures, Fourth Edition 42

Pop (continued)

Page 43: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

43

Copy Stack

Page 44: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

44

Copy Stack

• Notice that this function is similar to the definition of copyList for linked lists

Page 45: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

45

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

Page 46: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

46

Application of Stacks: Postfix Expressions Calculator

• 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

Page 47: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

47

Application of Stacks: Postfix Expressions Calculator

• 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 * +

Page 48: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

48

Application of Stacks: Postfix Expressions Calculator

Page 49: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

49

Application of Stacks: Postfix Expressions Calculator

• 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

Page 50: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

50

Application of Stacks: Postfix Expressions Calculator

• 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

Page 51: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

51

Application of Stacks: Postfix Expressions Calculator

• Example: 6 3 + 2 * =– Read next symbol

• + is an operator (two operands) pop stack twice, perform operation, put result back onto stack

Page 52: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

52

• Example: 6 3 + 2 * =– Read next symbol

• 2 is a number push it onto the stack

Application of Stacks: Postfix Expressions Calculator

Page 53: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

53

• 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

Page 54: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

54

• 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

Page 55: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

55

Application of Stacks: Postfix Expressions Calculator

• 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

Page 56: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

56

Application of Stacks: Postfix Expressions Calculator

• 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

Page 57: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

57

Application of Stacks: Postfix Expressions Calculator

• 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

Page 58: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

58

Conclusions• Stack: items are added/deleted from one end

– Last In First Out (LIFO) data structure– Operations: push, pop, initialize, destroy, check for

empty/full stack– Can be implemented as array or linked list– Middle elements should not be accessed

• Postfix notation: operators are written after the operands (no parentheses needed)

Page 59: DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack

59

ReferencesC++ Programming: Program Design Including

Data Structures, Fourth Edition