• No results found

Data Structures & Algorithms

N/A
N/A
Protected

Academic year: 2020

Share "Data Structures & Algorithms"

Copied!
51
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures & Algorithms

(2)

Today Topics

What are Stacks?

Representation of Stacks

Operations on Stacks

Push

Pop

IsEmpty

IsFull

Usage

Evaluation of Expressions

(3)

What are Stacks?

In array insertion and deletion can take place at

both end, i.e. start and end,

But if insertion and deletion are restricted from one

end, must used

STACKS and QUEUES.

Stack can be defined as:

A stack is a linear data structure which can

be accessed only at one of its ends for

storing and retrieving data

In which item may be inserted or deleted only from

one end called top of the stack.

There are two ways of implementing a stack:

Array (Static & Dynamic)

(4)

Example of Stack

A common model of a

stack is a plate or coin

stacker. Plates are

"pushed “onto to the top

and "popped" off from the

top.

Stacks principle

Last-In-First-Out (LIFO)

(5)

Stack terminology

“Top”: where insertion and

deletion take place

“Push”: is the term used to insert an

element into an stack

“Pop”: is the term used to delete an

element from a stack.

(6)

Basic operations of a Stack

New

Push

Pop

Peek

?

(7)

Implementation of Stacks

A stack can be implemented using an array

(static & dynamic)

A pointer variable

TOP

which contains the

location of the top element of the stack;

Variable

MAXSTK

which gives the maximum

number of elements that can be held by the

stack.

(8)

Note that all operations need check array

bounds

Pushing onto a full stack:

Overflow

When TOP=MAXSTK

Popping an empty stack:

Underflow

TOP=NULL

(9)

Operations on Stack

A stack is generally implemented with only

two principle operations:

Push: adds an item to a stack

Pop: extracts the most recently pushed item from

the stack

Other methods such as

top: returns the item at the top without removing

it

IsEmpty: determines whether the stack has

anything in it

IsFull: determines whether the stack is full or

stacksize is reached to MAXST anything in it

(10)

How the stack routines work

empty stack; push(a), push(b); pop

empty stack top = 0

a

push(a) top = 1

b

a

push(b) top = 2

a

pop() top = 1

(11)

Algorithms of Push Operation

Push (STACK , TOP, MAXST, ITEM)

Algorithm for push element into the Stack

1.

[stack already filled]

If TOP= MAXSTK, then: print:

“OVERFLOW”,

and return.

2.

[increases TOP by 1] Set TOP:= TOP + 1

3.

[insert ITEM in new TOP position ]

Set STACK[TOP]: =ITEM

4. RETRUN

(12)

Algorithms of Pop Operation

Pop (STACK, TOP, ITEM)

Algorithm for pop element from the Stack

1.

[Stack has an item to be removed]

If TOP= NUL, then

Print:

“UNDERFLOW”

and return

2. [assign TOP element to ITEM]

Set ITEM := STACK[TOP]

3. [Decrease TOP by 1]

Set TOP:= TOP -1

4. Return

(13)

Algorithm for Display Stack

elements

Display_Stack ()

Algorithm for display stack elements

1.

Start

2.

Repeat step 3 For i = 1 to TOP by 1

3.

Print S[i]

(14)

Algorithm for isempty Stack

isempty ()

Algorithm for return the Stack status

1.

Start

2.

if TOP = -1 then

Return True

else

Return False

3.

End

(15)

Algorithm for return the top of

stack value

top ()

Algorithm for return the top of Stack

value

1.

Start

2.

Return TOP

(16)

Evaluation of Expression

Arithmetic expression is made up

Operands (Numeric Variables or

Constants)

Arithmetic Operators (+, -, *, /)

Power Operator (^)

Parentheses

The Expression is always evaluated

from left to right

(17)

Evaluation of Expression

Order in which the expression is evaluated is:

If the expression has parenthesis, then they are

evaluated first

Exponential (^) is given highest priority

Multiplication (*) and division (/) have the next

highest priority

Addition (+) and subtraction (-) have the lowest

priority

(18)

Evaluation of Expression

Steps to evaluate the following expression:

(2^3 + 6) * 2 – 9 / 3

= (8 + 6) * 2 – 9 / 3

= 14 * 2 – 9 / 3

