DS Stack Next Part
DS Stack Next Part
h
header file:
की
stdio
сто
process.h
₤ include "STACK-b"
word
{
Main ()
stack s;
int num
Thit (&s);
(save as # define MAX 10
STACK⋅ in do
} stack.
woid Thit (stack &s)
}
ebe
زجه
S
६
}
stop =
-را-
}
Beton (1); return (0);
int full (Stack *s)
3
PJ (" stack
overflow");
exit (0);
33
Pf ("\n");
while() empty (45))
{
num=rop ({s);
{
if (sytop ==MAX-1)
sedm (1);
retro (0);
द
noid push (stack As
5
Tnt a)
s" top=s->top +); sdata [stop] = x;
Stack data structure is very useful. Few of its usages are given below:
1.
Expression conversion
5.
6.5.1
Simulation of recursion
Function call
Expression Representation :
abc & +
a+b*c→ Bbby. 9+
(b*c)
9+ (bc*)
a (bcx)+
: polish
notations
abc ++ →
posthy
There are three popular methods for representation of an expression.
(a) infix
x +y
operator between operands
(b)
prefix
+xy
operator before operands
(c)
postfix
xy+
operator after operands
Example :
Infix
*
x+yᏃ
Prefix
+x*yz
Postfix x y z*+
(a+b)*C →They
(ab+)xc [ab+) <*
ab+c+ postho
(a)
Evaluation of an infix expression :
x+(y+3)
SHE
Brit
(++ ab + c +
Infix expressions 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.
Infix expressions are not used for representation of an expression inside computer, due to
additional complexity of handling of precedence.
(b)
Evaluation of a prefix expression :
+5*32
first operator
First operator is * and therefore, 3 * 2 are multiplied expression becomes +
56.
(c)
Evaluation of postfix expression :
532*+
Find the first operator from left to right and perform the
operation
first operator is * and therefore, 3 and 2 are multiplied
expression becomes 5 6+
first operator is + and therefore, 5 and 6 are added
expression becomes : 11
Prefix and postfix expressions are free from any precedence. They are more suited for
mechanization. Computer uses postfix from for representation of an expression.
6.5.2
Evaluation of a Postfix Expression using a Stack:
Given expression
653+9*+
Stack
↑
First token is an operand, push 6 on the stack
53+9*+
6
3+9*+
5
6
Next token is and operand, push 3 on the stack
+9*+
3
Next token is an operator, pop two operands 3 and 5, add them and push the result on the
stack
9*+
8
#include<conio.h>
int op1,op2,val;
init(&s);
printf("\nEnter the expression (i.e 59+3*)\nsingle digit operand and operators
only:"); while((x=getchar())!='\n')
{
if(isdigit(x))
push(&s,x-48);
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
}
}
val=pop(&s);
printf("\nvalue of expression =
%d",val);
if(x='-')
return(op1-op2);
if(x='*')
}
int empty(stack *s)
{
if(s->top== -1)
return(1);
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
return(0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
x=s->data[s->top];
}
int x;
s->top-s->top -1;
return(x);
Output
/Half
૮
An element which is to be searched from the list of elements stored in array A[0...1] is called
KEY element.
Let A[m] be the mid element of array A. Then there are three conditions that needs
to be tested while searching the array using this method
1. If KEY=A[m] then desired element is present in the list.
2. Otherwise if KEY<A[m] then search the left sub
list
3. Otherwise if KEY>A[m] then search the right sub list.
Example
Consider a list of elements stored in array A as
0
10
2
3
4 5
6
10
20
30
40
50
60
70x1
↑
↑
High
Low
(low + high)/2
m
=
(0 + 6)/2
= 3
m
?
Then Check A[m] = KEY
#define SIZE 10
int n;
void main()
{
int A[SIZE],KEY,i,flag,low,high;
int BinSearch(int A[SIZE),int KEY,int low,int high); clrscr();
(5
scanf("%d",&A[i]);
printf("\n Enter the element which is to be searched");
scanf("%d",&KEY);
low=0;
high=n-1;
flag-BinSearch(A,KEY,low,high)
;
printf("\n The element is at A[%d] location",flag);
getch():
}
}
m=(low+high)/2; //mid of the array is obtained
if(KEY==A[m])
return m;
else if(KEY<A[m])
else
BinSearch(A,KEY,low,m-1);//search the left sub list
а
р
т
и
р
we all ITP.
It divide prob m
Glend calfocus
Curso Do:
Best case
search ele is mid ele.
p
ar
a
O(1).