Data Structure (3330704) : Q.1 Write Difference Between Linear and Non-Linear Data
Data Structure (3330704) : Q.1 Write Difference Between Linear and Non-Linear Data
Data Structure (3330704) : Q.1 Write Difference Between Linear and Non-Linear Data
Ans.
Sr.
Linear Data Structure Non-Linear data Structure
No.
1 Every item is related to its previous and next Every item is attached with many other
item. items.
2 Data is arranged in linear sequence. Data is not arranged in sequence.
3 Data items can be traversed in a single run. Data cannot be traversed in a single run.
4 Examples: Array, Stack, Queue, Linked List. Examples: Tree, Graph.
5 Implementation is Easy. Implementation is Difficult.
Ans.
An algorithm is a step by step method of solving a problem. It is commonly used for data
processing, calculation and other related computer and mathematical operations.
1
Key features of an algorithm:- :15
1. Easy to grasp - Algorithms are there to help humans to understand the solution.
2. Correctness - This is a must have feature for all algorithms.
3. Precision - the steps are precisely stated (defined).
4. Finiteness - the algorithm stops after a finite number of steps.
5. Generality - the algorithm applies to all possible distribution of inputs as stated.
From data structure point of view, following are some important categories of
algorithms: −
Example:
Sum of Two Numbers:
step 1 − START ADD
step 2 − get values of a & b
step 3 − c ← a + b
step 4 − display c
step 5 − STOP
(3)It uses pointer variable which occupies (3) It does not use pointer variable so it
an extra memory space. does not occupies extra memory space.
2
(4) Insertion and deletion operation are (4) Insertion and deletion operation are
very easy to perform. very difficult to perform.
Example:
A two dimensional array consist of two rows and three columns is stored
sequentially in row major order as:
For example the address of element A[1,2] in a[2][3] is calculated as: A [1, 2] = Base + ((i – L1) *
n + (j – L2))* s
Here, n=3, m=2, i=1, j=2, Base = 3000 =3000 + ((1 - 0) * 3 + (2 - 0))*2 =3000 + (3+2)*2 =3000 +
10 =3010
3
Q.5 Write a C program to reverse the given string (without using strrev()
function).
Ans.
#include <stdio.h>
#include <string.h>
int main()
{
char arr[100];
printf("Enter a string to reverse\n");
gets(arr);
strrev(arr);
printf("Reverse of the string is \n%s\n", arr);
return 0;
}
Q.6 Write a C program to find the length of the given string (without using
strlen() function).
Ans.
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string to calculate it's length\n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n", length);
return 0;
}
4
Q.7 Write Algorithm to for compare two strings.
Ans.
Length = 0
Step 1:
EQUAL = 0
Length1 = strlen (s1)
Step 2:
Length2 = strlen (s2)
If (Length1 ≠ Length2) then
Step 3:
Write “Strings are not equal”
Step 4: Repeat step 5 while s1 [Length] ≠ NULL
If s1 [Length] ≠ s2 [Length] then
EQUAL = 1
Step 5: Length = Length +1
Else
Length = Length + 1
If EQUAL = 0 then
Write “Strings are equal”
Step 6:
Else
Write “Strings are not equal”
Q. 8 Define Stack. List out Operation of Stack & also explain Push operation in detail.
A stack is a linear OR non-primitive list in which insertion and deletion operations are
performed at only one end of the list.
A stack is Last in First out (LIFO) Structure.
A stack is a linear data structure in which elements are added and remove from one end,
called top of stack.For example of stack is Railway Shunting System.
Operations on Stack
1.PUSH Operation
2. POP Operation
Push Operation
In push operation, we can add elements to the top of the stack, so before push operation,
user must check the stack, it should not be a full.
5
If stack is already full and when we try to add the elements then error occurs. It is called
“Stack Over Flow” error.
Algorithm: PUSH(S, TOP, VAL)
This algorithm insert an element X to the top of the stack.
The Stack is represented by vector S which contains N elements.
TOP is a pointer which points to the top element of the Stack.
Step-1: [Check for stack overflow]
If (TOP >= N) then
Write (‘Stack Overflow’)
Return
Step-2: [Increment TOP]
TOP = TOP + 1
Step-3: [Insert Element]
S [TOP] = VAL
Step-4: [Finished]
Return
6
Write (‘STACK UNDERFLOW’)
Exit
Step-2:[Decrement Pointer]
TOP = TOP - 1
STEP-3:[Return former top element of the stack]
Return(S [TOP + 1])
1. (A-B)+C*A+B
Step-1: (-AB)+C*A+B
Step-2:(-AB)+*CA+B
Step-3:+(-AB)*CA+B
Step-4:++-AB*CAB
2. (B*C/D)/(D/C+E)
Step-1:(*BC/D)/(D/C+E)
Step-2:(/*BCD)/(D/C+E)
Step-3:(/*BCD)/(/DC+E)
Step-4:(/*BCD)/(+/DCE)
Step-5://*BCD+/DCE
7
Insert Operation of Queue: QINSERT (Q, Front, Rear, N, Val)
This function insert an element into the queue
The Queue is represented by vector Q which contains N elements.
Front is a pointer which points to the front end
Rear is a pointer which points to the rear end
Y is the element to be inserted.
ALGORITHM:QINSERT
STEP-1: [Check Overflow error?]
If Rear≥N then
Write (‘Queue Overflow’)
Exit
Step-2: [Increment rear pointer]
RearRear+1
Step-3: [Insert element]
Q[Rear] Val
Step-4:[Is front pointer properly set?]
If Front=0 then
Front1
Return
Step-5: [Finished]
Exit
(2) In LIFO the element which is inserted last (2) In FIFO the element which is inserted first is
is first to delete. first to delete.
(3) LIFO require only one pointer called TOP (3) FIFO requires two pointers called front and
rear.
8
(4) Example: piles of trays in cafeteria (4) Example: students at registration counter
(5) In LIFO there is no wastage of memory (5) In FIFO even if we have free memory space
space. sometimes we cannot use that space to store
elements.
Q. 14 Define Linked List. List out type of Linked List.& Explain Singly Linked List.
Linked list is a collection of node. Each node in the list consist of two parts::
1.. Information(INFO)
2.. Address or printer to next node (LINK).
Type of Linked List
1. Singly Linked List
2. Circular Linked List
3. Doubly Linked List
Singly Linked List
Singly Linked List is a collection of variable number of nodes in which each node
consists of two parts. First part contains a value of the node and second part contains an
address of the next node
Consider Following example in which Singly Linked List consist of three nodes.
Each node having INFO part and LINK part.
INFO part contains value of the node.
LINK part contains address of the next node in the linked list.
9
Q. 15 Write a short note on Circular Linked List.
A list in which last node contains a link or pointer to the first node in the list is known
as circular linked list..
Representation of circular linked list is shown bellow:
In a circular linked list there are two methods to know if a node is the first node or not.
Either a external pointer, list, points the first node or
A header node is placed as the first node of the circular list.
The header node can be separated from the others by either heaving a sentinel value as
the info part or having a dedicated flag variable to specify if the node is a header node
or not.
10
Else
NEW_NODE=AVAIL
AVAIL = AVAIL->LINK
Step 2: If FIRST = NULL then
NEW_NODE -> INFO = X
NEW_NODE -> LINK = NULL
FIRST = NEW_NODE
Else
NEW_NODE -> INFO = X
NEW_NODE -> LINK = FIRST
FIRST = NEW_NODE
Step 3: Exit
NEW_NODE -> INFO = X
NEW_NODE -> LINK = FIRST
FIRST = NEW_NODE
Step 4: Exit
11
SAVE = FIRST
Repeat while SAVE->LINK ≠ NULL
SAVE = SAVE->LINK
SAVE->LINK = NEW_NODE
Step 3: Exit
It starts from first element and searches the entire array until it find smallest element.
Then smallest value interchanges with the first element.
12
Now select second element and searches for the second smallest element from the array,
if found then interchange with second element.
So in this method, after pass 1 smallest value arranged at first position then after pass 2
second minimum will arrange at second position and so on.
This process continues until all the elements in the array are arranged in ascending
order.
Algorithm
Selection_Sort(List,N)
Where, List--> Array of N Elements
N-> Size of array(Total No of Elements)
Step:1 [Initialization]
i0
Step:2 while(i <N-1) repeat thru Step 7
Step:3 ji+1
Step:4 while (j<N) repeat thru Step 6
Step:5 if (List[i] > List[j])
(i) tempList[i]
(ii) List[i]List[j]
(iii) List[j]temp
Step:6 jj+1
Step:7ii+1
Step :8 [Finished]
Exit.
13
N-> Total no of elements
Step:1 Read N
Step:2 Repeat thru step 2 for
i=0,1,2,…N-1
Read (a[i])
Step:3 Repeat thru Step 6 for
i=0,1,2,…N-1
Step:4 Index a[i]
ji
Step :5 Repeat while (j>0 and a[j-1]>index)
a[j]a[j-1)
j j-1
Step :6 a[j] Index
Step :7 [Finished]
Exit.
61
81 94 87
70 11 42 23 74 65 36 57 99
Pocket: 0 1 2 3 4 5 6 7 8 9
14
Now arrange the number according to their pocket. The numbers after first pass are as
follows:
70 11 81 61 42 23 74 94 65 36 57 87 99
During second pass we separate the next higher digit of the number and place the
number in to appropriate pocket according to its digit.
After second pass the numbers in each pockets are as follow:
65 74 87 99
11 23 36 42 57 61 70 81 94
Pocket: 0 1 2 3 4 5 6 7 8 9
Now arrange the number according to their pocket. The numbers after second pass are
as follows:
11 23 36 42 57 61 65 70 74 81 87 94 99
The same process is repeated until all the elements are sorted.
Q-22 Define Hashing. Also explain different Hash Table Methods to build various
Hash Functions.
This technique uses the hashing function say H which maps the Key to the particular
address of the record.
Hashing function provides key to address transformation.
A hashing function H maps the Key space K into an address space.
Some of the most widely used hashing functions are :
1) Division Method
2) The Mid square Method
3) The Folding Method
4) Multiplicative Hashing
1) Division Method
Hashing function defined as: H(x) = x mod m + 1
15
For example: H (35) = 35 mod 11 + 1 = 2 + 1 = 3
The Division method generates a key value which belongs to the set {1, 2… m}
2) The Mid square Method
A key is multiplied by itself and the address is obtained by selecting an appropriate
number of digits from the middle of the square
For example: consider a six digit key 123456.
Squaring this key result in the value 5241383936. if a three digit address is required
then position 5 to 7 could be chosen which gives 138.
3) The Folding Method
A key is partitioned into parts. The length of each part is similar to the length of
the address required. These parts are added together and final carry is ignored to
produce the address.
For example if a key 35678943 is transformed into 2 digit address then we have:
35 + 67 + 89 + 43 = 234 = 34.
This method is known as fold shifting method
4) Multiplicative Hashing
H(x)= [m (cx mod 1 + 1)] + 1
Where, x=Key
c= Constant(0<c<1)
m= Hash Table Size
16
1) Linear Probing
If a record with key x is mapped to address location d and that location is
already occupied by another key then other locations in the table are examined
until a free memory location is found.
For Example,
Suppose we have to insert the following key values with hashing function H(k) =
k % 100
50904, 78907, 68403, 86704, 72308
Index Key
00 Empty
01 Empty
02 Empty
03 68403
04 50904
05 86704
06 Empty
07 78907
08 72308
2) Rehashing
If a hash function results in a collision then we use the secondary hash function
to calculate the address.
In above example the hash function H (k) = k % 100 produces collision for the
key 86704 so we use secondary hash function as: H (k) = (k + constant) % 100.
3) Quadratic Probing
If there is a collision at hash address h, this method probes the table at locations
h+1, h+4, h+9…., that is h + i2 (mod hash size).
That is the increment function is i2.
This method substantially reduces clustering.
4) Random Probing
To use a pseudorandom number generator to obtain the increment.
The generator should be generates the same sequence provided it starts with
the same need.
17
Q-24 Define the following terms.
Tree: A tree is defined as a finite set of one or more nodes such that
There is a special node called the root node R.
The remaining nodes are divided into n ≥ 0 disjoint sets T1, T2,.,.,. TN, where each of
these sets are tree. T1, T2…., T n is called the sub tree of the root
Complete Binary Tree: If the out degree of every node is exactly equal to 2 or 0 and the
number of nodes at level i is 2i-1 then the tree is called full or complete binary tree.
Degree / Total Degree: In a tree , the sum of edges comes into particular node and edges
comes out from particular node is called degree of that particular node.
Indegree: In a tree number of edges comes in to particular node is called Indegree of
that particular node.
OutDegree : In a tree number of edges comes out from particular node is called Out
degree of that particular node.
Leaf Node: The node which does not have any child node is called the leaf node.
18
Root Node: The node at the top of the tree is called root. There is only one root per tree
and one path from the root node to any node.
A tree is called binary search tree if each and every node can have 0,1 or 2
branches.And it should contain following characteristics.
All the nodes to the left of the root node have value less than the value of the root node.
All the nodes to the right of the root node have value greather than the value of the root
node.
45 68 35 42 15 64 78
19
Step 2: Now we have to insert 68. First we compare 68 with the root node which is 45.
Since the value of 68 is greater then 45 so it is inserted to the right of the root node.
Step 3: Now we have to insert 35. First we compare 35 with the root node which is 45.
Since the value of 35 is less then 45 so it is inserted to the left of the root node.
20
Step 6: Now we have to insert 64.
21