4 DS-Stacks
4 DS-Stacks
CENTER OF EXCELLENCE IN
ALGORITHMS, KCE
WHAT IS STACK?
It is a linear data structure
It is an ordered group of homogeneous
items of elements.
It is a Last In First Out (LIFO) data
structure
i.e., recently pushed (inserted) data is
popped (deleted) out from the stack.
The last element to be added is the first
to be removed (LIFO: Last In, First Out).
WHAT IS STACK?
Also called as First In Last Out (FILO)
Elements are added to and removed from
the top of the stack (the most recently
added items are at the top of the stack).
USES IN SIMPLEST
ILLUSTRATION
OPERATIONS
Push (insertion)
Pop (deletion)
Peak or top (retrieval of top element)
IsEmpty (check whether stack is empty)
IsFull (check whether stack is full)
PUSH
Function: Adds new element to the top of
the stack.
int isFull()
{
if(TOP==SIZE-1)
return 1;
else
return 0;
}
STACK UNDERFLOW
The condition resulting from trying to pop an
empty stack.
int isEmpty()
{
if(TOP==-1)
return 1;
else
return 0;
}
APPLICATIONS
Function call (i.e., Recursion)
Expression evaluation
Expression Conversion
Parsing
FUNCTION CALL
POSTFIX NOTATIONS
Postfix, or Reverse Polish Notation (RPN)
is an alternative to the way we usually
write arithmetic expressions (which is
called infix, or algebraic notation
“Postfix” refers to the fact that the operator is
at the end
“Infix” refers to the fact that the operator is in
between
For example, 2 2 + postfix is the same as 2 + 2
infix
There is also a seldom-used prefix notation,
similar to postfix
ADVANTAGES OF POSTFIX
You don’t need rules of precedence
2 10 4 * 5 / + 9 3 - -
4 5 3
10 10 40 40 8 9 9 6
2 2 2 2 2 2 10 10 10 10 4
{ break;
e = exp; break;
{ n3 = n1 * n2;
if(isdigit(*e)) break;
{ case '/':
push(num); break;
} }
else push(n3);
{ }
n1 = pop(); e++;
n2 = pop(); }
{
case '+':
FROM INFIX TO POSTFIX
Figure out, using the infix rules, which
operation to perform next
Write the new operand or operands in their
correct places
Write the operator at the end
Postfix does not use parentheses, but we’ll
put them in temporarily to help show the
way things are grouped
FROM INFIX TO POSTFIX
Example: 2 + 10 * 4 / 5 – (9 – 3)
The multiply is done first: 10 4 *
Next, the divide: (10 4 *) 5 /
The addition: 2 (10 4 * 5 /) +
The rightmost subtraction: (2 10 4 * 5 / +) 9 3 -
The leftmost subtraction: (2 10 4 * 5 / +) (9 3 -) -
The final result: 2 10 4 * 5 / + 9 3 - -
INFIX TO POSTFIX CONVERSION
Infix expression
(a+b-c)*d–(e+f)
Postfix expression
INFIX TO POSTFIX CONVERSION
Infix expression
a+b-c)*d–(e+f)
Postfix expression
(
INFIX TO POSTFIX CONVERSION
Infix expression
+b-c)*d–(e+f)
Postfix expression
a
(
INFIX TO POSTFIX CONVERSION
Infix expression
b-c)*d–(e+f)
Postfix expression
a
+
(
INFIX TO POSTFIX CONVERSION
Infix expression
-c)*d–(e+f)
Postfix expression
ab
+
(
INFIX TO POSTFIX CONVERSION
Infix expression
c)*d–(e+f)
Postfix expression
ab+
-
(
INFIX TO POSTFIX CONVERSION
Infix expression
)*d–(e+f)
Postfix expression
ab+c
-
(
INFIX TO POSTFIX CONVERSION
Infix expression
*d–(e+f)
Postfix expression
ab+c-
INFIX TO POSTFIX CONVERSION
Infix expression
d–(e+f)
Postfix expression
ab+c-
*
INFIX TO POSTFIX CONVERSION
Infix expression
–(e+f)
Postfix expression
ab+c-d
*
INFIX TO POSTFIX CONVERSION
Infix expression
(e+f)
Postfix expression
ab+c–d*
-
INFIX TO POSTFIX CONVERSION
Infix expression
e+f)
Postfix expression
ab+c–d*
(
-
INFIX TO POSTFIX CONVERSION
Infix expression
+f)
Postfix expression
ab+c–d*e
(
-
INFIX TO POSTFIX CONVERSION
Infix expression
f)
Postfix expression
+ ab+c–d*e
(
-
INFIX TO POSTFIX CONVERSION
Infix expression
)
Postfix expression
+ ab+c–d*ef
(
-
INFIX TO POSTFIX CONVERSION
Infix expression
Postfix expression
ab+c–d*ef+
-
INFIX TO POSTFIX CONVERSION
Infix expression
Postfix expression
ab+c–d*ef+-
INFIX TO POSTFIX-ALGORITHM
void infixtopsotfix(char exp[])
else
{
{
char *e, x;
while(priority(stack[top])
e = exp; >= priority(*e))
while(*e != '\0') printf("%c",pop());
{ push(*e);
if(isalnum(*e)) }
printf("%c",*e); e++;
else if(*e == '(') }
push(*e); while(top != -1)
else if(*e == ')') {
{ printf("%c",pop());
while((x = pop()) != '(') }
printf("%c", x); }
}
int priority(char x) char stack[20];
int top = -1;
{
void push(char x)
if(x == '(') {
return 0; stack[++top] = x;
}
if(x == '+' || x == '-')
return 1; char pop()
if(x == '*' || x == '/') {
return 2; if(top == -1)
return -1;
} else
return stack[top--];
}