DSSlides M3
DSSlides M3
MODULE – III
LINKED LISTS
Introduction to Linked List
Null pointer
The link field of the last node contain NULL(in c program)
None(in python) rather than a valid address. It indicates end of
the list.
Key Terms
External pointer:
It is a pointer to the very first node in the
linked list, it enables us to access the entire
linked list.
Empty list:
If the nodes are not present in a lined list, then
it is called an empty linked list also called null
list.
A linked list can be made an empty list by
assigning a NULL value to the external pointer.
start= NULL(None)
Operations on Linked List
1. Creation
2. Insertion
➢ At the beginning of a linked list
➢ At the end of a linked list
➢ At the specified position in a linked list
3. Deletion
➢ Beginning of a linked list
➢ End of a linked list
➢ Specified position of a linked list
4. Traversing
5. Searching
6. Concatenation
7. Display
Operations on Linked List
➢ Creation:
This operation is used to create a linked list. Here, the node is
created as and when it is required and linked to the list to
preserve the integrity of the list.
➢ Insertion:
This operation is used to insert a new node in the linked list at
the specified position. A new node may be inserted
1. At the beginning of a linked list
2. At the end of a linked list
3. At the specified position in a linked list
4. If the list itself empty, then new node is inserted as a
first node.
Operations on Linked List
➢ Deletion:
This operation is used to delete a node from the linked
list. A node may be deleted from the
1. Beginning of a linked list
2. End of a linked list
3. Specified position of a linked list.
➢ Traversing:
• It is processing of going through all the nodes of a
linked list from one end to the other end.
• If we start traversing from the very first node towards
the last node, it is called forward traversing.
• If the desired element is found, signal operation is
SUCCESSFUL otherwise, signal as
UNSUCCESSFULL.
Operations on Linked List
➢ Concatenation:
It is process of appending the second list to the end of
first list consisting of m nodes. When we concatenate two lists,
the second list has n nodes, then the concatenated list will be
having (m+n) nodes.
➢ Display:
This operation is used to print each and every node’s
information. We access each node from the beginning (or
specified position) of the last node.
Single Linked List
1000
Single Linked List
New_node =node(20)
self.head.link=new_node
head
1000 2000 None
1000 2000
Inserting a node at the beginning
def insert_begin(self,data):
nb = Node(data)
nb.next = self.head
self.head = nb
Insert a node at the beginning of Linked List
Inserting a node at the end
def insert_end(self,data):
new_node=node(data)
if self.head is None:
self.start=new_node
return
temp=self.head
while temp.next is not None:
temp=temp.next
temp.next=new_node
Function to Insert a node at the end
Algorithm to Insert a node at given position
Algorithm inserts an item at the specified position in the linked
list.
Step 1.if position is equal to 1
create a node and insert data in to the node
return
Step 2. pos = p
temp = sel.head
for i in range(pos-1):
move the pointer to the next node i.e., temp =
temp.next
if pointer is None:
print position is not present in the list
else:
create a node i.e., np =node(data)
np.next = temp.next
temp.next= np
Function to Insert a node at given position
def insert_pos(self,pos,data):
np = Node(data)
temp = self.head
if post = 0:
np.next = self.head
self.head = np
return
else:
for i in range(pos-1):
temp = temp.next
if temp.next is None:
print("Position not availble in the list")
else:
np.next = temp.next
temp.next = np
Insert a node at given position
Deletion of a node
def delete_beg(self):
if self.head is None:
print("List is empty")
else:
temp = self.head
self.head = temp.next
temp.next = None
Delete a node at the beginning
Algorithm to delete a node at the end of list
➢ The following steps are followed to delete a node at
the end of the list:
– If list is empty then display ‗Empty List‘message.
– If the list is not empty, follow the steps given
below: