DS Unit 2
DS Unit 2
OUTLINE
> Stack
PUSH, POP & PEEP Operations
> Applications of stack
> Polish Expression & their Compilation
> Infix & Postfix & Prefix
> What is Stack
• A linear list which allows insertion and deletion of an element at one end only is called stack.
• The insertion operation is called as PUSH and deletion operation as POP.
• The most and least accessible elements in stack are known as top and bottom of the stack respectively.
• Since insertion and deletion operations are performed at one end of a stack, the elements can only be
removed in the opposite orders from that in which they were added to the stack; such a linear list is
referred to as a LIFO (last in first out) list.
• In a stack any operations(push/pop) has to be done only on a top
Top
(push)
4 1
3 3 3
2 9 9 9
1 7 7 7 7
0 5 5 5 5 5
Operations: 4) Change :
Overflow : If stack size is 4 and elements is 5 then last element(5th element) is not stored in array. Its called stack is
full(overflow)
Underflow : If stack size is 4 and no elements in stack for pop operation then its called stack is underflow
5) Display : Printing of the content of stack after push and pop operations
2) POP
if(top == -1
top = 0 it means stack is empty;
{
else
printf("Underflow");
top_item = stack(top); top = top -1;
return 0;
end;
}
else
{
Implementation
return stack[top-- ];
} int peek()
} {
if (top == -1)
{
Algorithm printf("Underflow");
return 0;
Begin }
2) PEEK
if else
top = -1 it means stack is empty {
else return stack [top];
item = stack[top] return item }
End }
> C Program for STACK Using Arrays
> Polish Expression & their Compilation
E = ( A + B * C / D - E + F / G / ( H + I ))
Rank (E) = R(A) + R(+) + R(B) + R(*) + R(C) + R (/) + R(D) + R(-) + R(E) + R(+) + R(F) + R(/) + R(G)+ R(/) + R(H) + R(+) + R(I)
Rank (E) = 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1
Rank (E) = 1
Any Expression is valid if Rank of that expression is 1
> Arithmetic Expression Evolution of Stack
1) Infix : if operator placed between the operands its called.. (Ex: A+B, A*B)
2) Prefix : if operator placed before the operands its called.. (Ex: +AB, *AB)
3) Postfix : if operator placed after the operands its called.. (Ex: AB+, AB*)
1) Precedence : precedence is nothing but priority, means which operator will take the operand first
(Ex : a + b * c)- multiplication has high priority as compare to “+” so expression in evolute
first multiplication (a+(b*c))
2) Associativity : means a rule where operator with the same priority appear in expression
(a+b-c)- here both “+” and “-” have the same priority (a+b)-c we can evolute whatever first
sign will come if (a-b+c) then (a-b)+c
*+AB*BD AB+BD**
1) / + A * B D – B C
1) ABD * + BC - *
Left most operator put between / + A * B D (B - C)
left sides of two operands
A (B*D) + BC - * / + A (B * D) (B - C)
A + (B*D) BC - * / A + (B * D) (B - C)
AB+CD-*
> Infix to Prefix without Stack Implementation
1) (A + B) * C / D + E ^ F / G + R5 R4 (Put the all values)
R1 * C / D + E ^ F / G + * + AB / CD / ^ EFG
R1 * R3 + R4
* R1 R3 + R4
Let Assume *R1R3 = R5
R5 + R4
+ R5 R4 Prefix
> Infix to Postfix without Stack Implementation
1) (A + B) * C / D + E ^ F / G R5 R4 + (Put the all values)
R1 * C / D + E ^ F / G AB+CD/*EF^G/+
R1 * C / D + E F ^ / G
Let Assume EF^ = R2
R1 * C / D + R2 / G
R1 * C D / + R2 / G
Let Assume CD/ = R3
R1 * R3 + R2 / G
R1 * R3 + R2 G /
Let Assume R2G/ = R4
R1 * R3 + R4
R1 R3 * + R4
Let Assume R1R3* = R5
R5 + R4
R5 R4 + Postfix
> Infix to Postfix using Stack Implementation
1) (A + B / C * (D + E) – F )
• Step 4 in / is print
before + because of Symbol Stack Postfix Rank Rule 1 : Two
high precedence same precedence
Step 1 ( ( 0 operator can’t
Step 5 in (+/ in / is Step 2 A ( A 1
•
stay together in
pop because * is
print in stack and Step 3 + (+ A stack
1
same precedence
operator can’t stay Step 4 B (+ AB 2
together
2 Rule 2 : Highest
Step 4 / (+/ AB
precedence always
• Step 11 in + is Step 5 C (+/ POP ABC 3 print right side of
pop because of
closing bracket, Step 6 * (+* ABC/ 2 lowest precedence
after that * is also
2 but lowest
pop because (step Step 7 ( (+*( ABC/
precedence does
12) – is not
Step 8 D (+*( ABC/D 3 not print before
printed in stack
before * because Step 9 + (+*(+ ABC/D 3 highest
of low precedence
Step 10 precedence
compare to *, after E (+*(+ ABC/DE 4
that + is also pop Step 11 ) (+*(+) POP ABC/DE
because same
4
precedence can’t Step 12 - (- ABC/DE+*+ 1 Rule 3 : Follow
stay together BODMAS rules
Step 13 F (- ABC/DE+*+F 2
Step 14 ) (-) ABC/DE+*+F- 1
> Infix to Prefix using Stack Implementation Class Task
(a + b ^ c ^ d) * ( e + f / d )
Class Task :
• Explain Difference between Stack and Queue.
• Consider the following arithmetic expression P, written in postfix notation.
Translate it in infix notation and evaluate. P: 12, 7, 3, -, /, 2, 1, 5, +, *, +
Class Task Answer :
• The process of allocating memory at run time is known as dynamic memory allocation
• C does not have this facility, there are four library routine known as “memory management function” that
can be used for allocating and freeing memory during program execution.
• These function are: malloc(), calloc(), realloc(), free()
• These function are defined “stdlib.h” (standard library)
1) malloc() : 2) calloc() :
1) free() :
1) Infix to Postfix :
THANK
YOU
Prof. Pandya Khushi