0% found this document useful (0 votes)
2 views27 pages

DS Unit 2

The document provides an overview of stack data structures, including operations such as PUSH, POP, and PEEP, as well as their applications and algorithms. It explains the concepts of infix, prefix, and postfix expressions, along with operator precedence and associativity. Additionally, it includes examples and algorithms for converting between different expression notations and handling stack operations using arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

DS Unit 2

The document provides an overview of stack data structures, including operations such as PUSH, POP, and PEEP, as well as their applications and algorithms. It explains the concepts of infix, prefix, and postfix expressions, along with operator precedence and associativity. Additionally, it includes examples and algorithms for converting between different expression notations and handling stack operations using arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DATA STRUCTURE

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

Operations: 1) PUSH(Insert Elements)


2) POP(Delete Elements)
3) PEEP
4) Change
5) Display
Example : {5, 7, 9, 3, 1}
Operations: 1) PUSH(Insert Elements)

Top
(push)
4 1
3 3 3
2 9 9 9
1 7 7 7 7
0 5 5 5 5 5

Operations: 2) POP(Delete Elements) Top


(pop)
1
3 3
9 9 9
7 7 7 7
5 5 5 5 5
Operations: 3) PEEP(Top Elements) : Find an item from the top of the stack

• When we need to return the value of the topmost element


of the Stack without deleting it from the Stack, the Peek
operation is used. This operation first checks if the Stack is
empty, i.e., TOP = NULL; if it is so, then an appropriate
message will display, else the value will return.

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

Overflow : 7 Elements: {1,3,5,7,9}


7 No element for pop
5 Last element is not operation in stack it
5 5
3 insert in stack because 3 means stack is
3 3
1 stack is overflow underflow
1 1 1 1
Stack Size - 4
Operations: 5) Display : Printing the content after push and pop operations

> Algorithms of all operations

1) PUSH (S, TOP, X) :


• This procedure inserts an element X to the top of a stack.
• Stack is represented by a vector S containing N elements.
• A pointer TOP represents the top element in the stack.

1. [Check for stack overflow]


If TOP ≥ N
Then write (‘STACK OVERFLOW’)
Return
2. [Increment TOP]
TOP ← TOP + 1
3. [Insert Element]
S[TOP] ← X
4. [Finished]
Return
2) POP (S, TOP) :
• This function removes & returns the top element from a stack.
• Stack is represented by a vector S containing N elements.
• A pointer TOP represents the top element in the stack.
1. [Check for stack underflow]
If TOP = 0
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Decrement TOP]
TOP ← TOP - 1
3. [Return former top element of stack]
Return(S[TOP + 1])

3) PEEP (S, TOP, I) :


1. [Check for stack underflow]
If TOP-I+1 ≤ 0
Then write (‘STACK UNDERFLOW’)
Return (0)
2. [Return Ith element from top
of the stack]
Return(S[TOP–I+1])
4) CHANGE (S, TOP, X, I) :
• This procedure changes the value of the Ith element from the top of the stack to X.
• Stack is represented by a vector S containing N elements.

5) Display : Printing of the content of stack after push and pop operations

Reference Sites for implement


stack using array:
GeeksforGeeks & Javatpoint
Algorithm Implementation
begin void push (int value, int n)
if {

1) PUSH top = n it means the stack is full


else
if (top == n )
printf("\n Overflow");
else
top = top + 1 stack (top) = new_item;
end {
top = top +1;
stack[top] = val;
}
}
Algorithm Implementation
int pop ()
Begin
{
if

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

1) Infix : An infix expression is a mathematical


expression where an operator is written between
two operand
Ex: (X + Y) is an infix operation
(Operand, Operator, Operand)
Evaluating Infix Expression

2) Prefix(Polish Notation) : The prefix 3) Postfix(Reverse Polish Notation) : The


expression as the name suggests has the operator postfix expression as the name suggests has the
placed before the operand is specified. operator placed right after the two operands.
Ex: +XY EX: XY+
Operator, Operand, Operand Operand, Operand, Operator
> Finding Rank of any Expression

E = ( A + B * C / D - E + F / G / ( H + I ))

Note: R = Rank, Rank of Variable(A,B..) = 1, Rank of operators(+,-,*,/...) = -1

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*)

> Operator Precedence & Associativity

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

Operator Precedence Associativity


^ Highest Right Asso.
*, / Second Highest Left Asso.
+, - Lowest Left Asso.
Infix 1)
2)
Infix to Prefix
Infix to Postfix
3) Prefix to Infix
Num of Operations 4) Postfix to Infix
5) Prefix to Postfix
Prefix Postfix 6) Postfix to Prefix

1) Infix to Prefix : 1) (A+B) * (B*D) 2) Infix to Postfix : 1) (A+B) * (B*D)


Prefix : Operator Postfix : Operator
after Operand
before Operand
(+AB) * (*BD) (AB+) * (BD*)

*+AB*BD AB+BD**

2) ((A+B)*C) - D Class Task : Class Task : 1) (A+B*D) * (B-C)


First we convert this function

((+AB)*C) - D • (A-B)+C • (a+b)*c because multiplication sign


precedence is higher then
• A+B*(A-B) • a/b + c/d addition

(*+ABC) - D • A*B(C/D)+E (A+(BD*)) * (BC-)


(ABD*+) * (BC-)
-*+ABCD ABD*+BC-*
3) Postfix to Infix : 4) Prefix to Infix :
Algorithm : Step 1 - Begin Algorithm : Step 1 - Begin
Step 2 - Select the left most operator Step 2 - Select the right most operator
with two left operands with two right operands
Step 3 - Convert it into infix expression Step 3 - Convert it into infix expression
Step 4 - Go to step 2 until the whole Step 4 - Go to step 2 until the whole
notation is not convert notation is not convert
Step 5 - Exit Step 5 - Exit

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)

A + (B*D) (B-C) * Class Task : A + (B * D) / (B - C) Class Task :


• AB + CD + * • + / AB / CD
A + (B*D) * (B-C) • abc+* • -*A+BC–DE

Note : Left to Right Note : Right to Left


5) Prefix to Postfix : 6) Postfix to Prefix :
1) * + A B - C D 1) A B - C + Class Task :
Class Task :
-AB+C
* + A B (C D -) +-ABC AB+C
• *-A/BC-/AKL
* (A B +)(C D -) Note : Right to Left Note : Left to Right

AB+CD-*
> Infix to Prefix without Stack Implementation
1) (A + B) * C / D + E ^ F / G + R5 R4 (Put the all values)

+AB*C/D+E^F/G + * R1R3 / R2G


Let Assume +AB = R1

R1 * C / D + E ^ F / G + * + AB / CD / ^ EFG

R1 * C / D + ^ E F / G Infix to Prefix Follow BODMAS Rules or Precedence Rule


Let Assume ^EF = R2
B - Bracket
()
R1 * C / D + R2 / G O - Of ^
D - Div
R1 * /C D + R2 / G M - Multi
/
Let Assume /CD = R3 A - Add *
R1 * R3 + R2 / G
S - Sub +
-
R1 * R3 + / R2 G
Let Assume /R2G = R4

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)

AB+*C/D+E^F/G R1R3* R2G/ +


Let Assume AB+ = R1

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 :

> Dynamic Memory Allocation

• 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

You might also like