Introduction To Stack

13
Introduction To Stack By: Dr. Ghulam Rasool

description

Introduction To Stack

Transcript of Introduction To Stack

Page 1: Introduction To Stack

Introduction To Stack

By:Dr. Ghulam Rasool

Page 2: Introduction To Stack

Stack

• A variable sized data structure in which insertion or deletion is made just from one end

or• A LIFO data structure is called Stack

Operations on Stack

1) Push ( to insert an element into stack)

2) Pop( to take an element out of stack)

The last element inserted is deleted or taken out first and first element at last

The stack can be implemented using Array as well as Link List

Page 3: Introduction To Stack

Stack using Arrays• The no of elements that can be inserted in a the stack is

the dimension of array. The current position of stack(i.e its size is known by a pointer called its top.

Algorithm to insert data in stack Push(S,Top, X, N)

1 If Top >= N then

wrtie (‘Stack full’) and exit

2 Top=Top+1

3 S[Top]= X

4 Exit

Page 4: Introduction To Stack

Stack Operations

Algorithm to delete an element from Stack Pop(S, Top)

1 If Top= 0 then

write(‘ stack is empty’) and exit

2 Pop = S[Top]

3 Top= Top-1

4 Exit

Applications of Stack

i) Recursion

ii) Evaluation of Arithmetic expressions

a) Infix notation

b) prefix notations

c) postfix notations

Page 5: Introduction To Stack

Recursion• The calling of a subprogram to itself is called recursions

Examples: Factorial of a number Fact(N)1) If N= 0 then Fact =1 2) Fact= N* Fact(N-1)

Polish Suffix notations• It is named after Polish mathematician Jan Lukasiewiez.

The operator symbol is placed before its two operands in this notations

• Examples: AB+, CD-, EF*

Page 6: Introduction To Stack

PSN notations• The expressions in which operands are preceded by the

operators are called PSN notations or postfix notations.• Example: Following are PSN notations

AB+ DE* ABC*+• Convert A+B*C into PSN

Symbol scanned operator Stack PSN A A + + A B + AB * +* AB C +* ABC $ ABC *+

Page 7: Introduction To Stack

RPN(Q, P)• Suppose Q is expression in infix form. This algorithm covert

this expression into equivalent postfix expression P

1) Scan Q from left to right and repeat Steps 2 to 5 for each element of Q until the end of Expression

2) If an operand is encountered, add it to P3) If a left parenthesis is encountered, push it onto stack4) If an operator is encountered then: a) Repeatedly pop from stack and add to P each operator which

has the same precedence as or higher precedence than (?) b) Add (?) to stack5) If a right parenthesis is encountered then: a) Repeatedly pop from stack and add to P each operator until a

left parenthesis is encountered b) remove left parenthesis6) Exit

Page 8: Introduction To Stack

Quiz 1

• Q.1 What is difference b/w Stack and Array. Can a stack be an array and an array can be a stack?

• Q.2 Convert following expressions into PSN ((A- (B+C)) *D)^(E+F)

Page 9: Introduction To Stack

Assignment IQ.2 Write a program that should take infix expression and convert it

into PSN. The program should also evaluate PSN expression.

Page 10: Introduction To Stack

Algorithm for evaluation of PSN1 Scan the Postfix string from left to right. 2 Initialise an empty stack.3 Repeat step 4 until the end of string4 If the scanned character is an operand, Push

it to the stack. a) If the scanned character is an Operator, pop top

two elements and apply operator. b) Push result back on top of stack

5 Pop last value from stack5 Exit.

Page 11: Introduction To Stack

Example• infix expression: 1+ 2 *3• Postfix : 1 2 3 + * Symbol Stack 1 1 2 1, 2 3 1, 2, 3 + 1, 5 * 5

Page 12: Introduction To Stack

Towers of Hanoi• Suppose three Pegs labled A, B and C and Suppose on Peg A

there are placed a finite number n of disks with decreasing size.• The objective of game is to move the disks from Peg A to PegC

using Peg B as an intermediate.• The rules of game are as:

i) Only one disk may be moved at a time, specifically only the top disk on any Peg may be moved to any other Peg.

ii) A larger disk can not be placed on a smaller disk• Example: For n=3• Move top disk from A to C Move top disk from A to C• Move top disk from A to B Move top disk from B to A• Move top disk from C to B Move top disk from B to C• Move top disk from A to C

Page 13: Introduction To Stack

Recursive Solution1 Move top n-1 disks from A to B2 Move the top disk from A to C3 Move the top n-1 disks from B to CGeneral notation:Tower(N, Beg, Sec, End)When N=1Tower(1, Beg, Sec, End)When N>1Tower( N-1 Beg, End, Sec)Tower(1, Beg, Sec, End) Beg …> EndTower( N-1, Sec, Beg, End)