Winter 2021 Solution - Ds
Winter 2021 Solution - Ds
Time Complexity
Time Complexity is the amount of time taken by the program for
execution As it is difficult to measure the time complexity in terms of
clockunits , we can measure the time complexity using the frequency
count.
Example
1. void display( )
{
z
int a, b, c;
a = 10;
aa
b = 20;
Aw
c = a + b;
print(“%d”, c);
}
ut
Solution
gr
a = 10 1
b = 20 1
c=a+b 1
printf(“%d”, c) 1
Total 4
ANS
Malloc
The malloc” or “ memory allocation”. Method in c is used to
dynamically allocate a single large block of Memory with specified size.
It returns a pointer of type void which can be cast into a pointer of
anyForm
It doesn’t initialize memory at execution time So that it has initialized
z
aa
each block with the default garbage value initially
Syntax :- ptr = ( cast_type *) malloc ( byte_size )
Aw
Free
‘free’ methed in c. is used to dynamically allocate the memory. The
ut
freeing it
Syntax : free(ptr)
ANS
1. Priority Queue
A priority queue is type of queue that arranged elements based
ontheir priority values elements with higher priority values are
typically retrieved before elements with Lower priority value.
2. Primitive Data Structure
Primitive data structures is a Fundamental type of data
structuresthat store the data of only one type .
Examples : int , float , char
3. Non – Primitive Data Structure
The Non-Primitive Data Structure is a type of data structure
z
which is a user-defined that store the data of different types ina
single entity. aa
Examples : structure, union and enumerated
Aw
4. Linear Data Structures
Linear data Structure are the data Structure in which data is
arranged in a list or in a straight sequence.
ut
ANS
Algorithm
1. Scan all the symbols one by one from left to right in the given infix
Expression
2. If the reading symbol is operand , then immediate append it to the
postfixExpression.
3. If the reading symbol is left parenthesis ‘(’ , then push it onto the
stack.
4. If the reading symbol is right parenthesis ‘)’ , then pop all the
contents of thestack until the respective left parenthesis is popped
and append each popped symbol to postfix expression.
z
aa
5. If the reading symbol is operator ( + , - , * , / ) then push it onto the
stack. However, first pop the operators which are already on the
Aw
stack that have higheror equal precedence than the current
operator and append then to the postfix if open parenthesis is there
on top of the stack then, push the operator into the stack
ut
6. If input is over, pop all remaining symbol from stack & append then
gr
to postfix
Ja
ANS
Algorithm
1. Create a stack of store operands
2. Scan the given expression from left to right
3. a) If the scanned character is on operand push it into the stack
b) If scanned character is an operator, pop 2 operands from
stack & perform operator and push result back to the stack
4. Repeat step 3 till the characters are scan
5. When the expression is ended, the number the stock is the
final result
z
ANS
C program to reverse string using stack aa
Program
Aw
#include <stdio.h>
#include <string.h>
#define size 20
ut
Int top = -1 ;
gr
z
Puts(str); aa
}
Output :
Aw
OR
gr
Q2(C) Write algorithm (i) insert & (ii) delete elements in circular queue
Ja
(7M)
Insertion
Step 1 :
If (REAR + 1) % MAX = FRONT
Write “OVERFLOW”
go to step 4
[ END OF IF ]
Step 2 :-
IF FRONT = -1 and REAR = -1
set FRONT = REAR = 0
else if REAR = MAX – 1 and FRONT! = 0
set REAR = 0
Else
Set REAR = ( REAR + 1 ) % MAX
Deletion
Step 1 :-
If FRONT = -1
Write “ UNDERFLOW ”
Go to step 4
[ END OF IF ]
Step 2 :- SET VAL = Queue [ FRONT ]
Step 3 :-
IF FRONT = REAR
Set Front = Rear = -1
else if front = max – 1
Set Front = 0
z
else
set front = front + 1
[ END OF IF ]
aa
Step 4 :- Exit
Aw
ANS
Ja
Program
int insertatpos (struct node *head)
{
int i, choice, i=0;
Printf (“Do you want to enter any other element Loor/”);
Scanf (“%d”, &i);
if ( i = = 1 )
{
Printf ( “ Enter position:”);
Scanf(“%d”, &i);
}
else
{
z
ptr→ next = newnode; aa
}
Aw
Q3(B) Write user defined C function to delete node from end in circular
linked list (4M)
ut
ANS
gr
Ja
Program
Void last_delete ( struct node*head)
{
Struct node *ptr, *preptr;
if ( head = = NULL )
{
Printf (“\n underflow\n”);
}
else if( headnext →= = head)
{
head = NULL;
free(head);
Printf(“\n node deleted\n”);
}
Else
Q3(C) Write user defined C function to delete node from end in circular
linked list (7M)
z
ANS
aa
Aw
Program
#include<stdio,h>
#include<stdlib,h>
ut
struct Node
gr
{
int data;
Ja
void main( )
{
int choice ;
while(choice 1 =4 )
{
Printf(“\n1. Insert \n2 delete \n3
display)Printf(“\n Enter the choice”);
Scanf(“%d”, &choice);
Switch (choice)
z
int item;
aa
ptr = (struct node * ) malloc(size of (struct node));
if(ptr = = NULL)
Aw
{
printf(“\n overflow\n”);
return;
}
ut
else
gr
{
printf(“\n enter value?\n”);
Ja
scanf(“%d”, &item);
ptr → data = item;
if(front = = NULL)
{
front = ptr;
rear = ptr;
front → next = NULL;
rear → next = NULL;
}
else
{
rear → next = ptr
rear = ptr;
rear → next = NULL;
}
}
z
Struct node * ptr;
ptr = front;
if ( front = = null)
aa
Aw
{ printf(“\n Empty Queue\n”);
}
Else
{
ut
{
Ja
ANS
PROGRAM :
Void lastinsert ( struct node * ptr, struct node * temp,int item)
{
ptr = ( struct node * ) malloc ( size of ( structuralnode ));
if ( ptr = = NULL)
{
Printf(“\n overflow\n”);
}
else
z
{
Ptrdata = item;
if ( head = = NULL )
aa
{
Aw
head = ptr;
ptrnext = head;
}
ut
else
{ temp = head;
gr
Tempnext = ptr;
Ptrnext = head;
}
}
}
OR
Q3(B) Write user defined C function to delete node from specific
location in doubly linked list
ANS
Program
void delfrom pos ( )
{
int pos, i = 1;
OR
Q3(C) Write C program to implement stack using linked list (7M)
ANS
z
Program
#include <stdio.h>
aa
Aw
#include <stdlib.h>
Void push ( );
Void pop( );
Void display( );
ut
Struct node {
gr
int val;
struct node * next;
Ja
};
Struct node * head;
Void main ( )
{
int choice = 0; While
(choice ! = 4)
{
Printf(“\n! push\n2. Pop\n3. Show.”);
Printf(“\n Enter your choice \n”);
Scanf(“%d”, &choice)
Switch ( choice )
{
Case1:
push() :
break( )
Case 2 :
pop( ) :
z
{
printf(“Enter value”);
scanf(“%d, &val);
aa
Aw
if (head = = NULL)
{
ptr val = val;
ptr next = NULL;
ut
head = ptr;
}
gr
else
{
Ja
ptrval = val;
ptrnext = head;
head = ptr;
}
printf(“Item pushed”);
}
}
}
void pop( )
{
int item;
struct node * ptr;
if ( head = = NULL)
{
printf(“Underflow”)
}
else
item = headval;
ptr = head
head = headnext;
Free( ptr );
Printf(“item popped”);
}
}
void display ( )
{
int i;
struct node * ptr;
ptr = head;
if ( ptr = = NULL )
{
Printf(“stack is empty\n”);
}
z
else
{ aa
printf(“printing stack elements\n”)
Aw
while ( ptr ! = NULL )
{
printf (“ %d\n” , ptrval);
ptr = ptrnext ;
ut
}
}
gr
}
Ja
ANS
ANS
ut
AVL Tree
AVL Tree is invented by GM Adelson Velsk and EM Landis in 1962.
gr
Q4(C) Explain concept of B tree with suitable example & list of its
applications (7M)
z
ANS
aa
Aw
B Tree
B-Tree is a specialized m-way tree in at can be widely used in disk
access.
ut
It is not necessary that, all the nodes contain the same number of
childrenbut, each node m have m/2 number of nodes
z
aa
OR : Q4(A) Construct binary search from following numbers :
Aw
38, 13, 51, 10, 12, 40, 84, 25, 89, 37, 66, 95 (3M)
ANS
ut
gr
Ja
ANS
BFS
BFS stands for Breadth first search it is also known as level order
traversal
The queue data structure is used for the breadth first search traversal
There are many ways to traverse a the graph but among them BFS is the
most commonly used apparands .It is a recursive algorithm to search all
thevertices of a tree or graph data structure
BFS put every vertex of the graph into two categories visited and non-
visited
z
OR : Q4(C) Explain B + tree with example (7M)
aa
Aw
ANS
B+ tree
ut
leaf nodes, where as B + tree records can only be stored on the leaf
nodeswhile internal nodes can only store the keys value
The leaf node of a B + tree are linked together in the form of singly
linkedlist to make the search queries more efficient
B + tree are used to store the large amount data which cannot be stored
inthe main memory. Due to fact that, size of main memory is always
limited. The internal node of the B + tree are in the main memory where
as, leaf node are in the secondary memory
The internal nodes of the B+ tree are often index nodes . A B+ nodes of
order three is shown in following figure
ANS
z
Prim’s Algorithm
aa
Prim’s algorithm is a greedy algorithm that is used to find the
minimum spanning tree from a graph prim’s algorithm finds the
Aw
subset of edges that includes every vertex of the graph such that
sum of weights of edges can be minimized .
Prim’s algorithm starts with the single node and explores all the
ut
adjacent nodes with all the connecting edges at every step . The
gr
edges with the minimal weights causing no cycles in the graph got
selected .
Ja
Applications
Prim’s algorithm can be used in network designing
It can be used to make network cycle
It can also be used to lay down electrical wiring cables
ANS
Program
# include <stdio.h>
Void selection (int arr[],int n)
{
int i ,j,small;
For (i=0,i<n-1,i++)
z
int i;
for ( i = 0; i<n; i++)
Printf(“%d”,a[i]);
aa
Aw
}
int main()
{
ut
printArr(a,n);
selection(a,n);
ANS
There are five types of hash functions that are used to place record in
hash table
1) Division Method
2) Midsquare Method
3) Multiplicative Hash Function
4) Digit Folding
5) Digit Analysis
Division Method
Hash function depends upon remainder of division
z
Typically divisor is table length aa
H(key) = key mod M
H(key) = record % table size
Aw
h(key) = 89 % 10 = 9
gr
h(key) = 37 % 10 = 7
Ja
z
If key = 170 and p = 50 then aa
H(key) = floor(50 *(107 * 0.61803398987)
H(key) = floor(3306.4818458045)
Aw
H(key) = 3306
ANS
Ja
Fields
A combination of one or more characters is called field. It is smallest
unit of data that can be accessed by user name of each field a record
is unique
Records
A collection of related fields treated at single as single unit is called
record
Database
A database is information that is set up for easy access, management
& updating
ANS
OR Q5(C) Apply quick sort for following data : 9,7, 5, 11, 12, 2, 14, 3, 10, 6
(7M)
z
ANS aa
Aw
ut
gr
Ja