Linked List - Note
Linked List - Note
In this case, variables get allocated In this case, variables get allocated only
permanently if your program unit gets active
It uses the data structure called It uses the data structure called heap
stack for implementing static for implementing dynamic allocation
allocation
free(ptr);
A memory leak occurs when memory is allocated using malloc() function and
not released using free() function. This results in an orphaned block which is not
referenced by anything.
Memory leak arises due to the following reasons:
Memory blocks allocated are not released even when not required.
The free statement is bypassed at the time of execution of the particular
function.
The memory allocated by malloc function is stored in a pointer variable which
was already pointing to another memory location.
6. What is Linked list? Advantage and Disadvantage of Linked list
Linked lists- It is a dynamic data-structure in which memory for each node is
allocated at run-time from the heap portion of RAM. The memory allocation for
the nodes need not be continuous as in arrays. It can be defined as a list of nodes
where each node contains the information part and the address of the next node.
Insertion/Deletion can take place from the beginning, middle or end.
The various types of linked list are:-
Single-linked list
Double-linked list
Circular linked list
Circular Double linked list
Advantages of linked lists over arrays:
a) As memory allocation is done at run-time, using a linked-list, results in
better memory utilization. This is because we end-up creating only as many
nodes as required by the user. Neither there is a wastage of memory nor a
shortage which might happen incase of arrays.
b) As in a linked list the nodes are not allocated continuous memory, again this
results in better memory utilization.
c) Insertion/Deletion is simpler and less time consuming as this requires only
the adjustment of some pointers. In an array, this process takes time as it
requires a shifting of elements.
Drawbacks:
a) Random access is not allowed. We have to access elements sequentially
starting from the first node. So we cannot do binary search with linked lists.
b) Extra memory space for a pointer is required with each element of the list.
c) Not cache friendly. Since array elements are contiguous locations, there is
locality of reference which is not there in case of linked lists.
Nodes are created for the linked list at run-time using memory allocation function,
malloc(). The nodes are created from the memory area known as heap.
The representation of a node in the linked list is done using the following self-
referential structure:
struct node{ int info;
struct node *next;
};
Each element (we will call it a node) of a list is comprising of two items - the data and a
reference to the next node. The last node has a reference to null. The entry point into a
linked list is called the head of the list. It should be noted that head is not a separate node,
but the reference to the first node. If the list is empty then the head is a null reference.
9. What are the application of single linked list?
Linked Lists can be used to implement Stacks , Queues.
Linked Lists can also be used to implement Graphs. (Adjacency list
representation of Graph).
Implementing Hash Tables :- Each Bucket of the hash table can itself be a
linked list. (Open chain hashing).
Undo functionality in Photoshop or Word . Linked list of states.
A polynomial can be represented in an array or in a linked list by simply
storing the coefficient and exponent of each term.
However, for any polynomial operation , such as addition or multiplication
of polynomials , linked list representation is more easier to deal with.
Linked lists are useful for dynamic memory allocation.
10. What are the advantages and disadvantages of single linked list?
ADVANTAGE :-
1) Insertions and Deletions can be done easily.
2) It does not need movement of elements for insertion and deletion.
3) It space is not wasted as we can get space according to our requirements.
4) Its size is not fixed.
5) It can be extended or reduced according to requirements.
6) Elements may or may not be stored in consecutive memory available,even
then we can store the data in computer.
7) It is less expensive.
*DISADVANTAGE :-
1) It requires more space as pointers are also stored with information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go through all those
elements that come before that element.
4) we can not traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked list.
There are various application of doubly linked list in the real world. Some of them
can be listed as:
Doubly linked list can be used in navigation systems where both front and back
navigation is required.
It is used by browsers to implement backward and forward navigation of visited
web pages i.e. backand forward button.
It is also used by various application to implement Undo and Redo functionality.
It can also be used to represent deck of cards in games.
It is also used to represent various states of a game.
The real life application where the circular linked list is used is our Personal
Computers, where multiple applications are running. All the running
applications are kept in a circular linked list and the OS gives a fixed time slot
to all for running. The Operating System keeps on iterating over the linked list
until all the applications are completed.
Another example can be Multiplayer games. All the Players are kept in a
Circular Linked List and the pointer keeps on moving forward as a player's
chance ends.
Circular Linked List can also be used to create Circular Queue. In a Queue we
have to keep two pointers, FRONT and REAR in memory all the time, where as
in Circular Linked List, only one pointer is required.
List initially contain some nodes, start points to first node of the
List: A node(Say M) is inserted with data = 7, so previous pointer of M points
to last node, next pointer of M points to first node and last node’s next
pointer points to this M node and first node’s previous pointer points to this M
node.
Insertion at the beginning of the list: To insert a node at the beginning of the
list, create a node(Say T) with data = 5, T next pointer points to first node of the
list, T previous pointer points to last node the list, last node’s next pointer points
to this T node, first node’s previous pointer also points this T node and at last
don’t forget to shift ‘Start’ pointer to this T node.
18. What are the advantages and disadvantages of Circular Doubly Linked
List
1. List can be traversed bothways from head to tail as well as tail to head
2. Being a circular linked list tail can be reached with one operation from head
node
Disadvantages