16
Data Structures an ordered list where all operations are restricted at one end of the list known as the top Stack Stack * Property of STI Page 1 of 16

Meljun Cortes Data Structures Stacks

Embed Size (px)

DESCRIPTION

Meljun Cortes Data Structures Stacks

Citation preview

Page 1: Meljun Cortes Data Structures Stacks

Data Structures

� an ordered list where all operations are

restricted at one end of the list known as the

top

Stack

Stack * Property of STIPage 1 of 16

Page 2: Meljun Cortes Data Structures Stacks

Data Structures

Stack Operations

� PUSH – adds item at the top

� POP – removes element from the top

� PEEK – accesses value at the top

� LIFO or Last-In-First-Out - the last object

inserted in the list will be the first one to be

retrieved

Stack * Property of STIPage 2 of 16

Page 3: Meljun Cortes Data Structures Stacks

Data Structures

Function Call

� It is an example of stack that arises in computer

programming

Stack * Property of STIPage 3 of 16

Procedure using two Functions

� This process of saving all information about the

calling routine is performed using a stack by

virtually every programming language that

implements recursion

Page 4: Meljun Cortes Data Structures Stacks

Data Structures

Function Call

Stack * Property of STIPage 4 of 16

Contents of stack after invoking FUNC1

Contents of stack after invoking FUNC2

Page 5: Meljun Cortes Data Structures Stacks

Data Structures

Function Call

Contents of stack after retrieving return

Stack * Property of STIPage 5 of 16

Contents of stack after retrieving return

address to FUNC2

Contents of stack after retrieving variables of

FUNC1

Page 6: Meljun Cortes Data Structures Stacks

Data Structures

Stack Representation

� One-dimensional array

Stack * Property of STIPage 6 of 16

� Doubly-Linked List

Page 7: Meljun Cortes Data Structures Stacks

Data Structures

Reversing a Word

� it uses stack to temporarily store an element

� extracting characters one-by-one from the

input string and pushed onto the stack

� popped off the stack and displayed

� stack reverses the order of the characters

because of its last-in-first-out characteristics

Stack * Property of STIPage 7 of 16

Page 8: Meljun Cortes Data Structures Stacks

Data Structures

Evaluation of Expressions

� An expression is made up of operands and

operators

A / B * C + D * E

� The operations to be performed on the

operands are described by the associated

operator

� Operators of a higher precedence are

Stack * Property of STIPage 8 of 16

� Operators of a higher precedence are

processed first

� Evaluation is performed from left-to-right

A / B * C + D * E

1

2

3

4

Page 9: Meljun Cortes Data Structures Stacks

Data Structures

Evaluation of Expressions

� Infix notation

operand operator operand

� Postfix notation

operand operand operator

infix expression may be directly translated into

postfix form by beginning with the conversion of

Stack * Property of STIPage 9 of 16

the subexpression with the highest precedence

1. ((A / B) / C ) * (D + E) = AB/

AB/C/

AB/C/DE+*

2. (A + B) / (C - A) + D * E = AB+

AB+CA-/

AB+CA-/DE*+

Page 10: Meljun Cortes Data Structures Stacks

Data Structures

Exercise

� convert the following infix notation to postfix

notation

1. (A*B+C)/D-E*F

2. P/O+D-S*R

3. J*Q+S-V/B-N

4. H-(A+K)+E*D

5. L/(F-R)+(O*R-(W/D))

Stack * Property of STIPage 10 of 16

Page 11: Meljun Cortes Data Structures Stacks

Data Structures

Postfix Notation

� The algorithm for Postfix performs the following

operations on expressions written in infix

notation:

� Detects errors in the syntax of the infix

expression.

� Transforms the infix expression into postfix

notation.

� Algorithm

Stack * Property of STIPage 11 of 16

� Algorithm

� If recognize an operand, push it on the

stack.

� If recognize an operator, pop its operands,

apply the operator and push the value on

the stack.

� Upon conclusion, the value of the postfix

expression is on the top of the stack.

Page 12: Meljun Cortes Data Structures Stacks

Data Structures

Postfix Notation

A B C D * + / E * F –

Tokens Operation Stacks

A Push a A

B Push b B A

C Push c C B A

D Push d D C B A

* Pop the top two element, multiply, push result

C*D B A

Stack * Property of STIPage 12 of 16

+ Pop the top two element, add, push result

B+(C*D) A

/ Pop the top two element, divide, push result

A/(B+(C*D))

E Push E E A/(B+(C*D))

* Pop the top two element, multiply, push result

A/(B+(C*D))*E

F Push F F A/(B+(C*D))*E

- Pop the top two element, subtract, push result

A/(B+(C*D))*E –F

Page 13: Meljun Cortes Data Structures Stacks

Data Structures

Exercise

� Convert the following postfix notation to infix

notation

1. AB*C+DEF*-/

2. PO/D+SR*-

3. JQ*S+VB/-N-

4. HAK+-ED*-

5. LFR-/OR*WD/-+

Stack * Property of STIPage 13 of 16

Page 14: Meljun Cortes Data Structures Stacks

Data Structures

Eliminating Recursion

� Most compilers implement recursion using

stacks

� Stack is used to hold activation records for each

method and so, for every invocation of a

method during recursion allows an activation of

record to be pushed on the system stack

Stack * Property of STIPage 14 of 16

Page 15: Meljun Cortes Data Structures Stacks

Data Structures

Eliminating Recursion

� Call and Return Process

� When a method/function is called:

1. An activation record is created; its size

depends on the number and size of the local

variables and parameters.

2. The Base Pointer value is saved in the

special location reserved for it.

3. The Program Counter value is saved in the

Return Address location.

Stack * Property of STIPage 15 of 16

Return Address location.

4. The Base Pointer is now reset to the new

base (top of the call stack prior to the

creation of the Activation Record).

5. The Program Counter is set to the location

of the first bytecode of the method being

called.

6. Copies the calling parameters into the

Parameter region.

7. Initializes local variables in the local variable

region.

Page 16: Meljun Cortes Data Structures Stacks

Data Structures

Eliminating Recursion

� When a method returns

� Get the program counter from the activation

record and replace what's in the Program

Counter.

� Get the base pointer value from the

Activation Record and replace what's in the

Base Pointer.

� Pop the Activation Record entirely from the

stack.

Stack * Property of STIPage 16 of 16

stack.