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

Array&Linked List

Uploaded by

shruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Array&Linked List

Uploaded by

shruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Amity School of Engineering &

Technology

Array
• Arrays are defined as the collection of
similar type of data items stored at
contiguous memory locations.
• Arrays are the derived data type in C
programming language which can store
the primitive type of data such as int,
char, double, float, etc.
• Array is the simplest data structure where
each data element can be randomly
accessed by using its index number.
Amity School of Engineering &
Technology

Properties of the Array


• Each element is of same data type and
carries a same size.
• Elements of the array are stored at
contiguous memory locations where the first
element is stored at the smallest memory
location.
• Elements of the array can be randomly
accessed since we can calculate the address
of each element of the array with the given
base address and the size of data element.
Amity School of Engineering &
Technology

Need of using Array


In computer programming, the most of the
cases requires to store the large number of
data of similar type. To store such amount of
data, we need to define a large number of
variables. It would be very difficult to
remember names of all the variables while
writing the programs. Instead of naming all
the variables with a different name, it is
better to define an array and store all the
elements into it.
Amity School of Engineering &
Technology
Complexity of Array operations
Time Complexity

Algorithm Best Case Average Case Worst Case


Access O(1) O(1) O(1)

Search O(1) O(n) O(n)

Insertion O(n) O(n) O(n)

Deletion O(n) O(n) O(n)


Amity School of Engineering &
Technology

Space Complexity
In array, space complexity is O(n).
Amity School of Engineering &
Technology
Advantages of Array
• Array provides the single name for the
group of variables of the same type
therefore, it is easy to remember the name
of all the elements of an array.
• Traversing an array is a very simple
process, we just need to increment the
base address of the array in order to visit
each element one by one.
• Any element in the array can be directly
accessed by using the index.
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology

Accessing Elements of an array


To access any random element of an array
we need the following information:
• Base Address of the array.
• Size of an element in bytes.
• Which type of indexing, array follows.
Amity School of Engineering &
Technology

Address of any element of a 1D array can


be calculated by using the following formula:

Byte address of element A[i] =


base address + size * ( i - first index)
Amity School of Engineering &
Technology
2D Array
2D array can be defined as an array of
arrays. The 2D array is organized as
matrices which can be represented as the
collection of rows and columns.
Amity School of Engineering &
Technology

The syntax of declaring two dimensional


array is very much similar to that of a one
dimensional array, given as follows.

int arr[max_rows][max_columns];
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology
Mapping 2D array to 1D array
When it comes to map a 2 dimensional
array, most of us might think that why this
mapping is required. However, 2 D arrays
exists from the user point of view. 2D arrays
are created to implement a relational
database table lookalike data structure, in
computer memory, the storage technique for
2D array is similar to that of an one
dimensional array.
Amity School of Engineering &
Technology

A 3 X 3 two dimensional array is shown in


the following image. However, this array
needs to be mapped to a one dimensional
array in order to store it into the memory.
Amity School of Engineering &
Technology
There are two main techniques of storing
2D array elements into memory
• Row Major ordering
In row major ordering, all the rows of the 2D
array are stored into the memory
contiguously. Considering the array shown
in the above image, its memory allocation
according to row major order is shown as
follows.
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology

• Column Major ordering


According to the column major ordering, all
the columns of the 2D array are stored into
the memory contiguously. The memory
allocation of the array which is shown in in
the above image is given as follows.
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology
Calculating the Address of the random
element of a 2D array
• By Row Major Order
If array is declared by a[m][n] where m is the
number of rows while n is the number of
columns, then address of an element a[i][j]
of the array stored in row major order is
calculated as,
Address(a[i][j]) = B. A. + (i * n + j) * size
where, B. A. is the base address or the
address of the first element of the array a[0]
[0] .
Amity School of Engineering &
Technology
Address of A[i][j] = BA + ((i – LR) * n + (j – LC)) * Size

i = Row Subset of an element whose address to be found,


j = Column Subset of an element whose address to be
found,
BA = Base Address,
Size= Storage size of one element store in an array(in
byte),
LR = Lower Limit of row/start row index of the matrix(If not
given assume it as zero),
LC = Lower Limit of column/start column index of the
matrix(If not given assume it as zero),
n = Number of column given in the matrix.
Amity School of Engineering &
Technology

Given an array, a[1………10][1………15]


with base value 100 and the size of each
element is 1 Byte in memory. Find the
address of a[8][6] with the help of row-major
order?
Address of A[i][j] = BA + ((i – LR) * n + (j –
LC)) * Size
Amity School of Engineering &
Technology

• Answer- 210
Amity School of Engineering &
Technology

By Column major order


If array is declared by a[m][n] where m is the
number of rows while n is the number of
columns, then address of an element a[i][j]
of the array stored in Column major order is
calculated as,
Address(a[i][j]) = BA+ ((j*m)+i)*Size
where BA is the base address of the array.
Amity School of Engineering &
Technology
Address of A[i][j] = BA + ((j – LC) * m + (i – LR))* Size

i = Row Subset of an element whose address to be found,


j = Column Subset of an element whose address to be
found,
BA = Base Address,
Size = Storage size of one element store in any array(in
byte),
LR = Lower Limit of row/start row index of matrix(If not
given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If
not given assume it as zero),
m = Number of rows given in the matrix.
Amity School of Engineering &
Technology

Given an array a[1………10][1………15]


