Professional Documents
Culture Documents
Array & Linked List
Array & Linked List
-----------
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.
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
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.
===============================================================================
===============================================================================
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.
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.
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
struct node {
int data;
}
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