Unit 4 Ds Link List t221
Unit 4 Ds Link List t221
Linked List
Linear Data Structure
A linked List
▪ The linked allocation method of storage can result in both
efficient use of computer storage and computer time.
• A linked list is a non-sequential collection of data items.
• Each node is divided into two parts, the first part represents the
information of the element and the second part contains the
address of the next mode.
• The last node of the list does not have successor node, so null
value is stored as the address.
• It is possible for a list to have no nodes at all, such a list is called
empty list.
Linked List - Linear Data Structure 4
Linked List Representation:
X 1000
2100
Typical node of L R
Doubly Linked List Doubly linked linear list
5 10 15 20 25 30
FIRST
Linked List - Linear Data Structure 14
Circular Singly Linked Linear List
▪ Disadvantages of Circular List
• It is not easy to reverse the linked list
• If proper care is not taken, then the problem of infinite loop can
occur
• If we at a node and go back to the previous node, then we can
not do it in single step. Instead we have to complete the entire
circle by going through the in between nodes and then we will
reach the required node
Node
of Node
Node Info (Node)
Link (Node)
Data Pointer to
Next Node
Python
Structure
to represent
a node
Linked List - Linear Data Structure 18
Algorithms for singly linked list
Insertion of a new node in the beginning of the list,
Insertion of a new node at the end of the list,
Insertion of a new node after a given node,
Insertion of a new node before a given node,
Insertion of a new node in sorted linked list,
Deleting the first and last node from a linked list,
Searching a node in Linked List,
Count the number of nodes in linked list
FIRST
50 link
NEW
FIRST
NEW_NODE.LINK = self.FIRST
self.FIRST = NEW_NODE
50
SAVE
NEW
10 50 -1 8 35 44
FIRST
Linked List - Linear Data Structure 28
Function: INSEND(X, First) Cont…
SAVE = FIRST
Repeat while X ≠ SAVE->INFO and SAVE->LINK ≠ NULL
PRED = SAVE
SAVE = SAVE->LINK
If X = SAVE->INFO then
NEW_NODE->LINK= SAVE->LINK
SAVE->LINK=NEW_NODE
Linked List - Linear Data Structure 32
Function: INSAFG(X, First) Cont…
self.SAVE=self.FIRST
Repeat while self.SAVE.LINK ≠ None
self.PREV=self.SAVE
self.SAVE=self.SAVE.LINK
Return self.SAVE.INFO
self.PREV.LINK=None
5 10 15 20 25 30
FIRST
Linked List - Linear Data Structure 51
Circularly Linked Linear List
▪ Disadvantages of Circular List
• It is not easy to reverse the linked list
• If proper care is not taken, then the problem of infinite loop can
occur
• If we at a node and go back to the previous node, then we can
not do it in single step. Instead we have to complete the entire
circle by going through the in between nodes and then we will
reach the required node
Typical node of L R
Doubly Linked List Doubly linked linear list