= 28 – 3

(19)

Infix, Prefix (Polish) and Postfix

(Reverse Polish) Notation

Infix

Prefix

(Polish Notation)

Postfix

(Reverse Polish

Notation)

A+B

+AB

AB+

A*B

*AB

AB*

A/B

/AB

AB/

(20)

AB-Evaluation of Expression

Computer evaluates an expression given in infix notation by converting it into postfix notation.

Stack is used to perform this operation

Following steps are take to evaluate a postfix expression:

Expression is scanned from left to right until the end of the expression

When an operand is encountered, it is pushed into stack

When an operator is encountered, then

Top two operands of stack are removed

Arithmetic operation is performed

Computed result is pushed back to the stack

When end of the expression is reached, the top value from the stack is picked. It is the computed value of the expression

(21)

Infix to Postfix Conversion

□ Stack is used to convert an infix expression to postfix.

□ Stack is used to store operators and operands and then pass to the postfix expression according to their precedence.

□ Infix expression is converted into postfix expression according to the following rules:

■ Infix expression is scanned from left to right until end of the expression.

■ Operands are passed directly to the output.

■ Operators are first passed to the stacks.

(22)

Infix to Postfix Conversion

■ Each time an operator is read, the stack is repeatedly popped and operands are passed to the output, until an operator is reached that has a lower precedence than the most recently read operator. The most recently read

operator is then pushed onto the stack.

■ When end of the infix expression is reached, all operators remaining in the stack are popped and passed to the output in the same sequence.

■ Parentheses can be used in the infix expression but these are not used in the postfix expression.

■ During conversion process, parentheses are treated as operators that have higher precedence than any other operator.

(23)

Infix to Postfix Conversion

Left parenthesis is pushed into the stack, when

encountered.

Right parenthesis is never pushed to the stack.

Left parenthesis is popped only when right

parenthesis is encountered.

The parentheses are not passed to the output postfix

expressions. They are discarded.

When end of expression is reached, then all operators

from stack are popped and added to the output.

(24)

Example (Infix to Postfix

Conversion)

Convert infix expression

A+B*C+(D*E+F)*G into postfix A + B * C + ( D * E + F ) * G

1. Scanned from left to right. First operand read is A and passed to output

Stack Output: A

(25)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

2. Next the ‘+’ operator is read, at this stage, stack is empty.

Therefore no operators are

popped and ‘+’ is pushed into the stack. Thus the stack and output will be:

+

Stack Output: A

(26)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

3. Next the ‘B’ operand is read and passed to the output. Thus the stack and output will be:

+

Stack Output: AB

(27)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

4. Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and

output will be:

* +

Stack Output: AB

(28)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

5. Next the ‘C’ operand is read and passed to the output. Thus the stack and output will be:

* +

Stack Output: ABC

(29)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

6. Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has ‘+’ operator which has same precedence than the ‘+’ operator. The Stack is popped and passed to output. Now stack is empty, therefore no operators are

popped and ‘+’ is pushed into the stack. Thus the stack and output

will be: +

Stack Output: ABC*+

(30)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

