CBSE-Class-12-Computer-Science-Arrays--Linked-List
CBSE-Class-12-Computer-Science-Arrays--Linked-List
com
Unit – II
Chapter -9 & 10
ARRAYS & LINKED LIST
Defination of Datastructure
A data structure is a logical method of representing data in memory using the simple and complex
data types provided by the language.
Arrays:
An array is collection of the homogeneous elements that are referred by a common name. It is also
called a subscripted variable as the elements of an array are used by the name of an array and an
index or subscript.
Array are of two types:
1. One-dimensional arrays 2. Multi-dimensional arrays.
Address Calculation:
The array elements are stored in contiguous memory locations by sequential allocation technique.
Address of arr[i] = B+(I-LB)*S
Sequential Allocation:
The process of storing elements in a fixed order in a data structure where the time required for such
access is dependent on the order of the elements.
Using Row Major order the add of a [i] [j] is given by,
Address of a [i] [j]= B+[(i-LB1)*N+(j-LB2)]*S
Using column Major order the add of a [i] [j] is given by,
Address of a [i] [j]= B+[(i-LB1)+(j-LB2)*M]*
Selection sort: it is very simple way of sorting data stored in a one dimensional array. All
the elements in the array are searched for the smallest element. The selected element is
exchanged with the first element in the array.
Bubble sort: it is also very simple sorting algorithm. This method proceeds by looking the
array for left to right, and whenever a pair of adjacent elements is found to be out of order,
52
(f) Merging: merging is the process of combining two or more sorted arrays into another array
which is also sorted.
Concatenation of Two Linear Array
Concatenation means joining the element of two arrays to form a new array. First copy all
the elements of one array and then copy all the elements of other array into the new array.
The size of the concatenated array must be equal to or greater than that of the sum of sizes of
the two arrays to be concatenated.
Two Dimensional Array:
A two dimensional array(having two subscripts) is suitable for table processing or matrix
manipulation.
Traversal: it means visiting each element one after the other.
Finding sum/difference of two NM arrays containing numeric values:
The sum/difference of two NM arrays containing numeric values can be obtained by
adding/subtracting the corresponding elements of the two arrays. The result can either be
directly displayed on monitor or stored into third array of size NM and then this resulting
array can be displayed.
For example, the sum of two arrays A and B of size 32 is shown below.
4 6 5 2 9 8
7 1 1 8 8 9
5 3 2 4 7 7
Array A Array B resultant after addition
If difference of two arrays of size 33 is required then we get result as illustrated by the following
example :
40 25 73 31 20 41 9 5 32
18 55 29 10 36 15 8 19 14
70 62 47 20 32 17 50 30 30
Array A Array B Result after difference
For example,
1 2 3 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9
Given Array Array after interchange
Stacks :
A stack is a list of elements in which an element may be inserted or deleted only at one end, called
the TOP of the stack. This means, in particular, that elements are removed from a stacks in the
reverse order of that in which they were inserted into the stack.
53
Queue:
A queue is a linear list of elements in which deletions can take place only at one end, called the
front, and insertions can take place only at the other end, called the rear. The terms “front” and
“rear” are used in describing a linear list only when it is implemented as a queue.
A queues are also called first-in first-out(FIFO) list.
Linked List:
A linked list is a linear collection of data elements, called nodes pointing to the next nodes by
means of pointers.
As a push can occur only at the top , top gets modified every time.
For instance, if we have a stack as shown:---
Solved Questions
Q1. What is the difference between linear and non- linear data structures?
Ans. Single level data structures where elements form a sequence are called linear data structures
eg. Stacks, queues, linked list etc. are linear data structures.
54
Unsolved Questions
Q. 1. How is computer memory allotted for a 2D array?
Q. 2. Binary search is to be used on the following sorted array to search for 30 and 60.
Index: 1 2 3 4 5 6 7 8 9 10
Value: 11 22 30 33 40 44 55 60 66 70
Give the index of the element that would be compared with at every step. Repeat the process
replacing 30 by 60.
Q. 3. Consider the single dimensional array AAA [45] having base address 300 and 4 bytes is the
size of each element of the array. Find the address of AAA [10], AAA [25] and AAA [40].
55
Q. 5. Write a C++ function to find and display the sum of each row and each column of a 2
dimensional array of type float. Use the array and its size as parameters with float as the return type.