Circulat CIment
4. as next and the first element contains link of
the last element as previous.
1. Singly Linked list
In asingly linked list, each node has a data element and a single link to the
next node in the sequence. The last node's link points to NULL, indicating the end
of the list.
Data Next Data Next Data Next NULL
HEAD
Suppose we have three nodes, and the addresses of these four nodes are 100.
Z00, 300 and 400 respectively. The representation of three nodes as a linked list is
shown in the below figure:
Linear Structures 2.17
100
start
10 200 20 300 30 40 X
400
100 200 300 400
The start
pointer holds Each node stores Stores the next
the address the data node address The next field of the
of the first node last node is NULLL
of the list
The singly linked list contains only single link. Only forward traversal is possible
and backward traversal is not possible, as it has only one link.
Python classes & constructors to create a new node
class Node:
definit _(self, data):
self.data = data
self.next = None
Simple Python program to create singly linked list
class Node:
definit _(self, data):
self.data = data
self.next = None
# Create the nodes
nodel = Node(l)
node2 = Node(2)
node3 = Node(3)
# Link the nodes together
node1.next = node2
node2.next = node3
# Accessing the linked list
head = nodel # Set the head node
# Traverse the linked list
Current head
2.18 Data Structures and
Algorithm
while Current is not None:
print(current.data)
Current = current.next
Output
1
2
3
Basic Operations on Singly
Linked List
Basic operations on a
deletion, searching, and singly linked list involve common tasks such
traversal. as insertion.
(a) Insertion
Insertion at the Beginning
In this case, the
the new head. new node is added
The next pointer at the beginning
the head pointer is of the new node of the linked list,
updated to the new node. will point to the current becoming
head, and
Example 1:
head
data next
newNode
Linear Structures 2.19
Example 2:
head
MSP ATL BOS
(a)
newest head
LAX MSP ATL
newest head
(b)
LAX MSP
Python code to insert node at the Beginning
def insert_at_beginning(self, data):
new _node = Node(data)
new_node.next = self.head
self.head = new_node
Insertion at the End (Append)
This case involves adding a new node at the end of the linked list.
If the list is emnpty, the new node becomnes the head.
Otherwise, we traverse the list until we reach the last node and set its next
pointer to the new node. The new node's next pointer is set to None.
head
Example 1:
10 NULL
data next
newNode
Data Structures and Algorithrn
220
Example 2:
tail
BOS
(a)
tail newest
MSP BOS MIA
(b)
tail newest
BOS MIA
(c)
Python code to insert node at the
End
def insert _at_end(self, data):
new_node = Node( data)
if self.head is None:
"self.head = new_node
else:
Current =selfhead
while Current. next
is not None:
Current = current.next
Current.next = new node
Linear Structures 2.21
Jnsertion after a Given Node
In this case, we want to insert a new node after a specific node in the linked
jst. We locate the given node, create the new node, and adjust the pointers to include
list.
the new node in the
Example 1:
head
prevNode
data next
newÑode
Python code to insert node after a Given Node
def insert _after _node(self, prev_node, data):
if prev_node is None:
print("Previous node is not found.")
return
new_node = Node(data)
new_node.next = prev_node.next
prev_node.next = new_node
(b) Deletion
Deletion at the Beginning
of the linked list. To delete the
In this case, the node to be deleted is the head
and then deallocate the
ode, update the head pointer to point to the next node,
nemory of the original head.