Linked List
Linked List
Linked List
What is Linked List ?
• Linked list is a linear data structure.
• It is a collection of data elements, called nodes pointing
to the next node by means of a pointer.
Data Next
10 Pointe
r
Node
Linked List
• A node is a collection of two sub-elements or parts.
• A data part that stores the element and a next part that
stores the link to the next node.
• A linked list is a non primitive type of data structure in
which each element is dynamically allocated and in
which elements point to each other to define a linear
relationship.
• Linked list require more memory compared to array
because along with value it stores pointer to next node.
• Linked lists are among the simplest and most common
data structures. They can be used to implement other
data structures like stacks, queues, and symbolic
expressions,
Node Implementation
• The first node is always use as a reference to
traverse the list and is called HEAD.
• The last nod points to NULL.
Types of linked list
• Singly Linked List
• Circular Linked List
• Doubly Linked List
• Doubly Circular Linked List
Singly Linked List
• It is basic type of linked list.
• Each node contains data and pointer to next node.
• It does not store any pointer or reference to the
previous node.
• It has successor and predecessor.
• First node does not have any predecessor while last
node does not have any successor.
• Last node has successor reference as NULL.
• Limitation of singly linked list is we can traverse only
in one direction, forward direction.
• In this type of linked list , only forward sequential
movement is possible, no direct access is allowed.
Singly Linked List
• First node of Linked List is denoted by First or
Head node.
Circular Linked List
• Circular linked list is a singly linked list where last
node points to first node in the list.
• It does not contain null pointers like singly linked
list.
• We can traverse only in one direction that is
forward direction.
Circular Linked List
• It has the biggest advantage of time saving when
we want to go from last node to first node, it
directly points to first node.
• A good example of an application where circular
linked list should be used is a timesharing
problem solved by the operating system.
Doubly Linked list
• Each node of doubly linked list contains data and
two pointers to point previous (LPTR) and next
(RPTR) node.
Doubly Linked list
• L and R in image denotes left most and right most
nodes in the list.
• Left link of L node and right link of R node is
NULL, indicating the end of list for each direction.
Advantages of Doubly
Linked
• Main advantage of doubly linked list is we can
traverse in any direction, forward or reverse.
Drawback of Doubly Linked
list
• Drawback of doubly linked list is it requires more
memory compared to singly linked list because
we need an extra pointer to point previous node.
• We can delete a node with little trouble , since we
have pointers to the previous and next nodes.
Doubly Circular
Linked List
• Circular Doubly Linked List has properties of both
doubly linked list and circular linked list in which
two consecutive elements are linked or connected
by previous and next pointer and the last node
points to first node by next pointer and also the
first node points to last node by previous pointer.
Advantages and disadvantages
of linked list over array
• Advantages of an array
• We can access any element of an array directly
means random access is easy
• It can be used to create other useful data
structures (queues, stacks)
• It is light on memory usage compared to other
structures
• Disadvantages of an array
• Its size is fixed
• It cannot be dynamically resized in most
languages
• It is hard to add/remove elements
• Size of all elements must be same.
• Rigid structure (Rigid = Inflexible or not
changeable)
Advantages and disadvantages
of linked list over array
• Advantages of Linked List
• Dynamic size
• It is easy to add/remove/change elements
• Elements of linked list are flexible, it can be
primary data type or user defined data types
• Disadvantages of Linked List
• 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.
• It cannot be easily sorted.
• We must traverse 1/2 the list on average to
access any element.
• More complex to create than an array.
• Extra memory space for a pointer is required with
each element of the list.
Linked List Sequential List/
Array
1. In linked list number of 1. in sequential list number of
elements in the list is not fixed. elements in list is fixed.
2. In linked list allocation of 2. In sequential list allocation of
memory is done at runtime. memory is done at compile time.
3. Insertion and deletion 3. Insertion and deletion
operation are very easy and less operation are very lengthy and
time consuming in linked list. time consuming in sequential list
4. In linked list we require 4. In sequential list there is no
pointer variable which occupies need to
extra memory space. use pointer variable so it does
not occupies extra memory
space.
5.Searching in a linked list is 5.Searching is less time
very time consuming because we consuming in
have to traverse entire list even sequential list because we can
if all the elements in the list are use binary search method to
sorted. search an element which is very
efficient
6.In linked list the elements 6. In sequential list elements are
Advantages and disadvantages of stack and
array?
Insertion & Deletion Operation
• Insertion and deletion operations are known as push
and pop operation in stack and as insert and delete
operation in queue.
• In the case of an array, if we have n elements list and
it is required to insert a new element between the first
and second element then n- 1 elements of the list
must be moved so as to make room for the new
element.
• In case of linked list, this can be accomplished by only
interchanging pointers.
• Thus, insertion and deletions are more efficient when
performed in linked list then array.
• Searching a node
• If a particular node in a linked list is required, it is
necessary to follow links from the first node onwards
until the desired node is found.
• Where as in the case of an array, directly we can
access any node.
• Join & Split
• We can join two linked list by assigning pointer of
second linked list in the last node of first linked list.
• Just assign null address in the node from where we
want to split one linked list in two parts.
• Joining and splitting of two arrays is much more difficult
compared to linked list.
• Memory
• The pointers in linked list consume additional
memory compared to an array
• Size
• Array is fixed sized so number of elements will be
limited in stack and queue.
• Size of linked list is dynamic and can be changed
easily so it is flexible in number of elements.
Operations on Linked
list
• Traversing a linked list.
• Insert new node at beginning of the list
• Insert new node at end of the list
• Insert new node into ordered list
• Insert new node at any position or in between the list.
• Delete first node of the list
• Delete last node of the list
• Delete node from any specific position in the list
• Searching element in list.
• Merging of two linked list
• Sorting operation of list
SINGLY LINKED
LIST
beginning
of the list
• Steps:
• Create a Node
• Set the node data Values
• Connect the pointers
o Make link of new node as first
5000
20 5000
5000 40
2000
3000