0% found this document useful (0 votes)
23 views47 pages

4 DS-Stacks

The document discusses stacks, which are linear data structures that follow the LIFO principle. It covers stack operations like push, pop and peek. It also discusses applications of stacks like expression evaluation, recursion and parsing. The document then explains conversion from infix to postfix notation using stacks.

Uploaded by

Anandkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views47 pages

4 DS-Stacks

The document discusses stacks, which are linear data structures that follow the LIFO principle. It covers stack operations like push, pop and peek. It also discusses applications of stacks like expression evaluation, recursion and parsing. The document then explains conversion from infix to postfix notation using stacks.

Uploaded by

Anandkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

STACK

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.

 Preconditions: Stack has been initialized


and is not full.

 Postconditions: new element is at the top


of the stack.
PUSH ANIMATION
PUSH
void push(int x)
{
if(TOP==SIZE-1)
print “Stack Overflow”;
else
{
TOP=TOP+1;
stack[TOP]=x;
}
}
POP
 Function: Removes top element from stack
and returns it in item.

 Preconditions: Stack has been initialized and


is not empty.

 Postconditions: Top element has been


removed from stack and item is a copy of
the removed element.
POP
POP
int pop()
{
if(TOP==-1)
print “Stack Underflow”;
return;
else
{
item=stack[TOP]
TOP=TOP-1;
return item;
}
}
PEAK OR TOP
 Retrieval of an element from the top of the
stack
PEAK OR TOP
int peak()
{
if(TOP==-1)
print “Stack Underflow”;
return
else
return stack[TOP]
}
STACK OVERFLOW
 The condition resulting from trying to push
an element onto a full 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

You don’t need rules for right and left


associativity

You don’t need parentheses to override


the above rules
ADVANTAGES OF INFIX

It’s easier to see visually what is done


first
HOW TO EVALUATE POSTFIX?
 Going from left to right, if you see an operator, apply
it to the previous two operands (numbers)
 Example:
2 10 4 * 5 / + 9 3 - -
40 6
8
10
4
 Equivalent infix: 2 + 10 * 4 / 5 – (9 – 3)
COMPUTER EVALUATION OF POSTFIX
 Going left to right,
 If you see a number, put it on the stack
 If you see an operator, pop two numbers from the stack, do
the operation, and put the result back on the stack
 (The top number on the stack is the operand on the right)

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

 The result is the only thing on the stack when done


 If there’s more than one thing, there was an error in the
expression
EVALUATION OF POSTFIX EXP
void evaluate(char exp[]) n3 = n1 + n2;

{ break;

char *e; case '-':

int n1,n2,n3,num; n3 = n2 - n1;

e = exp; break;

while(*e != '\0') case '*':

{ n3 = n1 * n2;

if(isdigit(*e)) break;

{ case '/':

num = *e - 48; n3 = n2 / n1;

push(num); break;

} }

else push(n3);

{ }

n1 = pop(); e++;

n2 = pop(); }

switch(*e) printf("\n Result of %s = %d\n\n",exp,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--];
}

You might also like