2. Definition
A linked list is an ordered collection of finite, homogeneous data
elements called nodes where the linear order is maintained by means of
links or pointers.
• Linked list is called a dynamic data structure as the amount of
memory can be varied during use.
• Adjacency between memory elements is maintained by means of links
and nodes.
• A link is nothing but the address of the subsequent element.
• An element in a linked list is referred as Node, which consists of two
fields: DATA and LINK.
DS - Linked List
3. Single Linked List
In a singly linked list, each node contains only one link which points to
the subsequent node in the list.
HEADER is an empty node (data content NULL) and is used to store
a pointer to the first node N1.
Starting from the first node one can reach the last node whose link
field does not contain any address but has a null value.
In a single linked list one can move from left to right only, so its also
called one way list.
DS - Linked List
4. Representations of a Single Linked List
A single linked list can be represented in two ways:
Static representation using Array
Dynamic representation using free pool of storage
DS - Linked List
5. Static Representation
2 arrays are maintained: one for
data, another for links.
2 parallel arrays of equal size
are allocated which should be
sufficient to store the entire list.
Though it contradicts the idea of
linked list but in programming
languages like ALGOL,
FORTRAN, BASIC etc. this is
the only way of representation.
DS - Linked List
7. Dynamic Representation
In this process there’s a memory bank (a collection of memory spaces
available to the programmer) and a memory manager (a program).
During creation of a linked list, when a node is required, the request is
sent to the memory manager.
It then checks the memory bank for the requested block and if its
found then grants the block to the caller.
There’s also a program called garbage collector. Whenever a node is
no more in use it send the unused node back to the memory bank.
This type of memory management is known as Dynamic Memory
Management
DS - Linked List
9. Operation on a Single Linked List
Traversal
Insertion
Deletion
Copy
Merge
Search
DS - Linked List
10. Traversing a Single Linked List
In traversing, we visit every single node in the list starting from the first to
last node.
Algorithm Traverse_SL
Input: HEADER is the pointer to the header node
Output: According to the Process()
Data Structure: A single linked list whose address of the starting node is
known from the HEADER.
DS - Linked List
1. ptr = HEADER → LINK
2. while(ptr=NULL)do
3. Process(ptr)
4. ptr=ptr → LINK
5. EndWhile
6. Stop
11. Inserting a node into a Single Linked List
Various positions are there for a node to be inserted:
At the front (as a first element)
At the end (as a last element)
At any other position
DS - Linked List
12. Insertion…..contd….
Let us assume a procedure GetNode(NODE) to get a pointer of a
memory block which suits the type NODE. The procedure is to
understand how a node can be allocated. In C, C++ and Java, there are
library routines like alloc(), malloc() etc. to do the same.
Procedure GetNode
Input: NODE is the type of data for which a memory has to be allocated
Output: Return a message if the allocation fails else the pointer to the
memory block allocated.
DS - Linked List
13. Procedure GetNode
DS - Linked List
If(AVAIL=NULL)
Return(NULL)
Print "Insufficient memory: Unable to allocate memory"
Else
ptr=AVAIL //Start from location where AVAIL points
While(Sizeof(ptr)!=SizeOf(NODE)) and (ptr->LINK!=NULL) do
ptr1=ptr //to keep track of previous block
ptr=ptr->LINK //move to the next block
EndWhile
If(SizeOf(ptr)=SizeOf(NODE)) //memory block of right size is found
ptr1->LINK=ptr->LINK //update the avail list
Return(ptr)
Else
Print"The memory block is too large to fit"
Return(NULL)
EndIf
EndIf
Stop
14. Insertion at the front
Algorithm InsertFront_SL
Input: HEADER is the pointer to the header node and X is the data of the
node to be inserted
Output: A single linked list with a newly inserted node at the front
Data Structure: A single linked list whose address of the starting node is
known from the header
DS - Linked List
new = GetNode(NODE)
If(new=NULL)then
Print"Memory underflow: No insertion"
Exit
Else
new->LINK=HEADER->LINK //change of pointer1 as in fig.
new->DATA=X //copy the data X to newly availed node
HEADER->LINK=new //change of pointer2 as in fig.
EndIf
Stop
16. Insertion at the end
Algorithm InsertFront_SL
Input: HEADER is the pointer to the header node and X is the data of the
node to be inserted
Output: A single linked list with a newly inserted node at the end
Data Structure: A single linked list whose address of the starting node is
known from the header
DS - Linked List
new = GetNode(NODE)
If(new=NULL)then
Print"Memory is insufficient: insertion not possible"
Exit
Else
ptr=HEADER
While(ptr->LINK!=NULL)do
ptr=ptr->LINK
EndWhile
ptr->LINK=new
new->DATA=X
EndIf
Stop
18. Insertion at any position
Algorithm InsertAny_SL
Input: HEADER is the pointer to the header node and X is the data of the
node to be inserted
Output: A single linked list with a newly inserted node having data X after
the node with data KEY
Data Structure: A single linked list whose address of the starting node is
known from the header
DS - Linked List
19. Insertion at any position……contd…
DS - Linked List
new = GetNode(NODE)
If(new=NULL)then
Print"Memory is insufficient: insertion not possible"
Exit
Else
ptr=HEADER
While(ptr.DATA!=KEY) and (ptr.LINK!=NULL)do
ptr->LINK
EndWhile
If(ptr.LINK=NULL)then
Print"KEY is not available in the list"
Exit
Else
new->LINK=ptr->LINK
new->DATA=X
ptr->LINK=new
EndIf
EndIf
Stop
20. Deletion of a node from a Single Linked List
Like Insertion operation, Deletion operation can also be executed
different positions:
At the front of the list
At the end of the list
At any position
Let us consider a procedure ReturnNode(ptr) which returns a node
having pointer ptr to the free pool of storage.
DS - Linked List
21. Procedure ReturnNode(ptr)
DS - Linked List
new = GetNode(NODE)
If(new=NULL)then
Print"Memory is insufficient: insertion not possible"
Exit
Else
ptr=HEADER
While(ptr.DATA!=KEY) and (ptr.LINK!=NULL)do
ptr->LINK
EndWhile
If(ptr.LINK=NULL)then
Print"KEY is not available in the list"
Exit
Else
new->LINK=ptr->LINK
new->DATA=X
ptr->LINK=new
EndIf
EndIf
Stop
22. Deletion from the front of list
Algortihm DeleteFront_SL
Input- HEADER is the pointer to the header node
Output: A single linked list eliminating the node at the front
Data Structure: A single linked list whose address of the starting node is
known from the header.
DS - Linked List
24. Deletion from the end of list
Algortihm DeleteEnd_SL
Input: HEADER is the pointer to the header node
Output: A single linked list eliminating the node at the end of list
Data Structure: A single linked list whose address of the starting node is
known from the header.
DS - Linked List
26. Deletion from any position in the list
Algortihm DeleteAny_SL
Input: HEADER is the pointer to the header node
Output: A single linked list eliminating the node at the end of list
Data Structure: A single linked list whose address of the starting node is
known from the header.
DS - Linked List
28. Copying a Single Linked List
Algortihm Copy_SL
Input: HEADER is the pointer to the header node
Output: HEADER1 is the pointer to the duplicate list
Data Structure: Single linked list structure
DS - Linked List
29. Merging two linked lists into one
Algortihm Merge_SL
Input: HEADER1 and HEADER are the pointers to the header nodes
Output: HEADER is the pointer to the resultant list
Data Structure: Single linked list structure
DS - Linked List
30. Merging two linked lists into one
DS - Linked List
ptr = HEADER1
While(ptr->LINK!=NULL)do
ptr=ptr->LINK
EndWhile
ptr->LINK=HEADER2->LINK
ReturnNode(HEADER2)
HEADER=HEADER1
Stop
31. Searching for an element in a Singly Linked List
Algortihm Search_SL
Input: KEY, the item to be searched
Output: LOCATION, the pointer to a node where the KEY belongs to or
an error message
Data Structure: Single linked list structure
DS - Linked List
32. Circular Linked List
A linked list where the last node points the header node is called the
Circular Linked List.
In a Circular Linked List, every member node is accessible from any
node by chaining through the list.
DS - Linked List
33. Searching an element in Circular Linked List
Algortihm Search_CL
Input: KEY, the item to be searched
Output: LOCATION, the pointer to a node where the KEY belongs to or
an error message
Data Structure: A circular linked list
DS - Linked List
34. Merging of two Circular Linked Lists
Algortihm Merge_CL
Input: HEADER1 and HEADER are the pointers to the header nodes
Output: A large circular linked list containing all the nodes from
HEADER1 and HEADER2
Data Structure: Circular linked list structure
DS - Linked List
36. Double Linked List
A Doubly Linked List is a two way list as, one can move either from
left to right or from right to left.
The two-way process is maintained using two link fields instead of one
as in a single linked list.
DS – Linked List
37. Inserting a node in the front a Doubly Linked List
Algorithm InsertFront_DL
Input: X is the data content of the node to be inserted
Output: A double linked list enriched with a node in the front
containing data X
Data Structure: Double linked list whose pointer to the
header node is HEADER
DS – Linked List
38. Inserting a node in the front of a Doubly Linked List
DS – Linked List
40. Inserting a node at the end of a Doubly Linked List
Algorithm InsertEnd_DL
Input: X is the data content of the node to be inserted
Output: A double linked list enriched with a node at the end containing
data X
Data Structure: Double linked list whose pointer to the header node is
HEADER
DS – Linked List
41. Inserting a node at the end a Doubly Linked List
DS – Linked List
42. Inserting a node at any point of a Doubly Linked List
Algorithm InsertAny_DL
Input: X is the data content of the node to be inserted, and KEY the data
content of the node after which the new node is to be inserted.
Output: A double linked list enriched with a node containing data X after
the node with data KEY, if KEY is not present in the list then it is
inserted at the end.
Data Structure: Double linked list whose pointer to the header node is
HEADER
DS – Linked List
43. Inserting a node at any point of a Doubly Linked List
DS – Linked List
44. Deleting a node from the front of a Doubly Linked List
DS – Linked List
Algorithm DeleteFront_DL
Input: A doubly linked list with data
Output: A reduced doubly linked list
Data Structure: Double linked list whose pointer to the header node is
HEADER
45. Deleting a node from the end of a Doubly Linked List
Algorithm DeleteEnd_DL
Input: A doubly linked list with data
Output: A reduced doubly linked list
Data Structure: Double linked list whose pointer to the header node is
HEADER
DS – Linked List
46. Deleting a node from any position in a Doubly Linked List
Algorithm DeleteAny_DL
Input: X is the data content of the node to be inserted, and KEY the data
content of the node to be deleted.
Output: A double linked list without a node having data content KEY, if
any.
Data Structure: Doubly linked list whose pointer to the header node is
HEADER
DS – Linked List
47. Deleting a node from any position in a Doubly Linked List
DS – Linked List