Singly Linked List: Item traversed only in forward direction.
Doubly Linked List: Item traversed in forward and backward directions.
Circular Singly Linked List: Last element contains link to the first element as next.
Singly Linked List:
Singly linked lists contain nodes which have a data part as well as an address part(pointer) i.e. next, which points to the next node in the sequence of nodes.
The operations we can perform on singly linked lists are insertion, deletion and traversal.
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.
Circular Singly Linked List :
In circular linked list the last node of the list holds the address of the first node hence forming a circular chain.
In case of array, memory is allocated in contiguous manner, hence array elements get stored in consecutive memory locations. So when you have to access any array element, all we have to do is use the array index, for example arr[2] will directly access the 3rd memory location, returning the data stored there.
But in case of linked list, data elements are allocated memory at runtime, hence the memory location can be anywhere. Therefore to be able to access every node of the linked list, address of every node is stored in the previous node, hence forming a link between every node.
We need this additional pointer because without it, the data stored at random memory locations will be lost. We need to store somewhere all the memory locations where elements are getting stored.
Yes, this requires an additional memory space with each node, which means an additional space of O(n) for every n node linked list.
Array and Linked list both we use to store linear data of same datatype, but array has some limitation
Linked List
The size of the linked list is dynamic.
Ease of insertion or deletion.
Linked List supports Sequential Access, which means to access any element/node in a linked list, we have to sequentially traverse the complete linked list, upto that element.
To access nth element of a linked list, time complexity is O(n).
In a linked list, new elements can be stored anywhere in the memory.
Memory is allocated at runtime, as and when a new node is added. It's also known as Dynamic Memory Allocation.
Linked list can be Linear(Singly) linked list, Doubly linked list or Circular linked list linked list.
Array gets memory allocated in the Stack section.
Array
The size of the arrays is fixed, so we should have knowledge of array size in advance.
Inserting and deleting data in array is difficult.
Array supports Random Access, which means elements can be accessed directly using their index, like arr[0] for 1st element, arr[6] for 7th element etc.
accessing elements in an array is fast with a constant time complexity of O(1).
In an array, elements are stored in contiguous memory location or consecutive manner in the memory.
Memory is allocated as soon as the array is declared, at compile time. It's also known as Static Memory Allocation.
Array can be single dimensional, two dimensional or multidimensional.
Whereas, linked list gets memory allocated in Heap section.
In simple words, a linked list is a collection of nodes where each node is connected to the next node through a pointer.
A linked list is a linear data structure or dynamic data structure which contain nodes and where each node contains 2 items, one is a data field and another is the reference(pointer) to the next element or node in the list.
Link list Basic Operations
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.
Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
Applications of Linked Lists
Linked lists are used to implement stacks, queues, graphs, etc.
Mostly Linked list we use for Sorting the data.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don't need to know the size in advance.