with base value 100 and the size of each
element is 1 Byte in memory find the
address of a[8][6] with the help of column-
major order.
Address of A[i][j] = BA + ((j – LC) * m + (i –
LR))* Size
Amity School of Engineering &
Technology

• Answer- 157
Amity School of Engineering &
Technology

Sparse Matrix
A matrix can be defined as a two-
dimensional array having 'm' columns and 'n'
rows representing n*m matrix. Sparse
matrices are those matrices that have the
majority of their elements equal to zero. In
other words, the sparse matrix can be
defined as the matrix that has a greater
number of zero elements than the non-zero
elements.
Amity School of Engineering &
Technology
Why do we need to use a sparse matrix
instead of a simple matrix?
We can also use the simple matrix to store
the elements in the memory; then why do
we need to use the sparse matrix. The
following are the advantages of using a
sparse matrix:
• Storage: As we know, a sparse matrix that
contains lesser non-zero elements than
zero so less memory can be used to store
elements. It evaluates only the non-zero
elements.
Amity School of Engineering &
Technology

• Computing time: In the case of searching


of sparse matrix, we need to traverse only
the non-zero elements rather than
traversing all the sparse matrix elements.
It saves computing time by logically
designing a data structure traversing non-
zero elements.
Amity School of Engineering &
Technology

Sparse Matrix Representation


The non-zero elements can be stored with
triples, i.e., rows, columns, and value. The
sparse matrix can be represented in the
following ways:
• Array representation
• Linked list representation
Amity School of Engineering &
Technology
Array Representation
The 2d array can be used to represent a
sparse matrix in which there are three
row/column named as:
• Row: It is an index of a row where a non-
zero element is located.
• Column: It is an index of the column where
a non-zero element is located.
• Value: The value of the non-zero element
is located at the index (row, column).
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology

Linked List Representation


In linked list representation, linked list data
structure is used to represent a sparse
matrix. In linked list representation, each
node consists of four fields whereas, in array
representation, there are three fields, i.e.,
row, column, and value.
Amity School of Engineering &
Technology

The following are the fields in the linked list:


• Row: It is an index of row where a non-
zero element is located.
• Column: It is an index of column where a
non-zero element is located.
• Value: It is the value of the non-zero
element which is located at the index (row,
column).
• Next node: It stores the address of the
next node.
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology
Amity School of Engineering &
Technology

Linked List
A linked list is also a collection of elements,
but the elements are not stored in a
consecutive location. A linked list can also
be defined as the collection of the nodes
in which one node is connected to
another node, and node consists of two
parts, i.e., one is the data part and the
second one is the address part.
Amity School of Engineering &
Technology

Each node contains the data and the


address of the next node. The last node of
the linked list contains the NULL value in the
address part.
Amity School of Engineering &
Technology
How can we declare the Linked list?
The structure of a linked list can be defined
as:
struct node
{
int data;
struct node *next;
}
Amity School of Engineering &
Technology
Advantages of using a Linked list over
Array

Dynamic data structure:-The size of the


linked list is not fixed as it can vary
according to our requirements.
Amity School of Engineering &
Technology

Insertion and Deletion:


Insertion and deletion in linked list are easier
than array as the elements in an array are
stored in a consecutive location. In contrast,
in the case of a linked list, the elements are
stored in a random location. The complexity
for insertion and deletion of elements from
the beginning is O(1) in the linked list, while
in the case of an array, the complexity would
be O(n).
Amity School of Engineering &
Technology

Memory efficient
Its memory consumption is efficient as the
size of the linked list can grow or shrink
according to our requirements.
Amity School of Engineering &
Technology

Disadvantages of Linked list


Memory usage:-
The node in a linked list occupies more
memory than array as each node occupies
two types of variables, i.e., one is a simple
variable, and another is a pointer variable
that occupies space in the memory.
Amity School of Engineering &
Technology

• Traversal
In a linked list, the traversal is not easy. If
we want to access the element in a linked
list, we cannot access the element
randomly, but in the case of an array, we
can randomly access the element by
index.
Amity School of Engineering &
Technology

• Reverse traversing
In a linked list, backtracking or reverse
traversing is difficult. In a doubly linked list,
it is easier but requires more memory to
store the back pointer.
Amity School of Engineering &
Technology
Applications of Linked List
With the help of a linked list, the polynomials
can be represented as well as we can
perform the operations on the polynomial.
We know that polynomial is a collection of
terms in which each term contains
coefficient and power. The coefficients and
power of each term are stored as node and
link pointer points to the next element in a
linked list, so linked list can be used to
create, delete and display the polynomial.
Amity School of Engineering &
Technology

• A sparse matrix is used in scientific


computation and numerical analysis. So, a
linked list is used to represent the sparse
matrix.
• The various operations like student's
details, employee's details or product
details can be implemented using the
linked list as the linked list uses the
structure data type that can hold different
data types.
Amity School of Engineering &
Technology

• Stack, Queue, tree and various other data


structures can be implemented using a
linked list.
• The graph is a collection of edges and
vertices, and the graph can be
represented as an adjacency matrix and
adjacency list. If we want to represent the
graph as an adjacency matrix, then it can
be implemented as an array. If we want to
represent the graph as an adjacency list,
then it can be implemented as a linked list.
Amity School of Engineering &
Technology

• To implement hashing, we require hash


tables. The hash table contains entries
that are implemented using linked list.
• A linked list can be used to implement
dynamic memory allocation. The dynamic
memory allocation is the memory
allocation done at the run-time.

You might also like