Chapter III
Chapter III
Array supports Random Access, which means Linked List supports Sequential Access, which means to
elements can be accessed directly using their index, access any element/node in a linked list, we have to
like arr[0] for 1st element, arr[6] for 7th element sequentially traverse the complete linked list, upto that
etc. element.
Hence, accessing elements in an array is fast.
In an array, elements are stored in contiguous In a linked list, new elements can be stored anywhere in
memory location or consecutive manner in the the memory.
memory. Address of the memory location allocated to the new
element is stored in the previous node of linked list.
In array, each element is independent and can be In case of a linked list, each node/element points to the
accessed using it's index value. next, previous, or maybe both nodes.
Array vs Linked list cont.…..
Size of the array must be specified at time Size of a Linked list is variable. It grows at
of array decalaration. runtime, as more nodes are added to it.
Example
Advantages of Linked Lists
• Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in the sequence of
nodes.
• Item navigation is forward only.
Ex:
operations
• Following are the basic operations supported by a list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Display − Displays the complete list.
• Search − Searches an element using the given key.
• Delete − Deletes an element using the given key.
Insertion Operation
• Adding a new node in linked list is a more than one step activity.
• First, create a node using the same structure and find the location
where it has to be inserted.
Deletion Operation
To delete anode in a linked list:
• First, locate the target node to be removed, by using searching
algorithms.
• The left (previous) node of the target node now should point to the
next node of the target node.
Doubly Linked List
• In a doubly linked list, each node contains a data part and two
addresses, one for the previous node and one for the next node.
• Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as
compared to Single Linked List.
• Next − Each link of a linked list contains a link to the next link called
Next.
• Prev − Each link of a linked list contains a link to the previous link
called Prev
• Each link carries a data field(s) and two link fields called next and
prev.
• Each link is linked with its next link using its next link.
• Each link is linked with its previous link using its previous link.
• The last link carries a link as null to mark the end of the list.
Basic Operations