Top Banner
Stack and Its Implementation 1 Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale [email protected] Room - 3131
61

Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Mar 19, 2018

Download

Documents

hadan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stack and Its Implementation

1

Tessema M. MengistuDepartment of Computer Science

Southern Illinois University [email protected]

Room - 3131

Page 2: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Outline• Definition of Stack

• Usage of Stack

– Checking for Balanced Algebraic Expression

– Infix to Postfix Conversion

– The Program Stack

• Array Based Implementation

• Linked List Based Implementation

• Vector Based Implementation

2

Page 3: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stack

• Stack is an ADT that follow last-in, first-out(LIFO) behavior

• All additions added to one end of stack– Added to “top”

– Called a “push” operation

• All removes to stack restricted to one end of stack– Remove only top entry

– Called a “pop” operation

3

Page 4: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stack

4

Page 5: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stack ADT

• Data

– A collection of objects having the same data type

• Operations

– +push(newEntry: T): void

– +pop(): T

– +peek(): T

– +isEmpty(): boolean

– +clear(): void

5

Page 6: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

public interface StackInterface<T>{

public void push(T newEntry);public T pop();public T peek();public boolean isEmpty();public void clear();

} // end

StackInterface

6

Page 7: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stackinterface …• Example:

7

Page 8: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

StackInterface …

8

Page 9: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

• Exercise

9

Page 10: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to Process Algebraic Expressions

• Algebraic expressions composed of

– Operands (variables, constants)

– Operators (+, -, /, *, ^)

• Operators can be unary or binary

• Different precedence notations

– Infix a + b

– Prefix + a b

– Postfix a b +

10

Page 11: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to Process Algebraic Expressions

• Precedence must be maintained

– Order of operators

– Use of parentheses (must be balanced)

• Use stacks to evaluate parentheses usage

– Scan expression

– Push symbols

– Pop symbols

11

Page 12: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to …• A balanced expression contains delimiters that

are paired correctly• Example

a {b [c (d + e)/2 - f ] + 1} - balanced a {b [c (d + e)/2 - f ] + 1} } – not balanced

Algorithm1. Read the expression from left to right, ignoring other

characters2. When an opening delimeter is found store it3. When a closing delimeter found, read the last saved

opening delimeter 4. If they are the same goto 1

12

Page 13: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to …• Example

a {b [c (d + e)/2 - f ] + 1}

• Lets use stack to store the delimiters

13

Page 14: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to …• Example

{a* [b + c *(c/d ]-6 )+e }

14

Page 15: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Using a Stack to …

• Example

[ ( ) ] }

15

Page 16: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

16

Page 17: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix Conversion • Manual algorithm for converting infix to

postfix (a + b) * c

– Write with parentheses to force correct operator precedence ((a + b) * c)

– Move operator to right inside parentheses((a b + ) c * )

– Remove parenthesesa b + c *

17

Page 18: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix …

• Algorithm basics

– Scan expression left to right

– When operand found, place at end of new expression

– When operator found, save to determine new position

18

Page 19: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix …

• Example

a + b*c

19

Page 20: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix …

• Example

a- b + c

20

Page 21: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix Conversion

1. Operand– Append to end of output expression

2. Operator ^– Push ^ onto stack

3. Operators +, -, *, /– Pop from stack, append to output expression

– Until stack empty or top operator has lower precedence than new operator

– Then push new operator onto stack

21

Page 22: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix Conversion

4. Open parenthesis

– Push ( onto stack

5. Close parenthesis

– Pop operators from stack and append to output

– Until open parenthesis is popped.

– Discard both parentheses

22

Page 23: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Example

a / b * (c + (d – e ) )

23

Page 24: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix• General rule :

24

Page 25: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Infix to Postfix

• Exercise

(a – b * c) / (d * e ^ f * g + h)

25

Page 26: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Evaluating Postfix Expression

• Requires no rules of precedence – The order of its operators and operands dictates the

order of operations

• Contains no parenthesis

• Algorithm – Scan the postfix expression

– Save operands till operator is found

– Apply the operator on the operands • The last saved operand is the second operand

– Save the result

– Continue till the expression is empty

26

Page 27: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

• Example

a b / where a = 2 , b= 4

27

Page 28: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Evaluating Postfix Expressions

28

Page 29: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

• Example

a b + c / where a= 2, b = 4 and c = 3

29

Page 30: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

• Exercise

e b c a ^ * + d -

where a = 2, b = 3, c = 4, d = 5, e = 6

30

Page 31: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

The Program Stack

• When a program executes, a special location called theprogram counter(PC) references the current instruction.

• When a method is called, the program’s run-timeenvironment creates an object called an activation record,or frame, for the method– Contains method’s arguments, local variables, and a reference

to the current instruction (PC)

• At the time the method is called, the activation record ispushed onto a stack called the program stack

• The program stack often contains more than one activation record.– The record at the top of the stack belongs to the method that is

currently executing

31

Page 32: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

The Program Stack

FIGURE 5-13 The program stack at three points in time: (a) when main begins execution; (PC is the program counter)

32

Page 33: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

The Program Stack

FIGURE 5-13 The program stack at three points in time: (b) when methodA begins execution; (PC is the program counter)

Copyright ©2012 by Pearson Education, Inc. All rights reserved33

Page 34: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

The Program Stack

FIGURE 5-13 The program stack at three points in time: (c) when methodB begins execution; (PC is the program counter)

Copyright ©2012 by Pearson Education, Inc. All rights reserved34

Page 35: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Java Class Library : Stack

• Under java.util package

• Methods

– public T push(T item);

– public T pop();

– public T peek();

– public boolean empty();

35

Page 36: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Linked Implementation• Consider push, pop, peek

– Each involves top of stack

• Best to put top of stack at head node

– Fastest, easiest to access

36

Page 37: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

37

Page 38: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

push Method

38

Page 39: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

push Method

39

Page 40: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

peek Method

40

Page 41: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

41

Page 42: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

42

Page 43: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Other Methods

43

Page 44: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Array Based Implementation• Again the question:

– Where to place the top entry?

44

Page 45: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Array Based Implementation

45

Page 46: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Array Based Implementation

• More efficient operations with bottom of stack at beginning of array

– Top of stack at last occupied entry

• Must consider memory wastage of unused array elements

46

Page 47: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

47

Page 48: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

push Method

48

Page 49: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

peek Method

Copyright ©2012 by Pearson Education, Inc. All rights reserved49

Page 50: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

50

Page 51: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

51

Page 52: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

52

Page 53: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Other Methodspublic clear()

{

for(int i=0;i<topIndex;i++)

stack[i]=null;

topIndex=-1;

}

53

Page 54: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Vector-Based Implementation

• Vector is a structure which behaves like a high level array

– Has methods to access entries

– Grows in size as needed (hidden from client)

54

Page 55: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Constructors and Methods of Vector

• public Vector()

• public Vector(int initialCapacity)

• public boolean add(T newEntry)

• public T remove(int index)

• public void clear()

• public T lastElement()

• public boolean isEmpty()

• public int size()

55

Page 56: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

56

Page 57: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Stack Methods for Vector Implementation

Copyright ©2012 by Pearson Education, Inc. All rights reserved57

Page 58: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

pop Method

58

Page 59: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

peek Method

59

Page 60: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Other Methods

60

Page 61: Stack and Its Implementationtmengistu/Courses/Fall2015/CS220/Slides/Stack and...–Infix to Postfix Conversion –The Program Stack ... •Manual algorithm for converting infix to

Exercise

• Write a program that accepts a string from the user and displays the reverse of the string. Use Stack class form Java’s collection framework

61