stack

77
Prepared By: Harekrushna Patel Tejas Patel

Upload: tejas-patel

Post on 22-May-2015

73 views

Category:

Education


7 download

DESCRIPTION

Basic concept of stack

TRANSCRIPT

Page 1: Stack

Prepared By:Harekrushna Patel

Tejas Patel

Page 2: Stack

A Stack is a Linear Data Structure. The Data Items in the stack are inserted and

deleted from one end. That is called Top of the stack.

Stack follows LIFO (Last In First Out) Mechanism.

Stacks are mostly used in system software. Like compilers and Operating Systems, etc..

Page 3: Stack

Insert values into stack is called push operation.

Reading Values from stack is called pop operation.

The Stack starts with position of 0(zero) . The Maximum Position of Stack is n-1.

Page 4: Stack

When The position of Stack is zero. Then, the stack is underflow.

When The position of Stack is grater than n-1. Then, the stack is called overflow.

Once an item is popped from stack, it is no longer available.

Page 5: Stack

The bunch of plates in the kitchen Books on a floor

Page 6: Stack

When TOS=-1 and we try to operate pop operation ‘empty stack’– ‘Underflow’

TOS=-1

Page 7: Stack

Push OperationHere Top Of Stack=-1,Max=10

TOS=-1

Page 8: Stack

Push OperationHere Top Of Stack will become

0,Max=10,To insert some values like 12,

12

TOS=0

Page 9: Stack

Push OperationHere Top Of Stack will become

1,Max=10,To insert some values like 12,34

12 34

TOS=1

Page 10: Stack

Push OperationHere Top Of Stack will become

2,Max=10,To insert some values like 12,34,17

12 34 17

TOS=2

Page 11: Stack

Push OperationHere Top Of Stack will become

3,Max=10,To insert some values like 12,34,17,26

12 34 17 26

TOS=3

Page 12: Stack

Push OperationHere Top Of Stack will become

4,Max=10,To insert some values like 12,34,17,26,46

12 34 17 26 46

TOS=4

Page 13: Stack

Push OperationHere Top Of Stack will become

5,Max=10,To insert some values like 12,34,17,26,46,53

12 34 17 26 46 53

TOS=5

Page 14: Stack

Pop OperationHere Top Of Stack will become

4,Max=10

12 34 17 26 46

TOS=4

Page 15: Stack

Push OperationHere Top Of Stack will become

5,Max=10Let’s insert value say 76

12 34 17 26 46 76

TOS=5

Page 16: Stack

Push OperationHere Top Of Stack will become

6,Max=10Let’s insert value say 76,28

12 34 17 26 46 76 28

TOS=6

Page 17: Stack

Push OperationHere Top Of Stack will become

7,Max=10Let’s insert value say 76,28,39

12 34 17 26 46 76 28 39

TOS=7

Page 18: Stack

• Push OperationHere Top Of Stack will become

8,Max=10Let’s insert value say 76,28,39,20

12 34 17 26 46 76 28 39 20

TOS=8

Page 19: Stack

Push OperationHere Top Of Stack will become

9,Max=10Let’s insert value say 76,28,39,20,9

12 34 17 26 46 76 28 39 20 9

TOS=9

Page 20: Stack

Push OperationHere Top Of Stack will become 10

and Max is also 10Now, When we are inserting element

say 87 ‘stack is full’ Called ‘Overflow’12 34 17 26 46 76 28 39 20 9

TOS=10

Page 21: Stack

Completely parenthesize the infix expression

Move each operator to the space held by the corresponding right parenthesis

Remove all parentheses

Page 22: Stack

infixExp

postfixExp

( a + b - c ) * d – ( e + f )

Page 23: Stack

infixExp

postfixExp

a + b - c ) * d – ( e + f )

(

stackExp

Page 24: Stack

infixExp

postfixExp

+ b - c ) * d – ( e + f )

(

a

stackExp

Page 25: Stack

infixExp

postfixExp

b - c ) * d – ( e + f )

(

a

+

stackExp

Page 26: Stack

infixExp

postfixExp

- c ) * d – ( e + f )

(

a b

+

stackExp

Page 27: Stack

infixExp

postfixExp

c ) * d – ( e + f )

(

a b +

-

stackExp

Page 28: Stack

infixExp

postfixExp

) * d – ( e + f )

(

a b + c

-

stackExp

Page 29: Stack

infixExp

postfixExp

* d – ( e + f )

a b + c -

stackExp

Page 30: Stack

infixExp

postfixExp

d – ( e + f )

a b + c -

*

stackExp

Page 31: Stack

infixExp

postfixExp

– ( e + f )

a b + c - d

*

stackExp

Page 32: Stack

infixExp

postfixExp

( e + f )

a b + c – d *

-

stackExp

Page 33: Stack

infixExp

postfixExp

e + f )

a b + c – d *

-

(

stackExp

Page 34: Stack

infixExp

postfixExp

+ f )

a b + c – d * e

-

(

stackExp

Page 35: Stack

infixExp

postfixExp

f )

