0% found this document useful (0 votes)
14 views20 pages

DS Stack Next Part

Ds stack notes

Uploaded by

avhadmukta218
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)
14 views20 pages

DS Stack Next Part

Ds stack notes

Uploaded by

avhadmukta218
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/ 20

Convert decimal to binary wing stack.

h
header file:

"save as (Decimal.c in bin


#
वी

की
stdio

сто

process.h

₤ include "STACK-b"

word

{
Main ()

stack s;
int num

Thit (&s);
(save as # define MAX 10
STACK⋅ in do

typedef struct stack


int data [MAX];
int top;

} stack.
woid Thit (stack &s)

Pf ("Enter decimal no!"); sif


("Vid", &num);
while (loum !=0))
‫م‬
if (! full (&s))
{
push (ts, num 922)
bum - num %2

}
ebe

‫زجه‬
S


}
stop =
-‫را‬-

int empty (stack


*s)
3
if (stop ==-1)

}
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;

Pf ("y.d", num); } getch(); }


3
The pop (Stack *s)
{ int x; x=s+data[sytop); 3- top = stop -~);
retim (2); }

Scanned with CamScanner


23
6.5
Applications of Stack:

Stack data structure is very useful. Few of its usages are given below:
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.

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 :

To understand the evaluation of prefix expression, let us consider an example

+5*32

find an operator from right to left and perform the operation.


+5*32

first operator
First operator is * and therefore, 3 * 2 are multiplied expression becomes +
56.

Scanned with CamScanner


First operator is + and therefore, 5 and 6 are added.
expression becomes: 11

(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

Initially stack is empty.


First token is an operand, push 6 on the stack

53+9*+
6

Next token is and operand, push 5 on the stack

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

Next token is an operand, push 9 on the


stack
*+
9

Scanned with CamScanner


Program 6.5.1: Program for evaluation of a postfix expression
Assumption-- primary operators -,+,*,/,%' operand -- a single digit
#include<stdio.h>

#include<conio.h>

#define MAX 20 typedef


struct stack
Scanned with CamScanner
{
int data[MAX];
int
top; }stack;

void init(stack *);


int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int evaluate(char x,int op1,int op2);
void main()
{
stack s;
char x;

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

int evaluate(char x,int op1,int op2)


return(op1+op2)
;
{
if(x=='+')

if(x='-')
return(op1-op2);
if(x='*')

Scanned with CamScanner


return(op1*op2);
if(x=='/')
return(op1/op2);
if(x=='%')
return(op1%op2);
}
void init(stack *s)
{
s->top= -1;

}
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

Enter the expression(i.e.


59+3*)
single digit operand and operators
only:653+9*+
value of expression = 78

Scanned with CamScanner

/Half

2.3 Binary Search Half internal search/


logarithmic
search
Binary search is an efficient searching method. While searching the elements using this
method the most essential thing is that the elements in the array should be sorted
one.

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.

This can be represented as


A[0] ... A[m-1] A[m] A[m+1] ... A[n-1]

Search here if KEY? Search here if


KEY<A(m)
KEY>A(m)

Let us take one example to understand this method

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

The KEY element (i.e. the element to be searched) is 60.


Now to obtain middle element we will apply a formula :
m
=

(low + high)/2

m
=

(0 + 6)/2

= 3
m

?
Then Check A[m] = KEY

Scanned with CamScanner


#include<conio.h>

#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();

printf("\n How Many elements in an array?");


scanf("%d",&n);
printf("\n Enter The Elements");
for(i=0;i<n;i++)

(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():
}

int BinSearch(int A[SIZE],int KEY,int low,int high)


{
int m;

}
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

BinSearch(A,KEY,m+1,high);//search the right sub list

How Many elements in an array? 5

Enter The Elements 10 20 30 40 50


Output

Enter the element which is to be searched 20

The element is at A[1] location

comor case/ Ang case It donor

а
р
т
и
р

we all ITP.
It divide prob m
Glend calfocus
Curso Do:
Best case
search ele is mid ele.

p
ar
a
O(1).

Scanned with CamScanner

You might also like