Linked list Insert and delete
Linked list Insert and delete

Linked list Insert and delete

Hi dear readers. I have published a post related to linked list earlier. There we discuss about singly linked list. In that we discuss about how to implement a singly linked list and how linked list operates. Yet in that post we couldn’t discuss about singly linked list insert and delete. Today from this post we are going to discuss about that. If you couldn’t read the singly linked post you can read it.

Post Outline

  • Linked list insert operation
    • Code for the linked list insert operation
    • Add or Insert linked list code explanation
  • Delete operation in linked list
    • Code for the linked list delete operation
    • Linked list delete operation code explanation
  • Quick Rap

Linked list insert operation

Here we are going to discuss insert a node to linked list. Up to now we know about add to head and tail options. Here we are trying to add a node after any of node in the linked list. For that when we adding a new node next values should change. The next value of the node which is our new node will connect should take new node address. Our new node next value should be the next nodes address. By using add option we can add a node to anywhere in the linked list. In that but the nodes will add according to the value we will add. For adding a new node at the adding point current linked list must break and add the new node. As example,

There is a linked list with five nodes. 99,100,101,102,103. The values are A,B,C,E,F. We are adding 200 node with value D. Then 200add after 101 node. Because D comes after C.101 node’s current next value = 102

200 node’s current next value = null

101 node’s new next value = 200

200 node’s new next value = 102

 

Insert node to singly linked list
Insert node to singly linked list

Code for the linked list insert operation

public void insert(char el){
if(head==null)
head=tail=new Node(el);
else
if(el<head.info)
head = new Node(el,head);
else
Node temp,pred;
for(pred=head,temp=head.next;temp!=null&&el<temp.info;pred=pred.next,temp=temp.next);
if(temp!=null)
pred.next=new Node(el,temp);
else
tail.next= new Node(el);
tail=tail.next;
}

 

Add or Insert linked list code explanation

If the both head and tail is null new node will added and head and tail node will take the added node address. When adding we can give the value through char el. If head and tail value is not same means there are some nodes in the linked list then it will find where to add this node. It will find it through the value we insert. Firstly it compares the node’s element with head info value. If it is less adding node will be the new head.

Current head will next node after the head. If the element is greater than the head info then it find where should it put. For that like in the deletion process extra nodes will use. In the deletion we use one node call temp. But in here we use two nodes call pred and temp. Pred will take the head value. Temp will take head.next value. For above example,

Pred = 99

Temp =100

Node value = D

1 st comparison = B>D =False

New pred =100

New temp = 101

2 nd Comparison = C>D = false

New pred = 101

New temp = 102

3 rd comparison = E>D = true

Loop will end this point since condition met. El > head.info condition met. If temp is not null means there are more nodes in the linked list. So the new node won’t be the tail node. Which means a highest value not in the current linked list. So it should add after the pred node. 101 node contains value C. So pred.next = new node address. New node next value will be temp. That’s why we use two nodes here. We need to find before and after nodes. So we have to use two nodes to find it.

Delete operation in linked list

When deleting a node a node will removed from the linked list. After removing the node next values need to be updated. The previous node next value should take the address of the after node to the deleting node. As an example.

There are five nodes in a linked list. 99,100,101,102,103. If we are deleting 101 node then,

Deleting node = 101

100 node’s next value = 101

100 node’s next value after deletion = 102

Linked list after deletion = 99,100,102,103

Delete in singly linked list
Delete in singly linked list

Code for the linked list delete operation

public char delete(char el){
if(head==tail && el==head.info)
head=tail=null;
else
if(el==head.info)
head=head.next;
else
Node temp,pred;
for(pred=head,temp=head.next;temp!=null&&temp.info!=el;pred=pred.next,temp=temp.next);
if(temp!=null)
pred.next=temp.next;
if(temp==tail)
tail=pred;
return el;
}

 

Linked list delete operation code explanation

This is also same as other deletion. We can delete a node from anywhere. Firstly we check whether both head and tail values are same and head.info equals to the deleting element. If so tail and head values are same means there are no any nodes in the list. Only one node is there. Therefore delete that node and make head and tail values null.

If there are many nodes and if we are deleting the head node. Then it will check whether element is equal to the head.info. That equality means we told to delete the head node. Then current head will removed and head.next node will be new head. So head node value will take previous head.next node’s address.

If we delete from delete like insert we need to find what are beside nodes. So that like insertion we have to use two extra special nodes call pred and temp. Pred will give the previous node and temp will give the after node. Let’s see an example.

If there are five nodes 99,100,101,102,103. Values of the nodes are A,B,C,D,E. If we want to delete 101 node. 101 node contains element C.

Pred = head = 99

Temp = head.next = 100

1 st comparision = B=C = false

New pred = 100

New temp = 101

2 nd comparison = C=C = true

Now exit from the loop since condition met.

Pred.next will take the temp.next vale. That means 100 node’s next value= 102. Because now 102 node is after 100 node. There is not 101 node it was deleted

Quick Rap

  • We use two extra nodes for singly list insert and delete operations.
  • They are pred and temp.
  • We are using them to identify previous and after nodes.
  • We use self for loop for  insert and delete operations.
  • In doubly linked list we don’t need two nodes for this operations.

Conclusion

From here I am concluding linked list insert and delete operation post. If you like this please comment and let us know your feedback. We will meet again with another interesting topic. Until goodbye all.

Audy Ranathunga

Audy Ranathunga, Author of myexamnote is our most experienced author. She has been working as a blog post writer for 4 years. She joined with myexamnote before 1 year ago and she has contribute lots of valuable posts for readers.

This Post Has 2 Comments

  1. Avatar
    Himanshu

    Well explained. I like so much the way you explained. Very simple to understand.

    1. Audy Ranathunga
      Audy Ranathunga

      Thank you Himanshu.

Leave a Reply