0% found this document useful (0 votes)
10 views

Data-Structure 6

Uploaded by

Sojitra Darshil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Data-Structure 6

Uploaded by

Sojitra Darshil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 Key features of an algorithm:- :15 (4) Insertion and deletion operation are (4) Insertion and deletion operation

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

ROW 1 A[0][0] A[0][1] A[0][2]


 Example:
ROW 2 A[1][0] A[1][1] A[1][2]
Sum of Two Numbers:
Sr. step 1 − START ADD
Linear Data Structure Non-Linear data Structure The address of element A[i, j] can be obtained by evaluating expression:
No. step 2 − get values of a & b
1 Every item is related to its previous and next Every item is attached with many other step 3 − c ← a + b
Loc (A [i, j]) = Base + ((i – L1) * n + (j – L2))* s
item. items. step 4 − display c
2 Data is arranged in linear sequence. Data is not arranged in sequence. step 5 − STOP
i is the row index.
3 Data items can be traversed in a single run. Data cannot be traversed in a single run. j is the column index.
4 Examples: Array, Stack, Queue, Linked List. Examples: Tree, Graph. Q.3 Differentiate between list and array.
m is the no. of rows.
5 Implementation is Easy. Implementation is Difficult. Ans.
n is the no of columns.
List Array
L1 is the lower bound for row.
(1) No. of elements in a list are not fixed (1) No. of elements in an array is fixed. L2 is the lower bound for column.
Q.2 Give definition of an algorithm. Explain key features of an algorithm.
(variable).(Dynamic Data structure) (Static Data structure)
Ans. (2) List is an ordered set of elements. (2) Array is an unordered set of element 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
An algorithm is a step by step method of solving a problem. It is commonly used for data (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.
processing, calculation and other related computer and mathematical operations. 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

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( )

Step-2: [Increment rear pointer] 2. Calloc( )


1. (A-B)+C*A+B
RearRear+1 3. Realloc( )
Step-1: (-AB)+C*A+B
Step-3: [Insert element] 4. Free( )
Step-2:(-AB)+*CA+B
Step-3:+(-AB)*CA+B Q[Rear] Val
Step-4:[Is front pointer properly set?] Q. 14 Define Linked List. List out type of Linked List.& Explain Singly Linked List.
Step-4:++-AB*CAB
If Front=0 then  Linked list is a collection of node. Each node in the list consist of two parts::

Front1 1.. Information(INFO)


2. (B*C/D)/(D/C+E)
Return 2.. Address or printer to next node (LINK).
Step-1:(*BC/D)/(D/C+E)
Step-2:(/*BCD)/(D/C+E) Step-5: [Finished] Type of Linked List
Step-3:(/*BCD)/(/DC+E) Exit 1. Singly Linked List
Step-4:(/*BCD)/(+/DCE) 2. Circular Linked List
Step-5://*BCD+/DCE 3. Doubly Linked List
Q. 12 Comparison of LIFO and FIFO.
LIFO FIFO Singly Linked List
Q. 11 Define Queue & also explain Insert Operation of Queue.  Singly Linked List is a collection of variable number of nodes in which each node
(1) In LIFO the insertion and deletion (1) In FIFO the insertion and deletion operation
 A queue is a linear list in which insertion is performed at one end called rear end and consists of two parts. First part contains a value of the node and second part contains an
operation are performed at only one end. are performed at two different ends.
deletion is performed at another end of the list called front end. address of the next node
 Since insertion is performed at one end and deletion is performed at another end the (2) In LIFO the element which is inserted last (2) In FIFO the element which is inserted first is
 Consider Following example in which Singly Linked List consist of three nodes.
is first to delete. first to delete.
element which is inserted first is first to delete. So it is also known as First in First out  Each node having INFO part and LINK part.
(3) LIFO require only one pointer called TOP (3) FIFO requires two pointers called front and
(FIFO) or First Come First Serve (FCFS) data structure. INFO part contains value of the node.
rear.
LINK part contains address of the next node in the linked list.

7 8 9

Else SAVE = FIRST


NEW_NODE=AVAIL Repeat while SAVE->LINK ≠ NULL
AVAIL = AVAIL->LINK SAVE = SAVE->LINK
Step 2: If FIRST = NULL then SAVE->LINK = NEW_NODE
Q. 15 Write a short note on Circular Linked List.
NEW_NODE -> INFO = X Step 3: Exit
 A list in which last node contains a link or pointer to the first node in the list is known
NEW_NODE -> LINK = NULL
as circular linked list.. Q.18 Explain Bubble Sort with algorithm.
FIRST = NEW_NODE
 Representation of circular linked list is shown bellow:
Else
 Bubble sort is easy to understand and simple sorting technique.
NEW_NODE -> INFO = X
 During the first pass element 1 and 2 are compared and if they are out of order then
NEW_NODE -> LINK = FIRST
they are interchanged. This process is repeated for elements 2 and 3 and so on.
FIRST = NEW_NODE
 After the end of first pass the record with the largest value is placed at nth(last) position.
Step 3: Exit Algorithm:
NEW_NODE -> INFO = X Bubble_Sort(List,N)
NEW_NODE -> LINK = FIRST Where, List-> Array of N Elements
FIRST = NEW_NODE N-> Size of array(Total No of Elements)
Step 4: Exit Step:1 [Initialization]
i0
Step:2 while(i<N-1) repeat thru Step 7
Q. 17 Write an Algorithms to Insert New Node at end of Linked List.
Step:3 j0
Step 1: If AVAIL=NULL then Step:4 while (j<N-i-1) repeat thru Step 6
Write “Availability Stack is Empty” Step:5 if (List[j] > List[j+1])
 In a circular linked list there are two methods to know if a node is the first node or not. Else (i) tempList[j]
NEW_NODE=AVAIL (ii) List[j]List[j+1]
 Either a external pointer, list, points the first node or
(iii) List[j+1]temp
 A header node is placed as the first node of the circular list. AVAIL = AVAIL->LINK
Step:6 jj+1
 The header node can be separated from the others by either heaving a sentinel value as Step 2: If FIRST = NULL then
Step:7ii+1
the info part or having a dedicated flag variable to specify if the node is a header node NEW_NODE -> INFO = X
Step :8 [Finished]
or not. NEW_NODE -> LINK = NULL Exit.
FIRST = NEW_NODE

Q. 16 Write an Algorithm for Singly linked list beginning. Else Q-19 Explain Selection Sort with algorithm.

Step 1: If AVAIL=NULL then NEW_NODE -> INFO = X


 It starts from first element and searches the entire array until it find smallest element.
Write “Availability Stack is Empty” NEW_NODE -> LINK = NULL
Then smallest value interchanges with the first element.

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) ji 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
i0 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 ji+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) tempList[i]  The same process is repeated until all the elements are sorted.

(ii) List[i]List[j] Q-21 ExplainRadix Sort with Example.


(iii) List[j]temp  We use ten pockets for the digits 0 to 9. Q-22 Define Hashing. Also explain different Hash Table Methods to build various
Step:6 jj+1  Consider the following set of data:
Step:7ii+1 42 23 74 11 65 57 94 36 99 87 70 81 61 Hash Functions.
Step :8 [Finished]  During first pass we separate the unit digit of the number and place the number in to  This technique uses the hashing function say H which maps the Key to the particular
Exit. appropriate pocket according to its unit digit. address of the record.
 For example the first number is 42 so we separate the unit digit of the number 42  Hashing function provides key to address transformation.
which is 2 so we place the number in pocket 2. Same procedure is repeated for  A hashing function H maps the Key space K into an address space.
Q-20 Explain Insertion sort with algorithm
remaining numbers.  Some of the most widely used hashing functions are :
 Suppose L is the list of n elements.  After first pass the numbers in each pockets are as follow: 1) Division Method
 The insertion sort technique scans the list L from L[1] to L[n].
2) The Mid square Method
 Inserting each element in to its proper position in the previously sorted sub list L [1], L 61
3) The Folding Method
[2]… L [i-1]. 81 94 87
4) Multiplicative Hashing
Algorithm 70 11 42 23 74 65 36 57 99
Insertion Sort(a,N) 1) Division Method
Pocket: 0 1 2 3 4 5 6 7 8 9
Where, a -> Array  Hashing function defined as: H(x) = x mod m + 1

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.

 Step 6: Now we have to insert 64.


 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.

 Binary Search Tree:

 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.

Q-25 Construct binary search tree for following set of data:


 Step 5: Now we have to insert 15
45 68 35 42 15 64 78

 Step 1: First element is 45 so it is inserted as a root node of the tree.

19 20 21

You might also like