Data Structure
Data Structure
(iv) Entity :
Ans. An entity is something that has certain attributes or properties which may be assigned
values.
The values themselves may be numeric or non-numeric.
Asked in Board
Exam (March 2005, 2009,2016; Oct. 2007,2014) Important
(v) Field: Field is a single elementary unit of information representing an attribute of an entity.
iii) The data structure should be simple and it should show the relationship between data
elements.
iv) Types:
(i) Linear data structure (ii) Non-linear data structure.
In linear data structure, data elements are stored in consecutive memory locations or by
using linked representation. e.g. arrays, linked list.
In non-linear data structures, the linear order cannot be maintained between data elements
Generally data elements have hierarchical relationship between them. e.g. trees
v) Computer language provides different data structures like arrays, stack, queue, tree etc.
Accessing, each record or element exactly once, so that it can be processed is called
traversing.
For e.g: multiplying each element of an array by 6.
(ii) Inserting :
Adding a new record to the existing structure is called as inserting.
(iii) Deleting :
Removing a record from the existing structure is called as deleting.
(iv) Searching :
Finding the location of a record with given key values or finding the locations of all records
which satisfy one or more conditions is called as searching.
(v) Sorting :
Arranging records in some logical order is called as sorting.
(vi) Merging :
Merging means combining the records in two different sorted files into a single sorted file.
Algorithmic Notation
Q. 4. What is an algorithm?
Ans. :
i) An algorithm is a finite step by step list of well-defined instructions for solving a particular
problem.
(b) The second part of algorithm consists of steps in algorithm that are executed one after
the another, generally beginning with step 1, unless stated otherwise. The control
can be transferred to step n, by the statement “go to step n”.
The algorithm is completed, when the statement ‘Exit’ or ‘Stop’ is encountered.
[End of If structure]
Step 3: [Increment counter] set k: = k + 1
[End of If structure]
Step 6: Exit
ii) The Sequence may be present explicitly by means of numbered step or by the order in
which modules are written.
iii) In short in sequential logic or sequential flow, modules of an algorithm are executed one
after the another.
Q.6 Describe conditional flow or selection logic.
Ans : Selection logic uses number of conditions, which cause selection of one out of several
alternative modules. The structure which implement this type of logic is known as selection
logic, or conditional structure:
If condition, then :
[module A]
[End of If structure]
The logic of this structure is as follows : If condition is satisfied (true) then module A, which
consists of number of statements, is executed. Otherwise, module A is skipped and next
module of algorithm is executed.
If condition, then :
[module A]
Else :
[module B]
[End of If structure]
The logic of this structure is as follows
If the condition is satisfied, then module A will be executed otherwise module B will be
executed.
[module A1]
[module A2]
[module An]
else :
[module B]
[End of If structure]
The logic of this structure allows only one module to be executed. The module following the
condition, which is satisfied the condition will be executed. If no condition is satisfied, then the
module, which follows last Else statement will be executed.
[module]
Here, K is called index variable, R and S are initial and final values of K and T is increment.
The logic of this structure is as follows :
At first, the body of loop i.e. module will be executed with K = R and then with K = R + T, then
with K = R + 2T and so on, until K <= S. The loop ends when K > S. If T is negative then K
decreases in value and loop ends when K < S.
[End of loop]
Here, body of loop i.e. module is executed repeatedly, unit the condition is satisfied.
There must be a statement before the structure that initializes the condition controlling the
loop and there must be a statement in the body of the loop that changes the condition.
Given a nonempty array DATA with N numerical values. This algorithm finds the location LOC
and the value MAX of the largest element of DATA
[End of If structure]
4. Set K := K +1
6. Exit
Ans :
(i) Sequence logic :
In the sequence logic modules are executed sequentially, one after the another. The sequence
may be present explicitly by means of numbered step or by the order in which modules are
written.
1. The elements of the array are referenced respectively by an index set (subscript)
consisting of ‘n’ consecutive numbers.
2. The elements of the array are stored respectively in successive memory locations.
In general, the size or length of the array can be obtained from the index set by the formul:-
Length = UB - LB +1
ii) Computer does not need to keep track of the address of every element of array. It just
requires the address of first element of array, LA, denoted by Base (LA) and called the base
address of linear array LA.
iii) Using this base address, the computer calculates address of any element of array by using
the formula.
where,
Given:- K = 1965
W=4
LB = 1932
The address of the array element for the year 1965 can be obtained -
Here LA is a linear array with lower bound LB and upper bound UB. Following algorithm apply
operation PROCESS to each element of LA.
set K: = LB
set K := K + 1
Step 5: Exit
OR
This algorithm traverses a linear array LA with lower bound LB and upper bound UB.
Step 1: Repeat FOR K= LB To UB:
[End of loop]
Step 2: Exit
ii) The element can be easily inserted at the end of array. But for insertion in the middle of
array, it is required to move the elements of array one byte forward.
iii) The following algorithm inserts a data element ITEM into the kth position in an array LA
with N elements.
Algorithm :
INSERT (LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer, such that K <= N. This
algorithm inserts an element ITEM at Kthposition in LA.
Set J: = N
Set J := J - 1
Step 6: [Reset N]
Set N := N + 1
Step 7: Exit
Ans :
ii) Deletion at the end of an array is easier. But, if to delete an element from mid of array. then
to move the elements of array one location upward.
[End of loop]
Set N := N - 1
Step 4: Exit
Ans :
write : K
[End of If structure]
[End of loop]
2. Exit
1. Set N := 0
N := N+1
[End of If structure]
[End of loop]
3. Write : N
4. Exit
(c) To find number of employees NL, who will be at least L years old at the end of year 1984
we want the number of employees born in ye ar 1984-L, or earlier.
1. Set NL = 0
2. Set X := 1984 - L
[End of loop]
4. Write : NL
5. Exit
Q. 18 Explain Bubble sort algorithm with suitable
example.
Asked in Board Exam (March 2002, 05, 08,12; Oct. 2005, 2008) Important
Ans:
Algorithm :
[End of lf structure]
Step 4: Exit
Explanation :
Suppose DATA is an array of N elements. Sorting these elements in ascending order means
arranging the elements such that :
DATA [1] < = DATA [2] <= …. <= DATA [N] In Bubble sort, compare DATA[1] with DATA[2]} and
exchange them it DATA[1] > DATA[2].
Next DATA[2] is compare with DATA[3]. They are exchanged if necessary. This process is
repeated till DATA [N – 1] is compared with DATA[N].
After the first pass the largest element is sink to the last position.
During the next pass, compare elements upto the last but one and second largest element
moves to the (N – 1)ˢᵗ position.
After N- 1 passes, all elements are sorted.
Pass 4:
In this way after complete execution of this algorithm, the array gets sorted in ascending
order as follows :
Ans :
Searching : Searching means to find out particular element from a given list of elements or
check whether required element is present or not in an array. There are two types of
searching algorithms as follows :
Ans :
Set LOC = 1
[End of loop]
Set LOC = 0
Step 5: Exit
Suppose ITEM = 33
11 22 33 44 55 33
Step 2; LOC = 1
Ans :
Here DATA is a sorted array with lower bound LB and upper bound UB. ITEM is given element.
BEG denotes beginning, MID denotes middle and END denotes end location of DATA.
This algorithm finds the location LOC of ITEM in DATA or sets LOC = NULL,
search is unsuccessful.
Step 1: [Initialize Variables]
Else :
[End of If structure]
Else :
LOC := NULL
[End of If structure]
Step 6: Exit
11 22 30 33 40 44 55
60 66 77 80 88 99
Suppose ITEM = 40
END = MID - 1 = 7 - 1 = 6
BEG = MID + 1 = 3 + 1 = 4
(1) Binary search algorithm is efficient as the search scope gets reduced to half the size of the
array, with each iteration.
(2) The number of comparisons required are approximately equal to log2, n which are less
than linear search.
11 22 30 33 40 44 55
Suppose ITEM = 40
MID =(5+7)/2=6
Step III: Since 44 > 40, END has its value changed by END = MID - 1 = 6 - 1 = 5
MID =(5+5)/2 = 5
In above example, only two comparisons are required because at each iteration MID is
calculated, only one half is checked.
In the same example, for linear search, 5 comparison are required.
Disadvantages :
1) The given list must be sorted.
2) The access of list must be random means the middle element can be accessed.
3) At each iteration, middle entry calculation is required.
2. Compare the desired element with all elements in an array until the match is found.
3. Insertion of an element in an array can be performed very efficiently when array is not
ordered.
4. For large size of array, time required for this search is very large.
Binary search
1. For binary search, the elements in array are stored in alphabetically or numerically in some
manner.
2. Compare the value of midpoint with desire value. If the value is greater than midpoint
value.the first half is checked, otherwise second half checked until search is successful or
interval empty.
ii) The variable is called as pointer variable, if it points to ariother variable i.e. it contains
memory address of other variable.
iii) Consider an organization, which divides its employee lisit into four groups, depending
certain conditions. Following figure shows the list of 4 groups. There are 15 employes groups
contain 2, 5, 4 and 4 employes respectively as
iv) If these groups are to be represented in memory, the most efficient way is to use 2 arrays.
The first is Employee array, which contains list of employees in all four groups sequentially,
while the second array is Group array, which is a pointer array, which contains the starting
address of each group in the Employee array, respectively.
v) It is shown in figure :
vi) Each element of Group array is a pointer, which holds the starting addresses of different
groups. Hence,it is called as pointer array.
Q. 25 What is a record ?
Asked in Board Exam (March-2011) Important
Ans :
i) A record is a collection of relative data items, each of which is called as field or attribute.
ii) Collection of records is known as files. Collection of data is frequently organized into a
hierarchy of fields, records and files.
iii) A record may contain non-homogeneous data i.e. data items of record need not to be of
same data type. In a record, natural ordering of elements is not possible. The elements in
record can be described by level number.
iv) e.g. An organization keeps records of its Employees. It contains following, data items-
Name, gender, Salary, Birthday, Address.
Name is group item consisting of First name, Middle name and Last name. Also, Birth date
and Address are group items,
1. Employee
Name
First Name
Middle Name
Last Name
Sex
Salary
Birth date
Date
Month
year
Address
City
Pincode
v) The number to the left of each variable indicates level number.
vi) Employee (30)
vii) This indicates a file of 30 records.
viii) To access first name of 3 rd employee, we should write Employee (3).Name.First name. In
this way, we can access variables in records.
Collection of data is frequently organized into hierarchy of fields i.e. records. A file is nothing
but collection of records.
(i) A record is a collection of fields, while an array is list of homogeneous data elements.
(ii) A record may contain non-homogeneous data i.e. data elements may be of different data
types.
An array always contains homogeneous data.
(iii) In a record, netural ordering of elements is not possible. Array elements can be naturally
ordered.
(iv) Elements of record are referenced by level number, while those of array can be
referenced by an index set consisting of n consecutive numbers.
Q. 27 How records are represented in memory
using array ?
Asked in Board Exam (Oct. 2002, March 2004) Important
Ans: i) Consider a record, whose structure is given below.
1. Employee
Name
First Name
Last Name
Sex
Address
City
Pincode
Phone Number
ii) To represent this record in memory, linear arrays are used.
iii) One separate linear array is used for each elementary item of record such as First name,
Last name, gender, City, Pincode, Phone no.
Following figure shows representation of above record using parallel linear arrays.
iv) The records are stored in memory using parallel linear arrays, such that for an index K of
all records, First name [K], Last name [K], Gender [K], …. belong to the same record in a file.(ie.
Kth record in the file)
2) But in entire file of records, all data elements belonging to the same identifier will be of
same type. So a file may be stored in memory as collection of arrays.
3) One array for each of data item. All the arrays should be parallel.
4) For eg.
A student file consisting three records and three fields.
OR
iii) Each node in the linked list is divided into two parts. First part is called as INFO per which
contains the information about the element or actual element and second par called as LINK
part, which is next pointers field i.e. it contains the address of next node the list.
(a) The above figure shows a linked list having six nodes.
(b) The left part of the node is the Info part, which contains information of the element, With
the right part is Link part, which is next pointers field ie. it points to next node.
(c) An arrow is drawn from Link part of one node to the next node to indicate link
(d) The address of the list is stored in Start or Name of the list.
(e) The Link part of last node is NULL pointer ie. it contains nothing.
(f) To trace the linked list, we Just equine the address ot Start or Name.
i) To store arrays in memory, require consecutive memory locations, while to store linked lists.
consecutive memorv locations are not required.
ii) Arrays can not be easily extended, while linked list can be easily extended.
ii) There is very complicated procedure to insert an element in an array. One can easily insert
an element in an linked list.
(iv) Similary, deletion of an element from array is very complicated, while deletion from
linkedlist is easy.
(v) Linked lists can be easily implemented and maintained in computer memory.
OR
Ans :
i) Linked lists can be represented in memory by using two arrays respectively known as INFO
and LINK, such that INFO[K] and LINK[K] contains information of element and next node
address respectively.
ii) The list also requires a variable ‘Name’ or ‘Start’, which contains address of first node.
Pointer field of last node denoted by NULL which indicates the end of list. e.g. Consider a
linked list given below :
It is easier to insert an element into or delete an element from a linked list than arrays.
Now, to delete second node from the list, then just transfer the LINK part of second node to
the LINK part of the first node.
(ii) Suppose F and then C are deleted from the list. After that G is inserted at
the beginning of the list. Find the final structure of the list.
(iii) Suppose G is inserted at the beginning of the list and then F after that C
is deleted from the list. Find the final structure of the list.
Ans:
i) Linear order of characters
[End of loop]
3. Exit
2. Set N = 0
set N = N + 1
[End of If structure]
5. Set Ptr = LINK [ptr]
6. Write : N
7.Exit
2. Set N := 0
Set N:=N+1
[End of If structure]
6. Write : N
7. Exit
(iii) 1. Set Ptr := START
[End of loop]
3. Exit
(i) LIFO system is last-in-first-out system. In this type of system, the element which is inserted
at last, will be deleted first.
(ii) Stack is an example of LIFO system. It is a linear system in which insertion and deletion
takes place only at one end i.e. top of the list.
(iii) The insertion operation is referred to as push and deletion operation as pop.
e.g. consider a stack of dishes. If we want to add a new dish to this stack then it is added at the
top of stack also deletion takes place from the top.
FIFO System :
(i) A FIFO system is first-in-first-out system. In this type of system, the element which is
inserted first in the list will also be deleted first.
(ii) Queue is an example of FIFO system, A queue is a linear list, in which insertion takes
place only at one end of the list known as ‘rear’ of the list and deletion takes place at the other
end, called as ‘front’ of the list.
Tree
Root :
A node which has no parent. Generally first node is called as ‘root node’. In figure, a the node
A is the root of the tree.
Leaf :
The node which has no child or children. Such nodes have degree zero. In figure a D, I, F, J, K
are the leaf nodes. Also called as terminal node.
Child :
The nodes which are reachable from a node, say u, through a single edge are called the
children of u. e.g In figure a, the children of node C are F, G, and H.
Sibling :
Children of the same parent are said to be siblings. e.g. The nodes D and E are both children
of node B. So D and E are siblings.
2. Depth / Height :
Depth of the tree as maximum level of any node in a tree. If root is level 0 than
depth or height of tree is equal to 1+ largest level number.
e.g. Depth of above tree is 3.
3. Degree :
The number of subtrees of a node is called degree of a node.
The degree of a tree is the maximum degree of the node in tree.
e.g. the degree of each node in figure is as
(b) other two subsets are themselves binary tree called left subtree and right subtree of the
original tree.
The binary tree contains 8 nodes (A to H). Root A is at the top of tree.
(6) Binary tree T1 and T2 are similar if they have same structure.
Any node N ina binary tree T has either 0, 1 or 2 successors.
Depth of a tree means maximum level of any node in a tree. Maximum number of node or
binary tree with depth n are 2n – 1.
For example :
(ii) Consider the following tree with depth 3.
So with depth 3, the total number of nodes in a given tree is
2n – 1 = 23 -1
= 8-1
=7
The tree with depth n having 2n – 1 number of total nodes.
Q. 42 Define the following :
Ans :
1. Complete Binary Tree : If all leaf nodes of a binary tree have same level number and every
non-leaf node has non-empty left and right subtrees then the tree is called as complete binary
tree. All nodes at the last level appears as far left as possible.
Linked representation uses three parallel arrays INFO and, LEFT and RIGHT and a pointer
variable ROOT such that for an index K, INFO [K] contains actual element, LEFT [K] contains
address of the Jeft child and RIGHT [K] contains address of right child.
AVAL stores address of first null node. To insert another node to tree T, it is inserted memory
location pointed by AVAIL.
Note : In above example, to insert an element K, then it will be inserted at INFO [10]. After
insertion LEFT [10] and RIGHT [10] will contain zero (null pointer) and AVAIL will contain 8 i.e.
next element is to be inserted at 8.
(b) If a node N of tree stored in TREE [K], then, its left successor is stored in TREE [2*K] and
right successor is stored in TREE [(2*K) +1]
e.g. Consider a binary tree as follows :
Group items are those all nodes having children excluding root.
A linear array is the data structure which consists of finite ordered set of homogeneous data
elements such that :
(i) The elements of the array are referenced respectively by an index set consisting or
consecutive numbers.
(ii) The elements of the array are stored respectively in successive memory locations.
(i) A linked list is a linear collection of data elements called nodes where the linear order’s
maintained with the help of pointers.
(ii) Each node in the linked list is divided into two parts. First part is called as INFO part which
contains the information about the element or actual element and second part is called as
LINK part which is next pointer field i.e, it contains the address of next node in the list.
(iii)
(c) Tree:
Tree is a non-linear hierarchical data structure which consists of finite set of one or more
nodes (i.e. collected data items such that :
2. Data items that are divided into subitems are called as ___________
(i) Group items
(ii) Elementary items
(iii) Nodes
(iv) Arrays
Ans. : (i) Group items
5. The time required to execute bubble sort algorithm having ‘n’ input items is directly
proportional to ___________
(i) n²
(ii) n
(iii) Log2n
(iv) Logen²
Ans.: (i) n²
6. Maximum number of nodes of symmetric binary tree with depth 5 are___________ (March
2002)
(i) 5
(ii) 25
(iii) 31
(iv) 32
Ans. : (iii) 31
8. Accessing each element in an array only once is called ___________ (Oct. 2002, March 2003)
(i) Searching
(ii) Inserting
(iii) Deleting
(iv) Traversing
Ans. : (iv) Traversing
11. The number of comparisons required for bubble sorting of an array of n elements
is_________(March 2004)
(i) n(n-1)/2
(ii)n/2
(iii) log2n
(iv) logign
Ans:(i) n(n – 1)/2
12. Finding the location of record with a given key value is known as ____________ (March 05,
Oct.11)
(i) Traversing
(ii) Searching
(iii) Sorting
(iv) Inserting
Ans : (ii) Searching
13. Maximum number of nodes ina symmetric binary tree with depth four are
________________ (March 2006)
(i) 4
(ii) 15
(iii) 16
(iv) 5
Ans: (ii) 15
14. In___________data structure, an element may be inserted or deleted only at one end called
Top.
(i) Queue
(ii) Array
(iii) Stack
(iv) Tree
Ans: (iii) Stack
15. Maximum number of nodes of symmetric binary tree with depth of 6 is ____________
(i) 64
(ii) 6
(iii) 63
(iv) 36
Ans: (iii) 63
16. ________________ is the only non-linear data structure from the following list. (March 2007)
(i) Array
(ii) Stack
(iii) Tree
(iv) Linked List
Ans:(iii) Tree
17. __________ is the operation of rearranging the elements of an array either in increasing and
decreasing order. (Oct 2007)
(i) Sorting
(ii) Searching
(iii) DMS
(iv) DBMS
Ans: (i) Sorting
18. The complete binary tree (Tn) has n= 15 nodes then its depth (dn) is ____________
(i) 2
(ii) 3
(iii) 4
(iv) 5
Ans: (iii)
29. ________________ data structure dove 29. Not require contiguous memory
allocation. (March 2015)
(i) Array
(ii) String
(ii) Pointer array
(iv) Linked list
Ans: (iv) Linked list
31.If a complete binary tree (Tn) has n = 1000 nodes, then its depth (Dn) is ____________ (July
2016)
(i) 10
(ii) 20
(iii) 50
(iv) 109
Ans: (i) 10
32. _______________ is the only non-linear data structure from the following list. (July 2017)
(i) Array
(ii) Stack
(iii) Tree
(iv) Linked List
Ans: (iii) Tree
33.If lower bound = 0 and upper bound = 15, then midterm is _______________ in binary, search
method. (July 2018)
(i) 6
(ii) 7
(iii) 8
(iv) 9
Ans.: (iii) 8
34. _________________ is very useful in situation when data is to be stored and retrieved in
reverse order. (March 2019)
(i) Stack
(ii) Queue
(iii) Linked List
(iv) Tree
Ans: (i) Stack
1. Hardware
2. Software
3. Input Device
4. Output Device
Ans: Software
1) Information management
2) Process
3) G.U.I.
4) None of these
Ans.: (1) Information management
1) public domain
2) free
3) paid
4) private
Ans.: (2) free
6. The time required for read-write head to move to the correct track is (March 2009)
1) Seek time
2) Rotational delay
3) Latency time
4) None of these
Ans. (1) Seek time
1) Memory management
2) Process management
3) Device driver
4) Information management
Ans. : (2) Process management
8. The time lost in turning the attention of processor from one process to other is called
as__________
1) Circuit switching
2) Band width
3) Context switching
4) None of these
Ans.: (3) Context switching
1) Creation of file
2) Halting process
3) Paging
4) None of these
Ans.: (3) Paging
10. If the page size for 2 MB memory is 2 kB, then the number of higher order bits on address
bus, used to denote page number is___________
1) 11
2) 10
3) 9
4) 8
Ans:(2) 10
1) Internal fragmentation
2) External fragmentation
3) Compaction
4) None of these
Ans. (1) Internal fragmentation
12. If a page is modified after it is loaded in main memory, then it is called as___________
1) Page fault
2) Dirty page
3) Paging
4) Locality of reference
Ans.: (2) Dirty page
1) logical
2) Virtual
3) Either physical or logical
4) Either virtual or physical
Ans.: (1) logical
1) ready
2) blocked
3) resumed
4) running
Ans. (3) resumed
1) Multiuser
2) Multitasking
3) Multithreading
4) All of the above
Ans.: (4) All of the above
1) UNIX
2) LINUX
3) MS-DOS
4) C++
Ans.: (4) C++
17. _______spread more rapidly but causes less damage to computer networks.
1) Virus
2) Worms
3) Bombs
4) None of these
Ans. (2) Worms
1) Application program
2) User program
3) Process management program
4) Antivirus program
Ans. (3) Process management program
19. ___________ is given to each process so that a process does not use the CUP indefinitely.
1) Context Switching
2) Time Slice
3) Token Time
4) Purchased Priority
Ans. (2) Time Slice