Escolar Documentos
Profissional Documentos
Cultura Documentos
Stack Overview
array
linked list
What is a stack?
The Stack
Fundamental/Primitive operations:
Stack ADT
Example
Consider a mathematical expression that includes several sets of nested
parenthesis.
7 ((x * (( x + y) / (j - 3)) + Y) / (4 2.5))
In expression we want to check
1. There are an equal number of right and left parenthesis.
2. Every right parenthesis is preceded by a matching left
parenthesis.
Expressions like ((A+B) or A+B( violates condition 1 and expressions
like )A + B(-C violate condition 2.
Nested Depth: is the number of scopes that have been opened
and not yet closed.
Parenthesis count: no of left parenthesis no of right parenthesis.
Example (Contd)
Expression id valid if following conditions are met.
1. Parenthesis count at end should be zero.
2. Parenthesis count at each point in expression should be non
negative.
7 ( ( x * ( ( x + y ) / ( j - 3 ) ) + Y ) / ( 4 2.5 ) )
00122234444334444322 211222 210
( ( A + B ) and (A + B] is illegal
1 22221
Parenthesis Stack
Consider expression
[x + {y (a + b)}]
(
[
[ x+
{
[ x+{y-(
[
[ x+{y(a+b)}
[ x+{y(a+b)}]
[ x+{y(a+b)
Implementation of Stacks
Array Implementation
Stack Operations
void push( Stack *s, int x)
- add item to the top of the stack
void push(Stack *s, int x)
{
if ( s->top==SIZE-1)
{
cout <<Stack overflow;
exit(1)
}
else
s->items[++(s->top)]=x;
}
Call of function will be like push(&s, 2);
Stack Operations
int pop( Stack *s )
- remove an item from the top of the stack
int pop(stack *s)
{
if (empty(s))
{
cout <<Stack underflow;
exit(1);
}
return(s->items[s->top--]);
}
Stack Operations
int stacktop( Stack *s )
- To get the top element of the stack
int stacktop(stack *s)
{
if (empty(s))
{
cout <<Stack underflow;
exit(1);
}
return(s->items[s->top]);
}
Stack Operations
int stacktop( Stack *s )
- To get the top element of the stack
int stacktop(stack *s)
{
if (empty(s))
{
cout <<Stack underflow;
exit(1);
}
return(s->items[s->top]);
}
Polish Notations
Expression can be written in prefix, postfix or
infix notations
+ A B prefix
operator precedes operands
A + B infix
Unlike Infix notation no parenthesis are
required to enforce precedence.
Expressions are evaluated from left to
right.
Exercises
A$B*C-D+E/F/(G+H)
((A+B)*C-(D-E))$(F+G)
A-B/(C*D$E)
(A+B$D)/(E-F)+G
A*(B+D)/E-f*(G+H/K)
Example: postfix
expressions
(cont.)
Postfix expressions:
Algorithm using stacks
opndstk
= the empty stack;
(cont.)
EXERCISE
623+-382/+*2$3+
symb op1
6
2
3
+
3
8
2
/
+
*
2
$
3
+
2
6
6
6
6
8
3
1
1
7
7
49
op2
3
5
5
5
5
2
4
7
7
2
2
3
value
5
1
1
1
1
4
7
7
7
49
49
52
opndstk
6
62
623
65
1
13
138
1382
134
17
7
72
49
49 3
52
EXERCISE
A+B*C
symb
A
+
B
*
C
Postfix
string
A
A
AB
AB
ABC
ABC*+
opstk
+
+
+*
+*
EXERCISE
A*B+C
symb
A
*
B
+
C
opstk
Postfix
string
A
A
AB
AB*
AB*C
AB*C+
*
*
+
+
push(opstk, symb);
else
topsymb = pop(opstk);
Evaluation of Expressions
sumb
A
+
(
B
*
C
(
D
/
E
$
F
)
*
G
)
*
H
postfix string
A
A
A
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
AB
+
C
C
C
C
C
C
C
C
C
C
C
C
C
C
*
*
*
*
*
*
*
*
*
*
*
*
*
D
D
D
D
D
D
D
D
D
D
D
E
E
E
E
E
E
E
E
E
F
F
F
F
F
F
F
$
$
$
$
$
$
/
/
/
/
/
/
G
G
G
G
G
opstk
*
*
*
*
-H
-H*
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
(
(
(*
(*
((- (
(-(
(-(/
(-(/
(- ( /$
(-(/$
((-*
(-*
*
*