Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

BANGALORE INSTITUTE OF TECHNOLOGY

K.R.Road, V.V. Puram, Bangalore


DEPARTMENT OF M.C.A

Semester - 2ND Test : 1st


Subject : Data Structures and Algorithms Max Marks : 30
Time : 75 min
Answer ANY THREE Questions

Scheme of evaluation Marks CO PO


1.

A) Illustrate the different types of data structures with neat diagram and with the definition
5 1 1,2,3
of data structures.

Ans:
DEFINITION:
“The logical & Mathematical representation of data in computer memory is called as Data
Structure”.
OR
“A data structure is a method of storing data in a computer so that it can be used efficiently”.
The Data Structure can be divided into
Primitive
The Primitive Data Structures are data structures that can be manipulated directly by
machine instructions.
Ex: int, float, char, double.
Non-Primitive
Non-primitive data structures cannot be manipulated directly by machine instructions.
Ex: linear and Non linear
Non primitive :
Linear Data Structures :
A data structure is said to be linear if its elements form a sequence, or, in other
words, a linear list.
Example : Arrays, Stacks, Quees, lists
Nonlinear Data Structures :
A data structure is said to be nonlinear if its elements are stored randomly in
memory.
Example : Graphs, Trees, Files

B) Demonstrate the different operations of data structures


Ans: The most commonly used operations on data structures
5 1 1,2,3

Insertion / Creation
Deletion
Traversing
Searching
Sorting
Merging.
OR
2.
A) Define Stack? Implement the operations of Stack Using C? 6 1 1,2,3
Ans:
Definition:
A stack is a special type of data structure, where elements are inserted from one end and
elements are deleted from the same end.
The position from where elements are inserted and from where elements are deleted is
called top of the stack.
Stack is also called as Last In First Out(LIFO) data structures.
Stack as an ADT:
void push() //function ---1
{
/* check for overflow of stack */
if (top == stack_size-1)
{
printf(“ stack overflow);
return;
}
top = top+1;
S[top] = item;
}
int pop() //function----2
{
int item_deleted;
if (top == -1)
{
printf(“ Stack Underflow “);
return 0;
}
item_deleted = S[top--];
return item_deleted;
}

Void display() //function----3


{
int i;
if ( top == -1 )
{ Printf(“ stack is empty”); }
for( i=0; i<=top; i++ )
{
printf(“%d”, S[i]);
}
}
#include <stdio.h>
#include < process.h>
#define STACK_SIZE 5;

int top;
int s[10];
int item;

Void main()
{
int item;
int item_deleted;
int choice;

top = -1;
for( ; ; )
{
printf(“1. push, 2. pop, 3. Display, 4. Exit”);
printf(“enter the choice”);
Scanf(“%d”,&choice);
Switch(choice)
{
Case 1:
printf(“ Enter the item to be inserted”);
Scanf( “%d”, &item);
push();
break;
Case 2:
item_deleted = pop();
if (item_deleted == 0)
printf(“stack is empty”);
else
printf(“Item deleted= %d”, item_deleted);
break;
Case 3:
display();
break;
default:
exit(0);
}
}
} 4 1 1,2,3
B) Define different types of arithmetic expression with examples?
Ans:
The sequence of operators and operands that reduces to a single value after evaluation is called an
expression.
There are three different types of expressions:
Prefix Expression:
Postfix expression:
Infix expression:
Infix expression:
In an expression, if an operator is in the b/w two operands, the expression is
called an infix expression. Ex: a+b, (a-b), (a+b*c-(d*f))
Postfix expression:
In an expression, if an operator follows the two operands ( operator comes after
the two operands ) , the expression is called “postfix expression”. It is also called as “Suffix
expression”.
Ex: ab+, ab-, (a+b*c) ab*c+
Prefix Expression:
In an expression, if an operator precede the two operands(i.e., operator comes
before the two operands), the expression is called “Prefix Expression”.
Ex: +ab, -ab…. 10 1 1,2,3

3. Design an algorithm to convert an Infix expression to postfix. Trace the algorithm for
the following infix expression,
((A*(B+C/D)*E) +F*G).
Ans:
void infixtopostfix(char *infix, char *postfix)
{
char symbol;
int i=0,k=0;
stack stk;
while((symbol=infix[i++])!='\0')
{
if(isalnum(symbol))
postfix[k++]=symbol;
else if(symbol=='(')
stk.push(symbol);
else if(symbol==')')
{
while(stk.stop()!='(')
postfix[k++]=stk.pop();
stk.pop();
}
else
{
while(priority(symbol)<=priority(stk.stop()))
postfix[k++]=stk.pop();
stk.push(symbol);
}
}
while(!stk.isEmpty())
postfix[k++]=stk.pop();
postfix[k]='\0';
}
Solved Manually.
5 1 1,2,3
4. A) Convert the following expression into POSTFIX and PREFIX.
((X-Y^(B/C*D) +E) +F*G/H*I).
5 1 1,2,3

B) Convert the following expression into Infix form.


A,B,C,*,-,D,E,F,/,+,*,G,+,H,+.

4 1 1,2,3
5.
A) Briefly explain the Direct and Indirect applications of Stack?
Ans:
Direct applications:
Conversion of expressions
Evaluation of expressions
Recursion
Indirect applications:
Undo in Microsoft applications.
Arrangements of plates. 6 1 1,2,3
Back in IE.

B) Evaluate the postfix expression by showing the contents of Stack. 2,1,2,^,^,4,-,3,+,-4,5,4,/,+


10 1 1,2,3

6. Design an algorithm to evaluate a postfix expression. Trace the algorithm for the
following postfix expression showing the contents of Stack.
3,2,1,^,*,3,10,2,/,-,*,2,/,3,+.
Ans:
while(p[i])
{
sym=p[i];
if(post.isoperand(sym))
{
cout<<"Enter val of postfix"<<sym<<":";
cin>>x;
post.push(x);
}
if(post.isoperator(sym))
{
b=post.pop();
a=post.pop();
switch(sym)
{
case '+' : c=a+b;
break;

case '-' : c=a-b;


break;

case '*' : c=a*b;


break;

case '/' : c=a/b;


break;

case '$' : c=pow(a,b);


break;
}
post.push(c);
}
i++;
}
val=post.pop();
cout<<"value of prefix:"<<val;
getch();
}

Course-Coordinator Module-Coordinator

IQAC Program-Coordinator

You might also like