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

Buet Admission Quest Basic

Data structures organize data in a computer to be used effectively. Common operations on data structures include insertion, deletion, modification, searching, sorting, traversing, and merging. There are two main types of data structures - linear and non-linear. Linear data structures arrange items in a sequence while non-linear structures attach items without a defined sequence. Memory for data structures can be allocated at compile-time (statically) or runtime (dynamically). Arrays are a fundamental data structure that store similar data items in a linear order through indexes.

Uploaded by

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

Buet Admission Quest Basic

Data structures organize data in a computer to be used effectively. Common operations on data structures include insertion, deletion, modification, searching, sorting, traversing, and merging. There are two main types of data structures - linear and non-linear. Linear data structures arrange items in a sequence while non-linear structures attach items without a defined sequence. Memory for data structures can be allocated at compile-time (statically) or runtime (dynamically). Arrays are a fundamental data structure that store similar data items in a linear order through indexes.

Uploaded by

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

Data Structures

A data structure is a particular way of organizing data in a computer so that it can be


used effectively.

General operations performed on a data structure are:-

 Insertion: to add items to it.


 Deletion: to remove items from it.
 Modification: to modify items.
 Searching: to find a particular element.
 Sorting: to sort the items in a required order.
 Traversing: to visit all the element.
 Merging: combing elements of two similar data structures.
Linear Data Structure Non-Linear Data Structure
Every item is related to its previous and next Every item is attached with many other
time. items.
Data is arranged in linear sequence. Data is not arranged in sequence.
Data items can be traversed in a single run. Data cannot be traversed in a single run.
Eg. Array, Stacks, linked list, queue. Eg. Tree, graph.
Implementation is easy. Implementation is difficult.

Memory Allocations in Data Structures


Memory allocation is the process of setting aside sections of memory in a program to be
used to store variables, and instances of structures and classes.

There are two types of memory allocations possible in C:

 Compile-time or Static allocation.


 Run-time or Dynamic allocation (using pointers).

Compile-time or Static allocation: - Static memory allocation allocated by the compiler.


Exact size and type of memory must be known at compile time.

Run-time or Dynamic allocation: - Dynamic memory allocation is the process of


assigning the memory space during the execution time or the run time.

C provides the following dynamic allocation and de-allocation functions:

 malloc()
 calloc()
 free()
 realloc()

While allocating memory on heap we need to delete the memory manually as memory is
not deallocated by the compiler itself even if the scope of allocated memory finishes (as
in case of stack).
Static Memory Allocation Dynamic Memory Allocation
Memory allocated during compile time is Memory allocation done at the time of
called static memory allocation. execution is known as dynamic memory
allocation.
It uses stack for managing the static It uses heap for managing the dynamic
allocation of memory. allocation of memory
It is less efficient It is more efficient
Faster execution than dynamic Slower execution than static
In static memory allocation, once the In dynamic memory allocation, when
memory is allocated, the memory size memory is allocated the memory size
cannot change. can be changed.

Array Data Structure

 An array is a collection of variables in the same datatype.


 Array index starts from 0
 In general, an array of size N will have elements from index 0 to N-1.
 We can’t group different data types in the array. Like, a combination of integer
and char, char and float etc.
 Hence array is called as the homogeneous data type.
Mapping a two-dimensional array to linear addressing. Assume a 4x3 array (e.g., T
a[4][3]; where T is some type and a is the name of the array). The array may be stored in
main memory in one of two ways: row major (data is entered row by row) or column major
(data is entered column by column).
To find the address of any element in a 2-Dimensional array there are the following two
ways-

 Row Major Order - Address of A[i][j] = B.A + (i * n + j) * data_type_size.


 Column Major Order - Address of A[i][j] = B.A + ( j* m + i ) * data_type_size.

Example: Given an array, arr[4][3] with base value 2000 and the size of each
element is 2 Byte in memory. Find the address of arr[1][0] with the help of row-
major order and column major order?

Solution:

Here, m=4, n=3, Base address=2000, i=1, j=0

Row Major Order: Loc( arr[1][0] ) = 2000+ (1*3+0)*2=2006

Column Major Order: Loc( arr[1][0] ) = 2000+ (0*4+1)*2=2002

 Row Major Order - Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))


 Column Major Order - Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Example: Given an array, arr[1………10][1………15] with base value 100 and
the size of each element is 1 Byte in memory. Find the address of arr[8][6] with
the help of row-major order and column major order?

Solution:

Here:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15

Number of column given in the matrix M = Upper Bound – Lower Bound + 1


= 10 – 1 + 1
= 10

Row Major Formula:

Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))

Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))


= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

Column Major Formula:

Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))


Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157

You might also like