Dsa Paper
Dsa Paper
Dsa Paper
Module 1
Q 1 a. What is data structure? What are various types of data structure? Explain. (5 marks)
Ans:
A structure represents name, type, size and format. A data structure is a represents to organize and
store data in terms of name, type, size and & format in computers memory. Data may contain a
single element or sometimes it may be a set of elements. Whether it is a single element
or multiple elements but it must be organized in a particular way in the computer memory system.
General data structure types include the array, lists, vectors, the file, the record, the table, the tree,
graphand so on. Any data structure is designed to organize data to suit a specific purpose so that it
can be accessed and operated with appropriate ways.
Q1b. What is a structure? How it is different from array? Explain different types of structure
declarations with examples & give differences between Union & Structure. (10 Marks)
Ans:
Structure is a data structure whose individual elements can differ in type. It may contain
integer elements, character elements, pointers, arrays and even other structures can
also be included as elements within a structure.
Different from Array: Array is collection of similar data type but structure is composed
of different data types. It represents a record with different fields. Structure of a student
may have roll number of integer, name as string and marks as float.
Or
Q2a. Explain dynamic memory allocation functions in detail. (6 Marks)
Ans:
Dynamic Memory allocation and de-allocation:
Memory can be allocated/ de-allocated at run-time as and when required. C has four in-built
functions for the same with header file stdlib.h
malloc(), calloc() and realloc() to allocate and free() to de-allocate
malloc()
The name malloc stands for "memory allocation". The function malloc() reserves a block of
memory of specified size in bytes and return a pointer of type void which can be casted into
pointer of any form.
Syntax of malloc()
ptr=(cast-type*)malloc(byte-size);
example:
ptr=(int*)malloc(100*sizeof(int));
calloc()
The name calloc stands for "contiguous allocation". The only difference between malloc()
and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates
multiple blocks of memory each of same size and sets all bytes to zero.
Syntax for calloc():
ptr=(cast-type*)calloc(n,element-size);
example:
ptr=(int*)calloc(25,sizeof(int));
realloc()
If the previously allocated memory is insufficient or more than sufficient. Then, you can
change memory size previously allocated using realloc().
Syntax/example:
ptr=realloc(ptr, newsize);
free()
Dynamically allocated memory with either calloc() or malloc() does not get return on its own.
The programmer must use free() explicitly to release space.
Syntax/example:
free(ptr);
while(str2[len2]!=NULL) len2++; /*length of string2 */
int main()
{
char str1[50],str2[50];
printf("Enter string 1:");
gets(str1);
printf("Enter string 2:");
gets(str2);
concat(str1,str2);
printf("concatenated string is %s\n",str1);
return (0);
}
Module 2
Q3a. Define stack. Give the implementation of push, pop and display functions in detail.(7marks)
Ans:
A stack is a LIFO(Last in first out) data structure having one end open only to insert or delete items.
The item inserted at last will be deleted at first.
A common model of a stack is a plate or coin stacker. Plates are "pushed" onto to the top and
"popped" off the top.
A stack is generally implemented with only two basic principle operations
Push : adds an item to a stack on “top”.
Pop: extracts the most recently pushed item from the stack from “top”.
Procedure/Functions:
/*MAX is the size of stack & item is to be pushed incrementing
the top variable */
Q3b. Write the postfix from the following expression using stack:
(i) A $ B * C – D + E|F|(G+H) (ii) A – B | (C * D $ E) (6marks)
Ans:
These expression having $ symbol that can be treated as exponentiation having greater
precedence.
Single vertical bar is understood as Bitwise OR .
Converting to postfix using stack will require one operator stack to keep operator and one postfix
string to add operands & operator (when another operator comes)
(i) A $ B * C – D + E|F|(G+H)
Ans: AB$C*D-EF|GH+|+
(ii) A – B | (C * D $ E)
Operator Stack & postfix string is created. Converting to postfix using stack will require one
operator stack to keep operator and one postfix string to add operands & operator (when another
operator comes)
*
( $
- | |
|
Postfix String
A B - C D * E $ |
Q3c. Write an algorithm to evaluate a postfix expression and apply the same for the given postfix
expression ABC-D*+E$F + (7 Marks)
Ans:
Algorithm to evaluate postfix expression:
i. Read postfix expression from left to right (Taking care of precedence & Parentheses)
a. If operand is encountered push it onto stack
b. If operator is encountered, pop two operands (a= top element; b= next to top
element)
c. Evaluate b operator a and push onto stack
ii. Set final result to pop
Evaluating postfix expression ABC-D*+E$F+
Or
Q4a. Define recursion. Write a recursive function for the following:
(i) Factorial of a number (ii) Tower of Hanoi (5marks)
Ans:
/i. *Factorial of a number */
long int fact(int n)
{
if(n == 0 || n == 1)
return (1);
else
return (n*fact(n-1));
}
Ans:
Queue is FIFO (first in first out data structure). Items are processed and deleted according to
their sequence of arrival in the queue. “Items are inserted from rear and deleted from front
end.
Ans:
A double-ended queue, or deque, supports insertion and deletion from the front and
rear
An input-restricted deque is one where deletion can be made from both ends, but
insertion can be made at one end only.
An output-restricted deque is one where insertion can be made at both ends, but
deletion can be made from one end only
Priority Queue:
A Priority Queue is a queue but different from a normal queue, because instead of being a “first-in-
first-out”, values come out in order by priority. It is an abstract data type that captures the idea of a
container whose elements have “priorities” attached to them.
Jobs are rescheduled in the queue based on their priority number. In general a job least priority
number will have highest priority. A job with higher priority is processed/de-queued before a job
with lower priority.
Jobs/Items with the similar priority will be scheduled on FCFS (First Come First Serve) basis.
Priority Queue
Abstract of Priority queue:
struct pq
{
char jname[20];
int priority
}jobs[5];
jobs[0].jname=”help”;
jobs[0].priority=3;
jobs[1].jname=”study”;
jobs[1].priority=3;
jobs[2].jname=”eat”;
jobs[2].priority=2;
jobs[3].jname=”sleep”;
jobs[3].priority=1;
jobs[4].jname=”play”;
jobs[4].priority=2;
Ans:
A linked list is a dynamic data structure where a node keeps item and address of next node.
Sometimes it is referred as self-referential structure. It can be considered as a list whose
order is given by links from one item to the next. Each node has at least two members, one
of which points to the next item or node in the list! These are defined as Single Linked Lists
because they only point to the next item, and not the previous.
Linked list
Types of Linked List:
i. Singly linked list
ii. Doubly Linked list
iii. Circular linked list (singly and doubly)
Doubly Linked List: a doubly-linked list is a linked list (linked data strcture) that consists of a set of
set of data, each having two special links that contain address of the previous and to the next node
in the sequence.
A doubly-linked list whose nodes contain three fields: an integer value, the link to the next node, and
the link to the previous node.
The two links allow walking along the list in either direction with equal ease. Compared to a singly-
linked list, modifying a doubly-linked list usually requires changing more pointers, but is sometimes
simpler because there is no need to keep track of the address of the previous node
Circular Linked List: In Circular Linked Lists last node points to first. If it is Doubly circular linked list
then prepvious pointer of the first node will point to the last node and next pointer of the last node
will point to first node.
Q5b. Write a C function to insert a node at front and delete a node from rear end in a circular
linked list. (8mars)
Ans:
/* C Function to insert a node at front in a circular linked list: */
struct Node *addfront(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
/* Creating a node dynamically. */
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
/* Now Adjusting the links */
temp -> next = last -> next;
last -> next = temp;
return last;
}
/* C Function to delete a node from rear in a circular linked list: */
struct Node *addEnd(struct Node *last, int data)
{
if (last == NULL)
return addToEmpty(last, data);
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp -> data = data;
temp -> next = last -> next;
last -> next = temp;
last = temp;
return last;
}
Q5c. Write C function for the concatenation of two doubly linked lists. (5marks)
Ans:
/* function to concatenate two doubly linked lists even and odd
void concatenate()
{
struct node *link;
list = even;
link = list;
while(link->next!= NULL) {
link = link->next; /* traversing to last of even DLL */
}
link->next = odd;
odd->prev = link;
/* assign link_last to last node of new list */
while(link->next!= NULL) {
link = link->next;
}
list_last = link;
}
Or
Q6a. Describe the doubly linked list with advantages and disadvantages. Write a C function to
delete a node from a circular doubly linked list with the header node. (8marks)
Ans:
i. Advantages of DLL over SLL
A DLL can be traversed in both forward and backward direction.
ii. The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
iii. In DLL, to delete a node, pointer to the previous node is needed. To get this previous node,
sometimes the list is traversed. In DLL, we can get the previous node using previous
pointer.
ii. All operations require an extra pointer previous to be maintained. For example, in insertion,
we need to modify previous pointers together with next pointers.
Q6b. For the given sparse matrix, give the diagramatic linked representation. (4marks)
0 1 2
A= 3 0 0
0 0 0
Ans:
A sparse matrix has relative number of elements 0. Representing a sparse matrix using 2-D array
takes substantial amount of space with no use.
A sparse matrix can be represented by triplets with maximum column of 3 and each row will have
corresponding row number, column number of non-zero elements and non-zero element itself.
Those triples can also be represented by a singly linked list having members row, col, val (non-zero)
and address of next node:
Structure definition:
struct spmlist
{
int row,col,val;
struct spmlist *next;
};
NULL
0 1 2 3 0 0 0 0 0
Module 4
Q6c. Write a C function to add two polynomials represented as circular list with header node.
(8marks)
Ans:
NODE poly_add(NODE head1, NODE head2, NODE head3)
{
NODE a,b;
int coeff;
a = head1->link;
b = head2->link;
while(a != head1 && b != head2)
{
if(a->expon == b->expon)
{
coeff = a->coeff + b->coeff;
if(coeff != 0)
head3 = attach(coeff, a->expon, head3);
a = a->link;
b = b->link;
}
else if(a->expon > b->expon)
{
head3 = attach(a->coeff, a->expon, head3);
a = a->link;
}
else
{
head3 = attach(b->coeff, b->expon, head3);
b = b->link;
}
}
while(a != head1)
{
head3 = attach(a->coeff, a->expon, head3);
a = a->link;
}
while(b != head2)
{
head3 = attach(b->coeff, b->expon, head3);
b = b->link;
}
return head3;
}
Module 4
Q7a. What is a tree? With suitable example, define:
(i) Binary Tree (ii) Level of Binary tree (iii) Complete Binary Tree (iv) Degree of the tree
(9marks)
Ans:
A tree is a non-linear data structure that simulates a hierarchical tree structure stating with a root
node and connected with subtree or children nodes.
Contrary to a physical tree, the root is usually depicted at the top of the tree structure and the
leaves (leaf nodes) are depicted at the bottom.
Leaf/External nodes will not have branch/child. Non-leaf/Internal nodes will have branch/child.
Internal nodes are also known as parent node to their children node.
Children of same parent are known as siblings. In given diagram B,C & D are siblings. E and F are also
siblings.
i. Binary Tree:
A Binary tree is a tree where a node may have 0, 1 or 2 (maximum) children ( called left
and right child). All sub-trees of Binary tree are also Binary tree.
Binary Trees
ii. Level of a Binary Tree:
Level (of a node): The number of parent nodes corresponding to a given a node of the tree.
For example, the root, having no parent, is at level 0
Level of Binary Tree: Level of a binary tree start from root node with value 0 Everytime we
move from top side of the tree towards the bottom side we increase the level by one.
Note- “Top down manner “ means here traversal from the root node to one of its child node
repeatedly
iii. Complete Binary Tree:
It is also termed as almost complete binary tree where nodes are added level by level &
left to right. Only the last level few nodes may be added (if not full) from left to right.)
A complete binary tree is a binary tree in which every level, except possibly the last, is
completely filled.
iv. Degree of the tree: Basically The degree of the tree is the total number of it's children i-e the
total number nodes that originate from it. The leaf of the tree doesnot have any child so
its degree is zero. Finally Degree of a tree is the highest degree of a node among all the
nodes in the tree.
Q7b. Write the C routines to traverse the tree using (i) pre-order traversal (ii) post-order traversal
(6marks)
Ans:
/*Recursive function of Pre-order Traversal*/
void preorder(TREE *bintree)
{
if(tree) {
printf(" %s",bintree->str);
preorder(bintree->left);
preorder(bintree->right);
}
}
/* Recursive function of post-order traversal */
void postorder(TREE *bintree)
{
if(tree) {
postorder(bintree->left);
postorder(bintree->right);
printf(" %s",bintree->str);
}
}
Q7c. For the given data, draw a Binary search tree and show the array and linked representation
of the same: 100, 85, 45, 55, 110, 20, 70, 65 (5marks)
Ans:
Binary Search Tree for given data:
100
85 110
45
20 55
70
65
Array Representation:
Children of nth node will be at 2nth and (2n+1)th node
100 | 85 | 110 | 45 | -|-|-|20|55|- |- |- |-|- |- |- |- |- |70 |- |- |- | - |- | - | - | -| -| -| -| - | -|-|- |- |- |- |65|
1 2 3 4 5 6 7 8 9 101112131415161718 19 202122 232425 26 27282930 3132333435363738
Linked Representation: All nodes structure L (left) pointer, data & R (right pointer)
Or
Q8a. What is the advantages of the threaded Binary tee over binary tree? Explain the construction
of threaded binary tree for 10, 20, 30, 40 and 50 (7marks)
Ans:
A binary tree is made threaded by making all right child pointers that would normally be NULL point
to the inorder successor of the node (if it exists).
Advantages:
Threaded trees make it possible to perform inorder-traversal without the use of stack
or recursion.
The threads make it possible to back-up to higher levels.
Q8b. Define Expression tree for a given tree in Fig8(b) traverse the tree using in-order, pre-order &
post-order traversal (7 marks)
+
* +
B *
C E
Fig 8(b)
Ans:
Expression tree is a binary tree in which each internal node corresponds to operator and each leaf
node corresponds to operand.
Surprisingly in given tree operators are more than operands(B,C,D &E). I presume A is missing & A
should be leaf node. However traversals are given here as per given tree.
BC EDGHFI
B D
E F
C
G I
Q9a. Define Graph. For the given graph, show the adjacency matrix and adjacency list
representation of the graph.[Ref. Fig Q9A] (5marks)
A C E
D F
Ans:
Adjacency matrix of given directed (1 for path going to adjacent, 0 for no path going)
Adjacency List Presentation:
Q9b. What are the methods used for traversing a graph? Explain any one with example and write
C function for the same. (8marks)
Ans:
Graph traversal means visiting every vertex and edge exactly once in a well-defined order. The most
common way of tracking vertices is to mark them. In general there are 2 ways to traverse graphs.
i. Breadth First Search (BFS)
BFS is the most commonly used approach.
We should start traversing from a selected node (source or starting node) and traverse the graph
layer-wise thus exploring the neighbour nodes (nodes which are directly connected to source node).
We must then move towards the next-level neighbour nodes. For BFS Queue data structure is used.
As the name BFS suggests, we are required to traverse the graph breadth-wise as follows:
A. First move horizontally and visit all the nodes of the current layer
B. Move to the next layer
Q9c. Write a C function for insertion sort. Sort the following list using insertion sort. (7marks)
50, 30, 10, 70, 40, 20, 60
Ans:
/Function for insertion sort */
void inssort(int a[], int size)
{
int x,y,picked;
for(x=1;x<size;x++)/* loop starts from 2nd element */
{
picked = a[x];
y=x;
while(a[y-1] > picked && y > 0)
{
a[y]=a[y-1]; /* shifting */
y--;
}
a[y]=picked; /* inserting */
}
}
Sorting:
50, 30, 10, 70, 40, 20, 60
50
30 is “picked”. 50 is greater than 30 so, 50 is shifted. There is no more to compare so, 30 is inserted at
first
30 50 10 70 40 20 60
30 50
10 is picked, 50 is greater so shifted, 30 is greater so shited. There is no more to compare so, 10 is
inserted at first.
10 30 50 70 40 20 60
70 is picked, 50 is less than 70 so inserted at same position
10 30 50 70 40 20 60
50 70
40 is picked, 70 is greater so shifted, 50 is greater so shited, 30 is less so, 40 is inserted
10 30 40 50 70 20 60
30 40 50 70
20 is picked. 70. 50. 40 & 30 have been shifted. 10 is less than 20 so, 20 is inserted.
10 20 30 40 50 70 60
70
60 is picked. 70 is greater therefore shifted. 50 is less than 60 so, 60 is inserted.
10 20 30 40 50 60 70
Now the array is sorted.
Or
Q10a. What is collision? What are the methods to resolves collision? Explain linear probing with
an example. (7marks)
Ans:
Hashing is a technique which is designed to locate/calculate/map a key value to be
stored/accessed. Hashing technique use a special function called the Hash function which is
used to map a given value with a particular key for faster access of elements.
Popular hash function is key MOD size.
int hash(int key) { return key%size; }
Collision:
Hash function performs poor when multiple keys are mapped to the same location. This is collision.
For collision resolution may techniques are suggested. The techniques also suffer problems of
primary & secondary clustering.
It is suggested that size of the hash table should be a prime number to reduce collision &
clustering.
Classification of Hashing collision resolution techniques.
Linear Probing:
Linear Probing is basic & popular collision resolution technique in hashing when collision occurs.
In linear probing, we linearly probe for next slot when collision occurs for the calculated key by the
hash function.
If location loc is full, then we try loc+1. Location is coming to beginning if crossing size.
/*Hashing - Linear Probing */
#include <stdio.h>
int size;
long hash(int key) { return (key%size); }
void insert(int hashtable[])
{
int x,y,key; long loc,temp;
for( x=0;x<size;x++)
{
printf("\nEnter element %d : ",x+1);
scanf("%d",&key);
loc=hash(key);
while(hashtable[loc]!='\0')
{
loc++;
if(loc==size) loc=0;
}
hashtable[loc]=key;
}
}
Example:
Ans.
Hashing is an efficient technique to directly search the location of desired data on the disk without
using index structure. Data is stored at the data blocks whose address is generated by
using hash function. The memory location where these records are stored is called as data block
or data bucket.
Static hashing:
Single hash function h(k) on key k
Desirable properties of a hash function
o Uniform: Total domain of keys is distributed uniformly over the range
o Random: Hash values should be distributed uniformly irrespective of distribution of
keys O(1) search
Example of hash functions: h(k) = k MOD m (size of hash table)
Collision resolution
Chaining
o Load factor
o Primary pages and overflow pages (or buckets)
o Search time more for overflow buckets
Open addressing
o Linear probing
o Quadratic probing
o Double hashing
Again: Dynamic hashing Hash function modified dynamically as number of records grow Needs to
maintain determinism Extendible hashing and Linear hashing
Organize overflow buckets as binary trees
m binary trees for m primary pages
h0(k) produces index of primary page
Particular access structure for binary trees
Family of functions : g(k) = { h1(k),....hi(k),....}
Each hi(k) produces a bit
At level i, hi(k) is 0, take left branch, otherwise right branch Example: bit representation
Q10c. Briefly Explain basic operations that can be performed on a file. Explain Indexed sequential
file organization. (7marks)
Ans:
A computer file is collection of data (text, program, image, animation or video) stored on permanent
storage device. A computer file has a name. A file belongs to a type. In general type is recognized by
its extension name.
In terms of operating system files can be classified as ordinary file, directory file, special file and FIFO
file.
Basic operations can be performed on a file:
Read operation: To read the content stored in a file
Write Operation: To modify, overwrite the contents or to create a new file
Rename operation: To change the name of a file
Copy operation: To copy a file from one location to another
Move operation: To move a file to another folder/directory or drive
Delete Operation: To remove a file from storage
Run operation: To open a file/ To execute a program file
Link operation: To create links of files (symbolic link or hard link)