0% found this document useful (0 votes)
25 views16 pages

Stack Notes unit-IV

Stack notes imp

Uploaded by

Himesh Kochale
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)
25 views16 pages

Stack Notes unit-IV

Stack notes imp

Uploaded by

Himesh Kochale
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/ 16

Unit-III STACK Marks-20

Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)


3.1 Introduction of Stack
Q.State the principle of stack with basic operation?
Q.Define stack.
Q.What is stack?
Q.State importance of top pointer in stack.?
 Stack is LIFO(Last in first Out)structure .
 It is an ordered list of the same types of elements.
 A stack is a linear list where all insertion and deletion are permitted only at one end of list.
 When elements are added to stack it grows at one end. similarly when elements are deleted from
stack ,It shrinks at the same end.
Fig shows how elements are inserted and how elements deleted

UNIT-III Page 1
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.2 Stacks as an ADT (abstract data type)

Q.Explain stack as an abstract data type.


Q.Describe the term stack overflow and stack underflow with example.
Typedef struct stack
{
int data[size];
int top;
} stack;

 size is a constant ,maximum number of elements that are stored.


 Stack is LIFO struture.stack can be represented using an array.
 One dimensional array can be used to hold elements of stack.
Another variable top is used to keep track of the index of top most element

3.3 Operation on Stacks


Q.State the principle of stack with basic operation ?
Q.What are the basic operation performed on stack?
Q.Explain four primitive operations on stack.
Q.Define push and Pop operations of stack.

1)initialize()-make a stack empty.


2)Empty ()-To determine if a stack is empty or not.
3)Full()-To determine if a stack is full or not.
4)Push()-if a stack is not full then push new elements at top the stack.
5)Pop()- if a stack is not empty then pop elements from its top of the stack.

3.4 Array Representation of stack


Q.Explain the concept of representing stack through arrays
Q.Describe the representation of stack using arrays.

Typedef struct stack


{
int data[size];
int top;
} stack;

 Initially top is set to -1.


 Stack with top as -1 is an empty stack.
 When the value of top becomes MAX-1 after a series of insertion, it is full.
 After “push” operation top=top+1 (stack growing)
 After “POP” operation top=top-1(stack shrinking)

UNIT-III Page 2
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.4.1 ‘C’ Function for primitive operations on stacks.
Q.Explain push and pop operation on stack using array representation.?
Q.Write a procedure to push element on stack?
Q.Write a C program to implement PUSH and POP function in stack as an array.
Q.What is the affect of push and pop operations on to the stack?
Q.Explain the concept of representing stack through arrays.?
Q.Explain operation on stack using array.

1)Void initialize(stack *s)


void initialize(stack *s)
{
stop=-1;
}
Initially stack is empty.
Where s is pointer to stack type variable.
Stack type variable are data[] and top

2)int empty(stack *s)


int empty(stack *s)
{
If(stop==-1)
return (1); //stack is empty
return (0); //stack is not empty
}
Above function check whether stack is empty or not.if it return 1 then stack is empty or if it return 0 then
stack is not empty.

3) int full(stack *s)


int full(stack *s)
{
if (stop= = MAX-1)
return(1);
return(0);
}
Above function check whether stack is full or not.if it return 1 then stack is full or if it return 0 then stack
is not full.

4) Void push (stack *s,int x)


void push (stack *s,int x)
{
stop=stop+1;
sdata[stop]=x;
}
The function push() increase the value of the top by 1 and the x stores the element at top location in data
array.

UNIT-III Page 3
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
5)int pop(stack *s)
int pop(stack *s)
{
int x;
x=sdata[stop];
stop=stop-1;
}
The function pop() return the top element from the stack. it also reduce the value of top by 1.

3.5 Operation on stack considering overflow and underflow[array implementation]


Q.Explain the term overflow and underflow with respect to stack .use suitable data and diagram.
Q.Write procedure to push an element on stack .also give meaning of stack overflow term.
Q.Give meaning of stack overflow
Q. Explain stack overflow and underflow.
Q.Describe following terms with suitable example
a)Stack overflow
b)Stack underflow

Stack overflow
Stack overflow is caused by insertion in stack that is already full.
C function for operation on stack
void push()
{
If(stop==MAX-1)
printf(“\ n overflow!!cannot be inserted”);
else
{
stop=stop+1;
sdata[stop]=x;
}
}

