What is Linked List in Data Structure with example ?
- A linked list is a sequence of data structures, which are connected together through links.
- Linked List is a sequence of links which contains objects. Each link contains a connection to another link. Linked list is the second most-used data structure after array.
- Link − Each link of a linked list can store a data called an element.
- Next − Each link of a linked list contains a link to the next link called Next.
- LinkedList − A Linked List contains the connection link to the first link called First.
- A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
Types of Linked Lists
- Singly linked list
- Doubly linked list
- Circular linked list
Singly linked list
- A singly linked list is Item navigation is forward only.
Doubly linked list
- A doubly linked list is a list that has two references, one to the next node and another to previous node.
Circular linked list
- Another important type of a linked list is called a circular linked list where last node of the list points back to the first node (or the head) of the list.
Doubly Circular linked list
- Circular doubly linked list is a type of data structure in which a node contain pointers to its previous node as well as the next node.
- Circular doubly linked list doesn’t contain NULL in any of the node.
- The last node contains the address of the first node of the list. The first node also contain address of the last node in its previous pointer.
Sample Code:
Output:
Advantages of Linked List
- Linked list is dynamic in nature which allocates the memory when required.
- In linked list, stack and queue can be easily executed.
- It reduces the access time.
- Insert and delete operation can be easily implemented in linked list.
Disadvantages of Linked List
- Reverse traversing is difficult in linked list.
- Linked list has to access each node sequentially; no element can be accessed randomly.
- In linked list, the memory is wasted as pointer requires extra memory for storage.