Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Array

-----------
1) What is an array?
An array is a homogeneous data structure having the same data type elements. That stores in a
sequence of consecutively numbered objects--allocated in contiguous memory. Each object of the
array can be accessed by using its number (i.e., index).
An array is defined as the collection of similar types of data items stored at contiguous memory locations.
They are the derived data type in C programming language which can store the primitive type of data such
as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The
array is the simplest data structure where each data element can be randomly accessed by using its index
number.
Big O notation : It is a mathematical notation is use to measure how running time or space requirement for
your program grows as input size grows.
Θ Notation: The theta notation bounds a function from above and below, so it defines exact
asymptotic behavior.
Notation Ω(n) is the formal way to express the lower bound of an algorithm's running time.

2. Properties:-
a) Each element of an array is of the same data type and carries the same size, i.e., int = 4 bytes.
b) Elements of the array are stored at contiguous memory locations where the first element is stored at
the smallest memory location.
c) 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 the data element.
3. Advantages:-
1) Code Optimization: Less code to access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.

4. Disadvantages:-
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So,
it doesn't grow the size dynamically like LinkedList.
2) The size of the array must be known in advance before using it in the program.
3) Increasing the size of the array is a time taking process. It is almost impossible to expand the size of
the array at run time.

5. Declaration of C Array:- data_type array_name[array_size]; Ex:- int marks[5];


Array Index: The location of an element in an array has an index, which identifies the element.
Array index starts from 0.
Array element: Items stored in an array is called an element. The elements can be accessed via its index.
Array Length: The length of an array is defined based on the number of elements an array can
store. In the above example, the array length is 6 which means that it can store 6 elements.
6. Why does the array index start from Zero?
This means that the index is used as an offset. The first element of the array is exactly
contained in the memory location that the array refers to (0 elements away), so it should be
denoted as array[0].
7. Array Operation:
a)Traverse − Print all the elements in the array one by one.
b)Insertion − Adds an element at the given index.
c)Deletion − Deletes an element at the given index.
d)Search − Searches an element in the array using the given index or the value.
e)Update − Updates an element at the given index.

8. Types of Arrays: a) One-dimensional array b) Multi-dimensional array


One-Dimensional Array:
A one-dimensional array is also called a single-dimensional array where the elements will be accessed in
sequential order. This type of array will be accessed either by column or row index.
Multi-Dimensional Array:
When the number of dimensions specified is more than one, then it is called a multi-dimensional array.
Multidimensional arrays include 2D arrays and 3D arrays.
9. Space complexity of an array is O(1).
Space complexity is the amount of memory used by the algorithm (including the input values to
the algorithm) to execute and produce the result.
Auxiliary Space is the extra space or the temporary space used by the algorithm during its
execution.
Space Complexity = Auxiliary Space + Input space.

Time complexity is the amount of time taken by an algorithm to run, as a function of the
length of the input. It measures the time taken to execute each statement of code in an
algorithm.

Malloc

The “malloc” or “memory allocation” method in C is used to dynamically allocate a


single large block of memory with the specified size. It returns a pointer of type void which
can be cast into a pointer of any form. It doesn’t Iniatialize memory at execution time so
that it has initializes each block with the default garbage value initially.

ptr = (int*) malloc(100 * sizeof(int));

“Calloc” or “contiguous allocation” method in C is used to dynamically allocate the


specified number of blocks of memory of the specified type. It is very much similar to
malloc() but has two different points and these are:

1. It initializes each block with a default value ‘0’.


2. It has two parameters or arguments as compare to malloc().
3. ptr = (float*) calloc(25, sizeof(float));

Free” method in C is used to dynamically de-allocate the memory. The memory allocated using
functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is
used,whenever the dynamic memory allocation takes place. It helps to reduce wastage of
memory by freeing it.free(ptr);
Realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of
a previously allocated memory. In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory.

ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'.

===============================================================================
===============================================================================

Linked List
------------------

A linked list is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers.

a) A linked list can be defined as a collection of objects called nodes that are randomly stored in the
memory.
b) A node contains two fields i.e. data stored at that particular address and the pointer which contains the
address of the next node in the memory.
c) The last node of the list contains a pointer to the null.

Characteristic of Linked List:


a) The list is not required to be continuously present in the memory. The node can reside anywhere in the
memory and be linked together to make a list. This achieves optimized utilization of space.
b) List size is limited to the memory size and doesn't need to be declared in advance.
c) Empty node can not be present in the linked list.
d) We can store values of primitive types or objects in the singly linked list.

Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is complicated because the room has to be
created for the new elements and to create a room existing elements have to be shifted.

Advantages over arrays : 1) Dynamic size 2) Ease of insertion/deletion


Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node.
So we cannot do a binary search with linked lists efficiently with its default implementation.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache-friendly: Since array elements are contiguous locations, there is the locality of reference
which is not there in the case of linked lists.

Representation:
A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If
the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node

// A linked list node


struct Node {
int data;
struct Node* next;
};
Single Linked List: Defined as the collection of an ordered set of elements. A node in the singly linked list
consists of two parts: the data part and the linked part. The data part of the node stores actual information
that is to be represented by the node while the linked part of the node stores the address of its immediate
successor.

Eta vlo kore dekbi


Doubly Ended Linked List: Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence. Therefore, in a doubly-linked list, a node
consists of three parts: node data, a pointer to the next node in sequence (next pointer), a pointer to the
previous node (previous pointer).

struct node {

struct node *prev;

int data;

struct node *next;

}
Circular Linked List: The last node of the list contains a pointer to the first node of the list. We can have
a circular singly linked list as well as a circular doubly linked list. We traverse a circular singly linked list until
we reach the same node where we started. The circular singly liked list has no beginning and no end. There
is no null value present in the next part of any of the nodes.

Example: Task maintenance in operating systems, browser surfing where a record of pages visited in the
past by the user, is maintained in the form of circular linked lists.
Circular Doubly Linked List: It is a more complex type of data structure in which a node contains
pointers to its previous node as well as the next node. The circular doubly linked list doesn't contain NULL
in any of the nodes. The last node of the list contains the address of the first node of the list. The first node
of the list also contains the address of the last node in its previous pointer.

It demands more space per node and more expensive basic operations. However, a circular doubly linked
list provides easy manipulation of the pointers and the searching becomes twice as efficient.
For More Information about Linked List:
https://www.youtube.com/watch?
v=R9PTBwOzceo&list=PLBlnK6fEyqRj9lld8sWIUNwlKfdUoPd1Y&index=31
For More Information about Array:
https://www.youtube.com/watch?v=55l-
aZ7_F24&list=PLBlnK6fEyqRjoG6aJ4FvFU1tlXbjLBiOP&index=2

You might also like