Stack underflow
Stack underflow is caused by deletion from an empty stack.
int pop(stack *s)
{
int x;
if(stop = = -1)
printf(“\ n underflow!!cannot be deleted”);
else
{
x=sdata[stop];
stop=stop-1;
}
return(x);
}

UNIT-III Page 4
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Reversing stack list
Q.Explain how stack can be used to reverse a list using suitable example.
 Stack is LIFO(Last in first Out)structure .
 It is an ordered list of the same types of elements.
 A stack is a linear list where all insertion and deletion are permitted only at one end of list.
 Reversal of list is application of stack.
 In reverse list element is push one by one into stack ,once all elements are push(inserted) into
stack then last element comes first at the time of pop(delete) operation. because of in stack first
topmost element is pop, hence reversal of list occurs.
We push element 3,5,7 in stack

We pop element in hence reversal of list occurs.

UNIT-III Page 5
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3.6 Application of stack
Q.Explain any two application of stack.
Q.Write down application of stacks.
Q.State any four application of stack.
Q.Explain any three application of stack in details with example.
Application of stack
1)Expression conversion
a)Infix to postfix b)infix to prefix
c)Postfix to infix d)prefix to infix
2)Expression evaluation
3)Parsing
4)Simulation
5)Function call

3.6.1 Expression Representation


Three type of Expression Representation
a)infix
x+y operator between operands
b)prefix
+xy operator before operands
c)postfix
xy+ operator after operands

Expression Representation
a)Evaluation of an infix expression
b)Evaluation of a postfix(reverse polish) expression
c)Evaluation of a prefix (polish notation) expression

a)Evaluation of an infix expression


 Infix expression are evaluated left to right but operator precedence must be taken into account.
 To evaluate x+y*z
 y and z will be multiplied first and then it will be added to x.

b)Evaluation of a postfix(reverse polish) expression


To understand the evaluation of postfix expression
let us take example
532 +*
1) Find an operator from left to right and perform operation.
2) First operator is * and therefore 3 * 2=6
3) Expression become 5 6+
4) Next operator is + and therefore 5+6=11
5) Expression become 11

UNIT-III Page 6
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)

UNIT-III Page 7
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)

UNIT-III Page 8
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Homework
Evaluate the following postfix expressions using stack
1) 5 4 6 + * 4 9 3 / + *
Ans :-350
2) 7 5 2 + * 4 1 1 + / -
Ans :-47
3)6 ,2,3,+,-,3,8,2,+,+,*,2, ,3,+
Ans :-172

c)Evaluation of a prefix (polish notation) expression


Q.Discuss polish notaion of arithmetic expression in application of stack.
To understand the evaluation of prefix expression
let us take example
+5*32
1. Find an operator from right to left and perform operation.
2. + 5 * 3 2
3. First operator is * and therefore 3 * 2=6
4. Expression become + 5 6
5. Next operator is + and therefore 5+6=11
6. Expression become 11

UNIT-III Page 9
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)

UNIT-III Page 10
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Conversion of expression from infix to postfix
Precedence of operator (Priority of operator)
1) ( )
2) ^ exponential
3) * /
4) + -
5) No two operator of same priority can stay together in the stack column.
6) ( ) if any operator come between open and close bracket then that operator is pop out from
stack
Q.Convert infix expression (A+B/C*(D+E)-F) into to postfix expression

UNIT-III Page 11
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q.Convert infix expression A*(B+C)/D-G into to postfix expression

Solve following example


Convert the following arithmetic infix expression into postfix and show stack status after every step at
each step.
1)(A+B)*D+E/(F+A*D)+C
2)(A+B*C/D-E+F/G/(H+I))
3)(A+B)*C-D/E*(F/G)
4)A+B C*(D/E)-F%G
5)(P+(Q*R-(S/T U)*V)*W)
6) ((A+B)*(C-D))
7)P*Q R-S/T+[U/V]
8)A+B-C*D/E+F$G/(I+J)

Conversion of expression from infix to Prefix


