You are on page 1of 29

Linked Lists_

Insertion
Dr. Muhammad Kamran Khan
Assistant Professor

Department of Electrical Engineering


Military College of Signals
National University of Sciences and Technology (NUST)
Insert-in-between
• Insert a node in between the two nodes
• Two temporary node type variables are
required
• A new node is to be created
• Links are to be updated
• The method takes 2 arguments
– Position at which the new node is to be inserted
– Data for the new node
Required Steps
• Validate the given position
• Assume the two Nodes P and Q
P Q

• And a new Node n


n

• The requirement is to insert ‘n’ in between ‘P’


and ‘Q’
Required Steps
n

P Q

1. Create a link between n and Q (n.next=P.next)


2. Update the link of P by changing its next (P.next=n)
Final Shape
n

P Q
inbetinsert(self,pos,d)
head
def inbetinsert(self,pos=3,d):
30 2000 20 1000 10 1550 50
if pos>self.count_nodes(): 500 2000 1000 1550

print(“invalid Pos”)
return

temp=Node() # for traversal


temp=self.head
c=1
while(c<pos-1):
temp=temp.next
c=c+1
n = Node(d) # a new node to be inserted
n.next = temp.next
temp.next = n
Function call Example
inbetinsert(3,45)
if pos > self.count_nodes():
print(“invalid position”)
return

head

30 2000
500
20 1000
2000

10 1550
1000

50 None
1550
temp=Node() # for traversal
temp=self.head
c=1
while(c<pos-1):
temp=temp.next
c=c+1
n=Node(d) # a new node
n.next=temp.next
temp.next=n
head temp
c=1
n
45 1000
30 2000 temp
c=2 1007
500
20 1000
2000

10 1550
1000

50 None
1550
Node_Search(self,d)
def node_search(self,d):
if self.isEmpty()==True:
return False
temp=Node()
temp=self.head
c=0
while(temp!=None):
if temp.data==d:
c=c+1
temp=temp.next
if c==0:
return False
else:
return True
insert_after(node_data, d)
• node_data------after which the new node is to
be inserted
• d------ the data for the new node
• Find whether the targeted node is present or
not
• If present then traverse the list to that
particular node and insert the newly created
node after it
insert_after(node_data, d)
def insert_after(node_data,d):
if self.search(node_data)==False:
print(“Node not Found”)
return
temp=Node()
temp=self.head
while temp.data!=node_data:
temp=temp.next
n=Node(d)
n.next=temp.next
temp.next=n
Example:Function call
insert_after(45,67)
head

30 2000
Existing List
500
20 1007
2000

45 1000
1007

10 1550
1000

50 None
1550
insert_after(45,67)
Validate the node with value 45
head
Search the Node having the data 45
The function will return True
30 2000
500
20 1007
2000

45 1000
1007

10 1550
1000

50 None
1550
insert_after(45,67)
temp=Node()
temp=self.head
while temp.data!=node_data:
head temp temp=temp.next
n=Node(d)
n.next=temp.next
30 2000 temp.next=n
temp
500
20 1007
temp
2000 n
67 1000
45 2340
1000
2340
1007

10 1550
1000

50 None
1550
Final List
head

30 2000
500
20 1007
2000

45 2340
1007
67 1000
2340
10 1550
1000

50 None
1550
Linked List
Deletion
del_head(self)

head temp
Now delete this Node

30 2000
500
20 1007
2000

45 2340
1007
67 1000
2340
10 1550
1000

50 None
1550
Updated List

head

20 1007
200
0 45 2340
100
7 67 1000
234
0
10 1550
100
0
50 None
1550
del_head()
def del_head(self):
temp=Node() Head=None

temp=self.head
self.head=self.head.next
temp.next=None
del temphead
Time Cost ??
40
del_last(self) : Steps
• List must not be empty
• Identify the 2nd last and the last node
• The next of 2nd last node must point to NONE
• Delete the last node
del_last(self)
Curr
head temp
Curr
temp

20 1007 Curr
2000 temp
45 2340 Curr
1007 temp
67 1000
2340 temp
10 1550
1000
50 None
1550
del_last()
def del_last(self):
temp=self.head
cur=self.head
while(temp.next!=None):
cur=temp
temp=temp.next
cur.next=None
temp.next=None
del temp
temp=self.head
del_last(self) cur=self.head
while(temp.next!=None):
cur=temp
temp=temp.next
temp.next=None
head temp cur cur.next=None
del temp
cur temp
20 1007
cur temp
2000
45 2340
1007 cur temp
67 1000
2340
temp
10 1550
1000
50 None
Time Cost ?? 1550
O(n)
Updated List

head

20 1007
2000
45 2340
1007
67 1000
2340
10
1000
del_node(self,pos)
• List must not be empty
• Position must a valid one
• Two temporary Node() type variables are
required
– One for the node to be deleted
– One to update the links
Example

head

temp
20 1007
200 temp2
0 45 1000
2340
100
7 67 1000
2340
10
1000
del_node(self, pos)
def del_node(self, pos): temp.next=temp2.next
if pos>self.count(): temp2.next=None
return del temp2
if pos==1:
self.del_head()
return
elif pos==self.count():
self.del_last()
return
temp=self.head
temp2=self.head
i=1
while i<pos:
temp=temp2
temp2=temp2.next
i=i+1
Function call: temp=self.head
temp2=self.head
del_node(3) i=1
while i<pos:
temp=temp2
temp2=temp2.next
head i=i+1
temp temp2
i=1 temp.next=temp2.next
temp2.next=None
temp temp2 del temp2
20 1007 i=2
2000 temp2
45 1000
2340 i=3
1007
67 1000
2340
10
Temp=2nd last node 1000
Temp2= last node (next=None)
del_node(self,d)
• Self practice

You might also like