Professional Documents
Culture Documents
Linked List PDF
Linked List PDF
Data Structure
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
Single Linked List
In a singly linked list, each node contains only one link which points to
the subsequent node in the list.
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
Representations of a Single Linked List
DS - Linked List
Static Representation
2 arrays are maintained: one for
data, another for links.
DS - Linked List
Static Representation
DS - Linked List
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).
It then checks the memory bank for the requested block and if its
found then grants the block to the caller.
DS - Linked List
Dynamic Representation
DS - Linked List
Operation on a Single Linked List
Traversal
Insertion
Deletion
Copy
Merge
Search
DS - Linked List
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
Inserting a node into a Single Linked List
DS - Linked List
Insertion…..contd….
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
Procedure GetNode
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
DS - Linked List
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
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
DS - Linked List
Insertion……contd…
DS - Linked List
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
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
DS - Linked List
Insertion at the end……contd…
DS - Linked List
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
Insertion at any position……contd…
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
DS - Linked List
Deletion of a node from a Single Linked List
Like Insertion operation, Deletion operation can also be executed
different positions:
At any position
DS - Linked List
Procedure ReturnNode(ptr)
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
DS - Linked List
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
Deletion from the front of list
DS - Linked List
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
Deletion from the end of list
DS - Linked List
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
Deletion from any position in the list
DS - Linked List
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
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
Merging two linked lists into one
ptr = HEADER1
While(ptr->LINK!=NULL)do
ptr=ptr->LINK
EndWhile
ptr->LINK=HEADER2->LINK
ReturnNode(HEADER2)
HEADER=HEADER1
Stop
DS - Linked List
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
Circular Linked List
A linked list where the last node points the header node is called the
Circular Linked List.
DS - Linked List
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
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
Merging of two Circular Linked Lists
DS - Linked List
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
Inserting a node in the front a Doubly Linked List
Algorithm InsertFront_DL
DS – Linked List
Inserting a node in the front of a Doubly Linked List
DS – Linked List
Inserting a node in a Doubly Linked List
DS – Linked List
Inserting a node at the end of a Doubly Linked List
Algorithm InsertEnd_DL
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
Inserting a node at the end a Doubly Linked List
DS – Linked List
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
Inserting a node at any point of a Doubly Linked List
DS – Linked List
Deleting a node from the front of a Doubly 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
DS – Linked List
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
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
Deleting a node from any position in a Doubly Linked List
DS – Linked List
Image
DS – Linked List
THANK YOU
If any doubt contact @9038540603
Or
Email me: kaustavr25@gmail.com