Explain about Notations in Data Structures?


Evaluating expressions: let Q be an arithmetic expression involving both operator &operands.

Operator: operator is a symbol that perform a particular tasks.

High priority { 1 }, c, {-----

Next high priority *, /

Low priority + , -

Let  Q = (5*6)+(2*10)+30*5+2

            = 30+20+150+2

            =202


Expressions are three types. Expressions are also called notation.

1.    Infix notation

2.    Prefix notation

3.    Postfix notation

     1.    Infix notation: operators are placed in between operands.
Ex: a+b

     2.    Prefix notation : operators are placed before operands
Ex : +ab

     3.    Postfix notation : operators are placed after operands.
Ex : ab+

Computers does not understand infix and prefix notation  It only understands postfix notation.

So that computer translates infix and prefix notations into postfix notations then evaluate the post fix notations.

Evaluating Postfix Notation:

Algorithm:

Step – 1:let Q be an arithmetic expression writing in postfix notation.

Step – 2: Add right bracket at the end of the Q .scan Q from left to right and repeat step  3 & step 4 until the end of Q.

Step – 3: If an operand is encountered push it on to stack.

Step – 4: If an operator is encountered remove the top two elements and perform res= a+b . and place the result in top.

b= st[top--]
a=st[top--]
res =a + b.
and place the result in top
st[++top]=res

notations

 
class Post
{
public static void main(String args[ ])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in());
int n;
System.out.println(“enter stack size”);
n= Integer.parseInt(br.readLine( ));
int st[ ]=new int(n);
int a, b, res, ch;
do
{
System.out.println(“1:operator 2:operand \n”);
ch= Integer.parseInt (br.readLine());
switch(ch)
{
b=st[top - -];
a=st[top - -];
case 1:
System.out.println(“enter operator”);
char x=br.readLine();
char op=x.charAt[0];
switch(op)
{
case ‘+’ :
                        res=a+b;
break;
case ‘-‘ :
res=a – b;
break
case ‘8’:
res = a * b;
break;
case ‘%’:
r=a/b;
break;
}
st[++top]=res;
break;
case 3:
System.out.println(“enter operand”);
st[++top]= Integer.parseInt (br.readLine());
break;
}
}while( ch < 3 ) ;
System.out.println(“res=”+res);
}
}


Related

Data Structures 8778601556838802974

Post a Comment

emo-but-icon

item