7. Next the left parenthesis ‘(’ is read, Since all operators have lower precedence than the left parenthesis, it is pushed into the stack. Thus the stack and output will be:

( +

Stack Output: ABC*+

(31)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

8. Next the ‘D’ operand is read and passed to the output. Thus the stack and output will be:

( +

Stack Output: ABC*+D

(32)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

9. Next the ‘*’ operator is read. Now, left parenthesis ‘(‘ has

higher precedence than ‘*’; it can not be popped from the stack

until a right parenthesis ‘)’ has been read. Thus the stack is not popped and ‘*’ is pushed into the stack. Thus the stack and output

will be: *

( +

Stack Output: ABC*+D

(33)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

10. Next the ‘E’ operand is read and passed to the output. Thus the stack and output will be:

* ( +

Stack Output: ABC*+DE

(34)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

11. Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has left parenthesis ‘(’ which has not been popped and ‘+’ operator is pushed into the stack. Thus the

stack and output will be: + ( +

Stack Output: AB*+DE*

(35)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

12. Next the ‘F’ operand is read and passed to the output. Thus the stack and output will be:

+ ( +

Stack Output: ABC*+DE*F

(36)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

13. Next the ‘)’ has encountered now popped till ‘( ‘ and passed to the output. Thus the stack and

output will be:

+

Stack Output: ABC*+DE*F+

(37)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

14. Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and

output will be:

* +

Stack Output: ABC*+DE*F+

(38)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

15. Next the ‘G’ operand is read and passed to the output. Thus the stack and output will be:

* +

Stack

(39)

Example (Infix to Postfix

Conversion)

A + B * C + ( D * E + F ) * G

16. The end of expression is

encountered. The operators are popped from the stacked and

passed to the output in the same sequence in which these are

popped. Thus the stack and output will be:

Stack

(40)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

1. Scanned from left to right. In first, second and third iteration, the value of A, B and C are pushed into the stack. Thus the stack will be:

9 6 5

(41)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

2. In fourth iteration, the operator ‘*’ is read. So the two values ‘9’ and ‘6’ are popped from the stack and

multiplication is perform. i.e 9*6=54. The computed value pushed back to the stack. Thus the stack will be:

54 5

(42)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

3. In fifth iteration, the operator ‘+’ is read. So the two values ‘54’ and ‘5’ are popped from the stack and

addition is perform. i.e 54+5=59. The computed value pushed back to the stack. Thus the stack will be:

59

(43)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

4. In sixth and seventh iteration, the value of D and E are pushed into the stack. Thus the stack will be:

4 2 59

(44)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

5. In eighth iteration, the operator ‘*’ is read. So the two values ‘4’ and ‘2’ are popped from the stack and

multiplication is perform. i.e 2*4=8. The computed value pushed back to the stack. Thus the stack will be:

8 59

(45)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

6. In ninth iteration, the value of F is pushed into the stack. Thus the stack will be:

8 8 59

(46)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

7. In tenth iteration, the operator ‘+’ is read. So the two values ‘8’ and ‘8’ are popped from the stack and

addition is perform. i.e 8+8=16. The computed value pushed back to the stack. Thus the stack will be:

16 59

(47)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

8. In eleventh iteration, the value of G is pushed into the stack. Thus the stack will be:

3 16 59

(48)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

9. In twelve iteration, the operator ‘*’ is read. So the two values ‘3’ and ‘16’ are popped from the stack and

multiplication is perform. i.e 3*16=48. The computed value

pushed back to the stack. Thus the

stack will be: 48

59

(49)

Example (Evaluation of Expression)

Postfix Expression

A B C * + D E * F + G * +

A=5, B=6, C=9, D=2, E=4, F=8, G=3

10. In thirteen iteration, the operator ‘+’ is read. So the two values ‘48’ and ‘59’ are popped from the stack and addition is perform. i.e 48+59=107. The computed value pushed back to the stack. Thus the stack will be:

107

(50)

Example (Evaluation of Expression)

A + B * C + ( D * E + F ) * G A=5, B=6, C=9, D=2, E=4, F=8, G=3 = 5 + 6 * 9 + (2 * 4 + 8) * 3 = 5 + 54 + (8 + 8) * 3 = 59 + 16 * 3 = 59 + 48 =107

(51)

Next Lecture

What are Queues?

Representation of Queues

Operations on Queues

QInsert

References

Related documents

In view of the demographic changes and the growing proportion of older people, the current  healthcare  system  has  to  be  reconsidered  to  meet  the 

The Rater will discuss with the Ratee the proposed objective, target completion date, the evaluation or measurement criteria by which successful results can be identified, costs,

Members and others are strongly encouraged to continue to forward any questions they have or issues they encounter regarding implementation of the ISF to [email protected] for

Similar to the developments in the United States (but certainly less pronounced), this rise in high income shares was not driven by a relative increase in capital income but rather

Given the tractability of the recovery of market value, we solved the optimal portfolio problem for the representative investor whose utility function is a Constant Relative

Arbitrators were aware of previous conclusions at the final arbitration discussion only; “Single arbitrator” = where the primary reading resulted in discordant interpretations for

President Abraham Lincoln issued the Emancipation Proclamation on January 1 163 as the nation approached its third cause of bloody civil penalty The proclamation declared that

Hence, similar effects on the number of jams and the jam weight are achieved using controlled advice systems but the negative effects on the average network speed are reduced..