Data-Structure 6
Data-Structure 6
n and deletion operation are (4) Insertion and deletion operation are
Data Structure (3330704)
1. Easy to grasp - Algorithms are there to help humans to understand the solution. very easy to perform. very difficult to perform.
Q.1 Write difference between Linear and Non-Linear dataStructure. 2. Correctness - This is a must have feature for all algorithms.
3. Precision - the steps are precisely stated (defined). Q.4 Explain Row major order of an array with example.
Ans. 4. Finiteness - the algorithm stops after a finite number of steps. Ans.
5. Generality - the algorithm applies to all possible distribution of inputs as stated. Row-major Arrays: If two dimensional array elements store sequentially row by row then it is
called Row major array.
From data structure point of view, following are some important categories of
algorithms: − Example:
A two dimensional array consist of two rows and three columns is stored
1. Search − Algorithm to search an item in a data-structure.
sequentially in row major order as:
2. Sort − Algorithm to sort items in certain order
3. Insert − Algorithm to insert item in a data-structure
A[0][0] A[0][1] A[0][2]
4. Update − Algorithm to update an existing item in a data-structure
A[1][0] A[1][1] A[1][2]
5. Delete − Algorithm to delete an existing item from a data-structure
1 2 3
Q.5 Write a C program to reverse the given string (without using strrev() Q.7 Write Algorithm to for compare two strings. If stack is already full and when we try to add the elements then error occurs. It is called
function). Ans. “Stack Over Flow” error.
Ans. Algorithm: PUSH(S, TOP, VAL)
Length = 0 This algorithm insert an element X to the top of the stack.
Step 1:
#include <stdio.h> EQUAL = 0
The Stack is represented by vector S which contains N elements.
#include <string.h> Length1 = strlen (s1)
Step 2: TOP is a pointer which points to the top element of the Stack.
int main() Length2 = strlen (s2)
If (Length1 ≠ Length2) then Step-1: [Check for stack overflow]
{
Step 3:
char arr[100]; Write “Strings are not equal” If (TOP >= N) then
printf("Enter a string to reverse\n"); Step 4: Repeat step 5 while s1 [Length] ≠ NULL Write (‘Stack Overflow’)
gets(arr); If s1 [Length] ≠ s2 [Length] then Return
strrev(arr); EQUAL = 1 Step-2: [Increment TOP]
printf("Reverse of the string is \n%s\n", arr); Step 5: Length = Length +1
TOP = TOP + 1
return 0; Else
Step-3: [Insert Element]
} Length = Length + 1
If EQUAL = 0 then S [TOP] = VAL
Write “Strings are equal” Step-4: [Finished]
Step 6:
Q.6 Write a C program to find the length of the given string (without using Else Return
Write “Strings are not equal”
strlen() function).
Ans. Q. 9 Explain POP operation in detail.
Q. 8 Define Stack. List out Operation of Stack & also explain Push operation in detail.
In POP operation, we can delete or remove an elements from top of the stack, so before
#include <stdio.h> A stack is a linear OR non-primitive list in which insertion and deletion operations are pop operation, user must check the stack, stack should not be a empty.
#include <string.h> performed at only one end of the list.
int main() A stack is Last in First out (LIFO) Structure.
If the stack is empty, and we try to pop an element, then error occur. It is called “Stack
{ A stack is a linear data structure in which elements are added and remove from one end, under Flow” error.
char a[100];
called top of stack.For example of stack is Railway Shunting System.
int length;
Operations on Stack
printf("Enter a string to calculate it's length\n"); Algorithm: POP(S, TOP)
gets(a); 1.PUSH Operation This algorithm removes an element from the Top of the Stack.
length = strlen(a); 2. POP Operation The Stack is represented by vector S which contains N elements.
printf("Length of the string = %d\n", length); Push Operation TOP is a pointer which points to the top element of the Stack.
return 0; In push operation, we can add elements to the top of the stack, so before push operation,
Step-1: [Check for the Underflow on the Stack]
} user must check the stack, it should not be a full. If TOP = 0 then
4 5 6
Write (‘STACK UNDERFLOW’) Insert Operation of Queue: QINSERT (Q, Front, Rear, N, Val) (4) Example: piles of trays in cafeteria (4) Example: students at registration counter
Exit This function insert an element into the queue (5) In LIFO there is no wastage of memory (5) In FIFO even if we have free memory space
Step-2:[Decrement Pointer] The Queue is represented by vector Q which contains N elements. space. sometimes we cannot use that space to store
TOP = TOP - 1 Front is a pointer which points to the front end elements.
STEP-3:[Return former top element of the stack] Rear is a pointer which points to the rear end
Return(S [TOP + 1]) Y is the element to be inserted. Q. 13 Explain Dynamic Memory Allocation.
ALGORITHM:QINSERT In the Dynamic memory allocation , the memory is allocated to a variable or program at
Q. 10 Convert following infix expression into Prefix expression. STEP-1: [Check Overflow error?] the run time.
If Rear≥N then The only way to access this dynamically allocated memory is through pointer.
1. (A-B)+C*A+B
Write (‘Queue Overflow’) Types of dynamic memory allocation.
2. (B*C/D)/(D/C+E)
Exit 1. Malloc( )
7 8 9
Q. 16 Write an Algorithm for Singly linked list beginning. Else Q-19 Explain Selection Sort with algorithm.
10 11 12
Now select second element and searches for the second smallest element from the array, N-> Total no of elements
if found then interchange with second element. Step:1 Read N Now arrange the number according to their pocket. The numbers after first pass are as
So in this method, after pass 1 smallest value arranged at first position then after pass 2 Step:2 Repeat thru step 2 for follows:
second minimum will arrange at second position and so on.
i=0,1,2,…N-1 70 11 81 61 42 23 74 94 65 36 57 87 99
This process continues until all the elements in the array are arranged in ascending
Read (a[i])
order.
Algorithm Step:3 Repeat thru Step 6 for During second pass we separate the next higher digit of the number and place the
Selection_Sort(List,N) i=0,1,2,…N-1 number in to appropriate pocket according to its digit.
Where, List--> Array of N Elements Step:4 Index a[i] After second pass the numbers in each pockets are as follow:
N-> Size of array(Total No of Elements) ji 65 74 87 99
Step:1 [Initialization] Step :5 Repeat while (j>0 and a[j-1]>index) 11 23 36 42 57 61 70 81 94
i0 a[j]a[j-1) Pocket: 0 1 2 3 4 5 6 7 8 9
Step:2 while(i <N-1) repeat thru Step 7 j j-1
Step:3 ji+1 Step :6 a[j] Index Now arrange the number according to their pocket. The numbers after second pass are
Step:4 while (j<N) repeat thru Step 6 Step :7 [Finished] as follows:
Step:5 if (List[i] > List[j]) Exit. 11 23 36 42 57 61 65 70 74 81 87 94 99
(i) tempList[i] The same process is repeated until all the elements are sorted.
13 14 15
For example: H (35) = 35 mod 11 + 1 = 2 + 1 = 3 1) Linear Probing Q-24 Define the following terms.
The Division method generates a key value which belongs to the set {1, 2… m} If a record with key x is mapped to address location d and that location is Tree: A tree is defined as a finite set of one or more nodes such that
2) The Mid square Method already occupied by another key then other locations in the table are examined There is a special node called the root node R.
A key is multiplied by itself and the address is obtained by selecting an appropriate until a free memory location is found. The remaining nodes are divided into n ≥ 0 disjoint sets T1, T2,.,.,. TN, where each of
number of digits from the middle of the square For Example, these sets are tree. T1, T2…., T n is called the sub tree of the root
For example: consider a six digit key 123456. Suppose we have to insert the following key values with hashing function H(k) =
Squaring this key result in the value 5241383936. if a three digit address is required
k % 100
then position 5 to 7 could be chosen which gives 138.
50904, 78907, 68403, 86704, 72308
3) The Folding Method
Index Key
A key is partitioned into parts. The length of each part is similar to the length of
00 Empty
the address required. These parts are added together and final carry is ignored to
01 Empty
produce the address.
02 Empty
For example if a key 35678943 is transformed into 2 digit address then we have: 03 68403
35 + 67 + 89 + 43 = 234 = 34. A forest is a set of n ≥ 0 disjoint trees. Forest is a collection of disjoint trees.
04 50904
This method is known as fold shifting method 05 86704
4) Multiplicative Hashing 06 Empty
H(x)= [m (cx mod 1 + 1)] + 1 07 78907
Where, x=Key 08 72308
c= Constant(0<c<1) 2) Rehashing
m= Hash Table Size 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
Q-23 Define Collision. List and explain Collision Resolution Techniques.
key 86704 so we use secondary hash function as: H (k) = (k + constant) % 100.
When a hashing function maps several keys to same address space then it is known as 3) Quadratic Probing
Complete Binary Tree: If the out degree of every node is exactly equal to 2 or 0 and the
collision. If there is a collision at hash address h, this method probes the table at locations
number of nodes at level i is 2i-1 then the tree is called full or complete binary tree.
Example: we are storing the records in an array which ranges from 0 to 99.if a h+1, h+4, h+9…., that is h + i2 (mod hash size).
Degree / Total Degree: In a tree , the sum of edges comes into particular node and edges
hashing function is H(k) = k % 100 then this function will produces same address for That is the increment function is i2.
comes out from particular node is called degree of that particular node.
This method substantially reduces clustering.
the keys 15433 and 26733. in this case collision is encountered. Indegree: In a tree number of edges comes in to particular node is called Indegree of
4) Random Probing
Collision resolution techniques are : that particular node.
To use a pseudorandom number generator to obtain the increment.
1. Linear Probing OutDegree : In a tree number of edges comes out from particular node is called Out
The generator should be generates the same sequence provided it starts with
degree of that particular node.
2. Rehashing the same need.
Leaf Node: The node which does not have any child node is called the leaf node.
3. Quadratic Probing
4. Random Probing
16 17 18
Root Node: The node at the top of the tree is called root. There is only one root per tree Step 2: Now we have to insert 68. First we compare 68 with the root node which is 45.
and one path from the root node to any node.
Since the value of 68 is greater then 45 so it is inserted to the right of the root node.
Since the value of 35 is less then 45 so it is inserted to the left of the root 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 Step 7: Now we have to insert 78.
node.
Step 4: Now we have to insert 42.
19 20 21