04 Stack
04 Stack
Stack
Stack
A stack is an ordered collection of
items inot which new items may be
added and from which items may be
deleted at one end, called the top of
the stack.
push
pop
Stack operations
push
pop
This stack is called last-in first-out
stack LIFO.
2
Representation of Stack
In 2 ways stack can be represented:
1. Using one-dimensional ARRAY
2. Using single linked list
ARRAY REPRENTATION
3
A[0]
4
A[1]
BASE=0
5
A[2]
6
A[3]
TOP
.
A[max-1]
Upper
limit=
MAX-1
Stack
Representing stacks
s.item[7]
s.top
5
Top=1
Top=0
Top=3
Stack Underflow
Top=-1
6
LINKED LIST
REPRESENTATION OF STACK
push(1) push(3)
1
[213]
push(9)
push(11)
119
3
213
201
9
TOP
119
290
11
201
Stack Operations
1. void Push (S,item)
2. stacktype Pop(S)
3. stacktype Stacktop(S)
4. boolean Isempty(S)
5. boolean Isfull (S)
8
Stack
Push and pop algorithms
Push x
if stack isfull then
stop
else
add x onto a stack
end
Pop
if stack isempty then
stop
else
remove item from a stack
end
Stack
Programming push operation
void push(stack &st, int x)
{
if(st.top == SIZE-1)
{
cout<< Stack overflow;
exit(1);
}
else
st.items[++st.top] = x;
}
7
6
5
4
3
2
1
0
-1
Function call
push(s, 4);
push(s, 33);
10
Stack
Programming pop operation
int pop(stack &st)
{
if(st.top == -1)
{
cout << Stack underflow;
exit(1);
}
else
return st.items[st.top--];
}
12
8
-5
33
4
4
Function call
y = pop(s);
n = pop(s);
11
// stack implementation
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
const MAX = 10;
struct stack{
int items[MAX];
int top;
};
void push(stack &, int); //prototype for push
int pop(stack &);
//prototype for pop
void main ()
{
stack s;
int item;
char ch;
s.top = -1;
// intializing top to -1
do{
cout<<"select : \n A --- push an item\n B --- pop\n C --- exit\n =>";
cin >> ch; //taking input
ch = toupper(ch);
if (ch=='A') //if PUSH
{
12
13
Stack Applications
1.
EXAMPLE
R
POP one by
one till the
stack is empty
I
N
15
16
PARANTHESIS BALANCING
UNBALANCED EXPRESSION:
((A+B)*(C-D
BALANCED EXPRESSION:
((A+B)*(C-D))
17
18
MECHANICAL EVALUATION OF
INFIX EXPRESSION
1. Infix expression given by the user is
first converted to postfix expression by
the system, then evaluation takes
place.
2. During the conversion of infix
expression to postfix, stack is used
internally by the system.
19
ARITHMETIC EXPRESSION
INFIX
a operator b
POSTFIX
operator a b
PREFIX a b operator
a,b are two operands
20
EXAMPLE
(infix to PREFIX CONVERSION)
A+B*C/D-E is infix arithmetic expression
In Prefix Expression
step 1:A+(*BC)/D-E
step 2:A+(/*BCD)-E
step 3:(+A/*BCD)-E
Finally , -+A/*BCDE
21
EXAMPLE
(infix To POSTFIX CONVERSION
A+B*C/D-E is infix arithmetic expression
Performing calculations
To evaluate an expression, such as
1+2*3+4, you need two stacks: one for
operands (numbers), the other for
operators: going left to right,
CONTINUED
23
Example: 1+2*3+4
25
27
28
29
30
31
32
What is Recursion?
It is the use of functions that call
themselves. A recursive function
repeatedly calls itself directly or
indirectly until a termination
condition is met.
By calling itself repeatedly a
recursive function achieves
repetition of a code section.
33
Usually
functions
calls
are
computationally costly; therefore
recursion is recommended only in
naturally recursive cases such as
computing the factorial of a
number.
34
Factorial of a number?
The factorial of
5 = !5
or 5 x 4 x 3 x 2 x 1
10 = !10
or 10 x 9 x 8 x 7 x 6 x 5 x 4
x3x2x1
In the above case 5! Could be expressed
as
5 * (4!)
Or
5 * 4* (3!)
Or 5 * 4 * 3 * (2!)
Or 5 * 4 * 3 * 2 * 1
So you can see that once we know how to
work out the factorial it is just a matter of
calling that process repeatedly.
35
36
RECURSION VERSION OF A
FACTORIAL
Int factorial ( int number)
{
if (number <= 1)
{
printf(\n %d, number);
return 1;
}
else
{
printf(\n %d , number);
return (number * factorial(number 1);
}
}
e.g- 4x3x2x1=24
37
Iteration Vs Recursion
Both involve a termination test
With Iteration until the loop-continuation
condition fails
With Recursion until a value is less than
to equal to 1
Recursion has many negatives it can be
expensive both in processor time and
memory space. Each recursive call causes
another copy of the function variables to be
38
created.
39
Iteration Vs Recursion
So why choose recursion ?
Example: factorial(4)
int factorial (int n)
{
if (n == 1)
return 1;
else
return n *
factorial (n - 1);
}
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
n
return value
47
n
return value
n
return value
48
Value Returned
in the First Activation
n
return value
4
24
49