Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views19 pages

Stack

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 19

Stacks

 A stack is a list in which insertion and deletion take


place at the same end
 This end is called top

 The other end is called bottom

1
 Stacks are known as LIFO (Last In, First Out) lists:
The last element inserted will be the first to be
retrieved, using Push and Pop
 Attributes of Stack
 MAXSTACK: which gives the maximum
number of elements that can be held by the stack.
 TOP: the index of the top element of stack

 Operations of Stack
 push: add an element to the top of stack

 pop: delete the element at the top of stack

2
Array Representation
STACK
of Stack
A B C

1 2 3 4 5 6 7 8 9

3 9
TOP MAXSTACK
 STACK: Linear Array
 TOP: Pointer variable, which contains the location of top element of the stack.
 MAXSTACK: A variable, which gives the maximum number of elements that can
be held by the stack.
 If TOP=0 or TOP=NULL Stack is empty and this condition is

known as underflow.
 If TOP= MAXSTACK Stack is Full and this condition is known
as Overflow.

3
Push and Pop operations on Stack
PUSH(STACK,TOP,MAXSTACK,ITEM) POP(STACK,TOP,ITEM)

1. [STACK already filled] 1. If TOP=0, then print


If TOP=MAXSTACK, then Print UNDERFLOW and Return
OVERFLOW and Return. 2. Set ITEM=STACK[TOP].
2. Set TOP=TOP+1
3. Set STACK[TOP]=ITEM. 3. Set TOP=TOP-1.
4. Return. 4. Return.
empty stack push an element push another pop

top
B
top top A
A A
top

4
Linked Representation of

Stacks
Linked Stack is a stack that is implemented
using a singly linked list
 INFO field of the node holds the element of
the stack
 LINK field of the node holds pointer to the
neighboring element in the stack
5 10 3 7
INFO LINK

5
PUSH operation in Linked Stack

1. If AVAIL == NULL then write OVERFLOW


and Exit
2. Set NEW = AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW] = ITEM
4. Set LINK[NEW] = TOP
5. Set TOP = NEW
6. Exit

*AVAIL is the pointer to available free nodes list


6
POP operation in Linked Stack
1. If TOP == NULL then write UNDERFLOW
and Exit
2. Set ITEM = INFO[TOP]
3. Set TEMP = TOP and TOP = LINK[TOP]
4. Set LINK[TEMP]=AVAIL and AVAIL=TEMP
5. Exit

*TEMP is the temporary node to save the


deleted node and add it to available free
nodes list
7
Arithmetic Expressions
Infix, Prefix and Postfix
If the operator is placed in between operands, it is called
infix notation
e.g. A+B, (A+B)*C, A+(B*C)
Prefix notation (Polish Notation) – operators preceded the
operands
e.g. +AB
Postfix Notation (Reverse Polish Notation) - Operator
symbol is placed after the operands
e.g. A+B= AB+

8
Evaluation of a postfix notation
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat Step3 and 4 for each
element of P until the sentinel ”)” is encountered.
3. If an operand is encountered, push it on STACK.
4. If an operator X is encountered, then:
(a) Remove the two top elements of STACK, where A is the
top element and B is the next to top element.
(b) Evaluate B X A.
(c) Place the result of (b) back on STACK.
5. Set VALUE equal to the top element on the STACK.
6. Exit.

9
Example

 Consider the following arithmetic expression


P written in postfix notations :
P : 5, 6, 2, +, *, 12, 4, /, -
 We evaluate P by simulating algorithm. First

we add a sentinel right parenthesis at the end


of P to obtain
P : 5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

10
Symbol Scanned STACK
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
(10) )

11
Infix to Postfix Conversion
POLISH(Q,P)
1. Push “(” onto STACK and add “)” to the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the
STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator X is encountered , then:

1. Repeatedly pop from STACK and add to P each operator which has
the same or higher precedence than X.
2. Add X to STACK.
6. If a right parenthesis is encountered, then:
1. Repeatedly pop from STACK and add to P each operator until a left
parenthesis is encountered.
2. Remove the left parenthesis.
7. Exit.

12
Example: Infix to Postfix Expression A+(B*C-(D/E↑F)*G)*H
Symbol Scanned
(1) A ( A
(2) + (+ A
(3) ( (+( A
(4) B (+( AB
(5) * (+(* AB
(6) C (+(* ABC
(7) - (+(- ABC*
(8) ( (+(-( ABC*
(9) D (+(-( ABC*D
(10) / (+(-(/ ABC*D
(11) E (+(-(/ ABC*DE

(12) ↑ (+(-(/↑ ABC*DE

(13) F (+(-(/↑ ABC*DEF

(14) ) (+(- ABC*DEF↑ /


(15) * (+(-* ABC*DEF↑ /
(16) G (+(-* ABC*DEF↑ /G
(17) ) (+ ABC*DEF↑ /G*-
(18) * (+* ABC*DEF↑ /G*-
(19) H (+* ABC*DEF↑ /G*-H
13
(20) ) ABC*DEF↑ /G*-H*+
Stacks
 Real life analogy:
 Dish holders (stacks)

 Typical uses of stacks:


 Prefix-/Postfix- calculators

 Any list implementation could be used to implement a stack


 Arrays (static: the size of stack is given initially)

 Linked lists (dynamic: never becomes full)

14
RECURSION: Application of Stack
 To calculate factorial of a number
int fact (int n)
{
if ( n==1)
return 1;
return (n * fact (n-1) );
}
 To make Fibonacci Series is another
example of RECURSION.
15
Towers of Hanoi : Application of
Stack
 Three pegs labeled A, B,C
 In A, there are finite number of disks with
decreasing size
 Objective- move the disks from A to C using
B as an auxiliary.
 The rules of the game are as follows
 Only one disk may be moved at a time.
 At no time can a larger disk be placed on a
smaller disk

16
A B C

17
If n=3
A C A B C B A C B A B C A C

For n disks
1. Move the top n-1 disks from peg A to peg B
2. Move the top disk from peg A to C
3. Move the top n-1 disks from peg B to peg C

Tower(N-1, BEG, END, AUX)


Tower(1,BEG, AUX, END)
Tower(N-1,AUX,BEG,END)

18
Tower (2, A,B,C)

Tower(3, A,C,B) A B Tower(1,C,B,A)

Tower(4,A,B,C) A C Tower(2, C,A,B) C B


Tower(2, B,C,A)
Tower(1,A,C,B)
Tower(3, B,A,C) B C

Tower(2, A,B,C)

19

You might also like