Conversion of expression from infix to Prefix form with help of following steps.
1. Reverse the infix expression.
2. Make every ‘(‘ open bracket as ‘)’ close bracket and ‘)’ closing bracket as ‘(‘ opening bracket.
3. Convert the modified to postfix form.
4. Reverse the postfix expression.
Example:
(A+B^C)*D+E^5
Step1: Reverse the infix expression.
5^E+D*) C^B+A (
Step2: Make every ‘(‘opening bracket as ‘)’ closing bracket and ‘)’ closing bracket as ‘(‘ opening
bracket.

UNIT-III Page 12
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
5^E+D*(C^B+A)

Step3: Convert the modified to postfix form

4. Reverse the postfix expression


5E^DCB^A+*+
Above expression in form postfix ,we have to reverse it
+*+A^BCD^E5

Home work
Conversion of expression from infix to Prefix
1)(A-B/C)*(D*E-F)
2) ((A+B)*(C+D))/(E+F)
3)(q-p)*(y+x)

Conversion of expression from Postfix to infix


P=AB-C+DEF-+/
We are start scanning from left to right
P=AB-C+DEF-+/
P=(A-B)C+DEF-+/
P=[(A-B)+C]DEF-+/
P=[(A-B)+C]D(E-F)+/
P=[(A-B)+C][D+(E-F)]/
P=[(A-B)+C] / [D+(E-F)]
= (A-B)+C / D+(E-F)

UNIT-III Page 13
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Q:Consider the following arithmetic expression P, written in postfix notaion.
P:12,7,3,-,1,2,+,5,+,*,+
P= 12,7,3,-,1,2,+,5,+,*,+
P= 12,(7-3),1,2,+,5,+,*,+
P= 12,(7-3),(1+2),5,+,*,+
P= 12,(7-3),(1+2+5),*,+
P= 12,(7-3)*(1+2+5),+
P= 12+(7-3)*(1+2+5) INFIX
P=12+4*8
P=12+32=44

Homework
Q:Consider the following arithmetic expression P, written in postfix notaion
1) P: 5, 6, 2, +,*, 12, 4,/,-
A) 37 B)73 C) 44 D)50
2)P:4,2, ,3,*,3,-,8,4,/,1,1,+,/,+
A) 46 B)73 C) 87 D)88
3)P:10,2,*,15,3,/,+,12,3,2, ,+,+
A) 46 B)77 C) 89 D)88
4)P:12,7,3,-,/,2,1,5,+,*,+
A) 15 B)24 C) 30D)32

Conversion of expression from Prefix to infix


P= -+/A^BC*DE*AC
We are start scanning from right to left
P= -+/A^BC*DE*AC
P= -+/A^BC*DE(A*C)
P= -+/A^BC(D*E)(A*C)
P= -+/A(B^C)(D*E)(A*C)
P= -+/A(B^C)(D*E)(A*C)
P= -+[A/(B^C)](D*E)(A*C)
P= -[A/(B^C)+(D*E)](A*C)
P= A/(B^C)+(D*E)-(A*C)

3.8 Introduction to Recursion


Q.What is recursion?
Q.Define recursion?
Q.How stack is used in recursion? Describe with suitable example?
Q.Recursion is one of the application of stack YES/NO? Explain it for calculating the factorial of a
number 5 using recursion
 Recursion is fundamental concept of mathematics.
 When function is defined in term of itself then it is called a recursive function.
 When function call itself is also called as recursive function.
 Consider the definition of factorial of a positive integer n.

 Factorial(n)= 1 if(n=0)
n*factorial(n-1) otherwise

UNIT-III Page 14
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)

Function ‘factorial ()’ is defined in term of itself for n>0.


Value of function at n=0 is 1and it is called the base
Recursive definition of factorial of an integer number.

f(n)=1 if(n = =0)


F(n)=n*f(n-1) otherwise

C function for finding factorial


Int factorial (int n)
{
if(n= =0)
return (1);
return (n*factorial(n-1)); otherwise
}

Tower of Hanoi problem


Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings
is as depicted −

These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the
larger one. There are other variations of the puzzle where the number of disks increase, but the tower
count remains the same.

Rules

 Only one disk can be moved among the towers at any given time.

 Only the "top" disk can be removed.

 No large disk can sit over a small disk.

These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the
larger one. There are other variations of the puzzle where the number of disks increase, but the tower
count remains the same.

UNIT-III Page 15
Unit-III STACK Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Rules

 Only one disk can be moved among the towers at any given time.

 Only the "top" disk can be removed.

 No large disk can sit over a small disk.

Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation shows that a
puzzle with 3 disks has taken 23 - 1 = 7 steps.

UNIT-III Page 16

You might also like