a b + c – d * e

-

(

+

stackExp

Page 36: Stack

infixExp

postfixExp

)

a b + c – d * e f

-

(

+

stackExp

Page 37: Stack

infixExp

postfixExp

a b + c – d * e f +

-

stackExp

Page 38: Stack

infixExp

postfixExp

a b + c – d * e f + -

stackExp

Page 39: Stack

Steps:1. Scan the expression from left to right.2. Initialise an empty stack.3. If the scanned character is an operand,

add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack.

Page 40: Stack

4. If the scanned character is an Operator, then we store the top most element of the stack(top Stack) in a variable temp. Pop the stack. Now evaluate top Stack(Operator) temp. Let the result of this operation be ret Val. Pop the stack and Push ret Val into the stack.

Repeat this step till all the characters are scanned.

Page 41: Stack

6. After all characters are scanned, we will have only one element in the stack. Return top Stack.

Page 42: Stack

636+5*9/- // original expression

Page 43: Stack

636+5*9/- // original expression695*9/- // 3+6 evaluated

Page 44: Stack

636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated

Page 45: Stack

636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated

Page 46: Stack

636+5*9/- // original expression695*9/- // 3+6 evaluated6459/- // 6*9 evaluated65- // 45/9 evaluated1 // 6-5 evaluated

Page 47: Stack

Steps:1. Scan the expression from right to left.2. An operand is pushed on top of the

stack.3. In case of operator, two operand are

popped from the stack, evaluated and pushed back on to stack.

4. Final result is found on top of stack.

Page 48: Stack

* - + 4 3 5 / + 2 4 3 // original expression

Page 49: Stack

* - + 4 3 5 / + 2 4 3 // original expression

* - 7 5 / + 2 4 3 // 4+3 evaluated

Page 50: Stack

* - + 4 3 5 / + 2 4 3 // original expression

* - 7 5 / + 2 4 3 // 4+3 evaluated

* 2 / + 2 4 3 // 7-5 evaluated

Page 51: Stack

* - + 4 3 5 / + 2 4 3 // original expression

* - 7 5 / + 2 4 3 // 4+3 evaluated

* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated

Page 52: Stack

* - + 4 3 5 / + 2 4 3 // original expression

* - 7 5 / + 2 4 3 // 4+3 evaluated

* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated* 2 2 // 6/3 evaluated

Page 53: Stack

* - + 4 3 5 / + 2 4 3 // original expression

* - 7 5 / + 2 4 3 // 4+3 evaluated

* 2 / + 2 4 3 // 7-5 evaluated* 2 / 6 3 // 2+4 evaluated* 2 2 // 6/3 evaluated 4 // 2*2

evaluated

Page 54: Stack

• Any sort of nesting (such as parentheses)

• Evaluating arithmetic expressions (and other sorts of expression)

• Implementing function or method calls• Keeping track of previous choices (as

in backtracking)• Keeping track of choices yet to be

made (as in creating a maze)

Page 55: Stack

Steps:1. Scan the Infix string from left to right.2. Initialise an empty stack.3. If the scanned character is an operand,

add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.

Page 56: Stack

4. If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (top Stack). If top Stack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and top Stack has precedence over the character.

Page 57: Stack

5. Repeat this step till all the characters are scanned.

6. After all characters are scanned, we have to add any character that the stack may have to the Postfix string. If stack is not empty add top Stack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.

Page 58: Stack

Infix

A+BA+B-C

(A+B)*(C-D)A$B*C-D+E/F/(G+H)

((A+B)*C(DE))$(F+G)

A-B/(C*D$E)

Postfix

AB+AB+C-AB+CD-*AB$C*D-EF/GH+/+AB+C*DE--FG+$ABCDE$*/-

Page 59: Stack

Steps:1. Reverse the infix expression.2. Make every ‘(‘ (opening bracket) as ‘)’

(closing bracket) and vice versa.3. Convert the modified expression to

postfix form.4. Reverse the postfix expression.

Page 60: Stack

Infix

A+BA+B-C

(A+B)*(C-D)A$B*C-D+E/F/(G+H)

((A+B)*C(DE))$(F+G)

A-B/(C*D$E)

Prefix

+AB-+ABC*+AB-CD+-*$ABCD//EF+GH$-*+ABC-DE+FG-A/B*C$DE

Page 61: Stack

Goal: Move stack of rings to another peg Rule 1: May move only 1 ring at a time Rule 2: May never have larger ring on

top of smaller ring

Page 62: Stack

A B C

Page 63: Stack

A B C

Page 64: Stack

A B C

Page 65: Stack

A B C

Page 66: Stack

A B C

Page 67: Stack

A B C

Page 68: Stack

A B C

Page 69: Stack

A B C

Page 70: Stack

A B C

Page 71: Stack

A B C

Page 72: Stack

A B C

Page 73: Stack

A B C

Page 74: Stack

A B C

Page 75: Stack

A B C

Page 76: Stack

A B C

Page 77: Stack

A B C