The document discusses stacks and their applications. Stacks follow LIFO (last-in, first-out) order and allow insertions and removals only from the top. Common applications include evaluating arithmetic expressions by converting them to postfix notation and solving problems like Tower of Hanoi. The algorithms for push, pop, infix to postfix conversion and postfix evaluation are presented.
SyllabusSyllabus
List&Arrays Stacks ShortingList&Arrays
‐Consisting of a
collection
of elements
‐Allow insertions and
removals only at
top of stack(LIFO)
Shorting
‐Arranging data in
some given order
of elements top of stack(LIFO)
Queue
All i ti t
•Searching
‐Finding the
Recursion
‐Allow insertions at
the back and removals
from the front (FIFO)
Finding the
location of a given
item
‐Is the process of
repeating items in
a self‐similar way
Linked Lists Trees
Graphs and their
Application Linked Lists
‐Allow insertions and removals
anywhere
‐ High‐speed searching and
sorting of data and efficient
elimination of duplicate data
items
Previous classPrevious class
List vsAarryList vs Aarry
Sum=0
Sum of array Sum of Matrices Product of Matrices
Int c[3,5] Sum=0
F (k 0 k 3 k )for (i=0;i<9;i++)
{
sum=sum+a[i]
[ ]
For(i=0;i<3;i++)
{
For(j=0;j<5;j++)
For(k=0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum sum+a[l][i]*b[i][l]}
scanf(“%d”,sum)
{
c[i][j]=a[i][j]+b[i][j]
}
sum=sum+a[l][i]*b[i][l]
}
c[k][l]=sum
sum=0
4
}
sum=0
}
}
5.
StacksStacks
– New nodescan be added and removed only at theNew nodes can be added and removed only at the
top
Similar to a pile of dishes– Similar to a pile of dishes
– Last‐in, first‐out (LIFO)
– Bottom of stack indicated by a link member to
NULL
TOP
– Constrained version of a linked list
₋ TOP=‐1 means there is not item in
h kthe stacks
6.
Push and PopPush and Pop
push
–Adds a new node to the top of the stack
poppop
– Removes a node from the top
– Stores the popped value
– Returns true if pop was successful
7.
Push and Pop(cont’d)Push and Pop(contd)
Push Push Push Push Pop Push
D D
E
Dtop
top
top
A
D
C
B
C
B
C
B
D
C
Btop
top
p
B
A
B
A
B
A A
B
Atop
top
A
top=0 top=1 Top=2 Top=3 Top=3top=4top=‐1
If top is greater than maximum capacity of stack push operationIf top is greater than maximum capacity of stack , push operation
cannot be performed.
If top is lesser than 0, pop operation cannot be performed.
Infix to Postfix(cont’d)Infix to Postfix(contd)
Example
INFIX POSTFIX
A + B A B +
p
A * B + C
A B * C +
A * (B + C) A B C + *
A ‐ (B ‐ (C ‐ D)) A B C D---
A ‐ B ‐ C ‐ D A B-C-D-
16
17.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
• It useful because evaluation of postfix is faster• It useful because evaluation of postfix is faster
•Humans usually apply the rules of precedence to•Humans usually apply the rules of precedence to
set parentheses to determine the order of
evaluation .evaluation .
•Then build the postfix expression starting with the e bu d e pos e p ess o s a g e
first operator.
• e.g., 1*2+3 = (1*2)+3 leads to postfix 12*3+
•How do we apply the rules of precedence?
18.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
•Example: 1*2+3
* ( ) d 12 ( fi )‐scan: *,+ (operators) and 12 (postfix)
d f * i hi h th th‐precedence of * is higher than +; thus,
append * to postfix: + (ops ) and 12* (postfix)append * to postfix: + (ops.) and 12* (postfix)
scan further: + (ops ) and 12*3 (postfix)‐scan further: + (ops.) and 12*3 (postfix)
‐there are no further operators thus postfix is 12*3+‐there are no further operators, thus, postfix is 12 3+
19.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
Example: 1+2*3^4/5‐6Example: 1+2 3 4/5 6
scan: + * (operators) and 12 (postfix)‐scan: +,* (operators) and 12 (postfix)
‐precedence of + is lower than *; thus,
‐scan further: +,*,^ (operators) and 123 (postfix)
‐precedence of * is lower than ^; thus,
‐scan further: +,*,^,/ (ops.) and 1234 (postfix)scan further: +, , ,/ (ops.) and 1234 (postfix)
‐precedence of ^ is higher than /; thus,
append ^: +,*,/ (ops.) and 1234^ (postfix)
20.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
Example: 1+2*3^4/5‐6p
‐currently: + * / (ops ) and 1234^ (postfix)currently: +, ,/ (ops.) and 1234 (postfix)
‐"precedence" of * is higher than /; thus,
‐append *: +,/ (ops.) and 1234^* (postfix)
d f i l th / th‐precedence of + is lower than /; thus,
‐scan further: +,/,‐ (ops.) and 1234^*5 (postfix), , ( p ) (p )
‐precedence of / is higher than ‐; thus,
‐append /: +,‐ (ops.) and 1234^*5/ (postfix)
21.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
Example: 1+2*3^4/5‐6
‐currently: +,‐ (ops.) and 1234^*5/ (postfix)
‐"precedence" of + is higher than ‐; thus,
‐append +: ‐ (ops.) and 1234^*5/+ (postfix)append +: (ops.) and 1234 5/+ (postfix)
‐scan: ‐ (ops.) and 1234^*5/+6 (postfix)
‐no more operators: 1234^*5/+6‐ (postfix)
22.
Infix to Postfix(cont’d)Infix to Postfix (cont d)
operator_stack = empty stack
h l ( NULL)
Algorithm:
while (input!=NULL)
{
symbol = next input
g
if (symbol ==operand)
postfix string= symbol
else
while (top>=0&& operator_stack (top)> symbol)
{
postfix string= pop(operator stack)postfix string pop(operator_stack)
}
push (operator_stack, symbol )
}}
while (operator_stack not empty)
{
fi i ( k)postfix string= pop(operator_stack)
}
Postfix Evaluation(cont’d)Postfix Evaluation(cont d)
Evaluatingpostfix Expressions
Example: 623+‐382/+*2^3+
Evaluating postfix Expressions
‐scan from left to right: 6,2,3,+
l t 2 d 3 6 5‐apply + to 2 and 3: 6,5
‐scan further: 6,5,‐
l t 6 d 5 1‐apply ‐ to 6 and 5: 1
‐scan further: 1,3,8,2,/
l / t 8 d 2 1 3 4‐apply / to 8 and 2: 1,3,4
‐scan further: 1,3,4,+
25.
Postfix Evaluation(cont’d)Postfix Evaluation(cont d)
Evaluating postfixExpressions
Example.: 623+‐382/+*2^3+,
currently: 1,3,4,+
‐apply + to 3 and 4: 1,7
‐scan further: 1,7,*
‐apply * to 1 and 7: 7
‐scan further: 7,2,^
‐apply ^ to 7 and 2: 49
‐scan further: 49,3,+
‐apply + to 49 and 3: 52
The value ofthe expression(7‐4) that has been evaluated(3) is pushed into the stack.
Stack ExpressionStack Expression
The value of the expression(7‐4) that has been evaluated(3) is pushed into the stack.
Stack Expression
Now, since all the characters are scanned, the remaining element in the stack (there will be only one
element in the stack) will be returned.
E d ltEnd result:
Postfix String : 1 2 3 * + 4 ‐
Result : 3
29.
Postfix(cont’d)Postfix(cont d)
Algorithm
operand_stack =empty stack
while (input!=NULL)
Algorithm:
while (input! NULL)
{
symbol = next input symbol
if (symbol ==operand)
push(operand_stack, symbol)
elseelse
operand2 = pop(operand_stack)
operand1 = pop(operand stack)p d p p( p d_ t k)
result = apply symbol to operand1 and operand2
push(operand_stack, result)
pop(operand_stack)
}
30.
Infix to PrefixInfix to Prefix
INFIXPREFIX
A + B + A B
A * B + C + * A B C
A * (B + C) * A + B C
A ‐ (B ‐ (C ‐ D)) -A-B-C D
A ‐ B ‐ C ‐ D ---A B C D
fi b ffi b f
30
Prefix : Operators come before
the operands
Prefix : Operators come before
the operands
31.
Infix to prefixconversionInfix to prefix conversion
Show me the algorithm in next classShow me the algorithm in next class
31
32.
Prefix EvaluationPrefix Evaluation
Example: Infix:1 * (2 + 3); Prefix: *1 + 2 3Example: Infix: 1 (2 + 3); Prefix: 1 + 2 3
Reverse string: 32+1*g
in stack 3 ; in string 2+1*
in stack 32; in string +1*g
scan +; in string 1*
Result= 2 + 3
in stack 5; in string 1*
in stack 51; in string*;
Scan *
Result=1*5
in stack 5
The Tower ofHanoi ProblemThe Tower of Hanoi Problem
GIVEN: three poles– GIVEN: three poles
– a set of discs on the first pole, discs of different sizes, the smallest
discs at the top
– GOAL: move all the discs from the left pole to the right one.
– CONDITIONS: only one disc may be moved at a time.
A di b l d ith t l t f l– A disc can be placed either on an empty pole or on top of a larger
disc.
Towers of Hanoi:AlgorithmTowers of Hanoi: Algorithm
Input number of disksInput number of disks
If (n==1)
move single disk from peg A(1st) to peg C(3rd) and stopmove single disk from peg A(1 ) to peg C(3 ) and stop.
else
move the top (n‐1) disks from peg A to peg B using peg C as
auxiliary.auxiliary.
Move remaining disks from peg A to peg CMove remaining disks from peg A to peg C.
Move (n 1) disks from peg B to peg C using peg A asMove (n‐1) disks from peg B to peg C using peg A as
auxiliary.