Linked list implementation codes explanation
Linked list implementation codes explanation

Linked list implementation codes explanation

Howdy readers? I have already publish a post regarding singly liked list. There we discussed the implementation codes and how linked list works. Although we couldn’t look at the codes meaning there. I promised you to I will publish another post by explaining the code. So now I bring you that post. I hope until now you have read our linked list post. Then you know how singly linked list works. If you haven’t read that I suggest you to read it first. Here is the link for that. Singly linked list. So today we will discuss about  linked list implementation codes explanation.

In our previous post we have discussed the operations in the linked list. The code segment related to those operations today we will discuss. Let’s get started.

Initializing the linked list

Linked list initialization will be happen when creating the linked list through an object. For that constructors are using. In the constructor we assign both head and tail as null. Because initially both head and tail don’t contain any value.

public void List(){
head=tail=null;
}

Add to head

In add to head we have to give node value. Therefore we accept character (char) value inside method parameters. We can only accept one parameter here because when adding to head the node will connect after head. When adding to head we can’t insert a node after another node. If we want to do that we have to use add operation. After that head will replace with new node address. When the adding time tail is null tail also will take the head value.

public void addTohead(char el){
head=new Node(el,head);
if(tail==null)
tail=head;
}

Add to tail

This is also same as add to head. When tail is null means there is no nodes in the list. Therefore the new node address will take by both head and tail. If not that means there are nodes in the list. If so new node will added with the element and current tail.next value should take the new node address value. After that tail node value will replace with current tail.next value. Then a new tail will create.

public void addTotail(char el){
if(tail==null)
head=tail=new Node(el);
else{
tail.next=new Node(el);
tail=tail.next;
}
}

Delete from head

When deleting from head we have to remove current head and connect head node with current head.next node. For that we firstly should retrieve head value. Then assign it to variable. We do this for identification purpose. If head and tail values are same that means there is only one node in the list. After remove that node both head and tail value should be null. If not the scenario I explained earlier should happen for that head value will replace with head.next value. Current head.next value is the next node address after the head. We can return now el variable at the end.

public char deleteFromHead(){
char el=head.info;
if(head==tail)
head=tail=null;
else
head=head.next;
return el;
}

Delete from tail

Previous post I have explained you to delete from tail we have to find the node before the tail. Why is that? Because when current tail removed, the node before to the current tail will be the new tail. Therefore before deletion we have to find that node. For that we use temp node. Yet we don’t face this issue if there is only one node in the linked list. So this temp node used for the else scenario. If both head and tail is same we can simply make them both null and complete the deletion. If not we have to do a search in the linked list. So we use for loop. In for loop first we give the initialization. Means from where to start. Then we give loop stopping condition. Until when this loop should go. Third we give the increment value.

Here we start the loop with assign temp value with head value. We have to run this until we meet node before the current tail. The next value will give the next node address. The tail address and tail node value is same. Temp always refer the node. So when temp.next = tail means temp is the node before the current tail node which we have looking for. Until then we assign temp value with temp.next value. That is the increment clause. When we found it and loop execution ends temp address will assign as tail. Then tail.next value will null. Because now there is no nodes after the tail node.

public char deleteFromTail(){
char el=tail.info;
if(head==tail)
head=tail=null;
else{
for(temp=head;temp.next!tail;temp=temp.next);
tail=temp;
tail.next=null;
}
return el;
}

Conclusion

So for now I am concluding linked list implementation codes explanation post. If you like this post please don’t forget to comment. You can share this post with friends also. If you are interested more about linked list please check our linked list post and linked list insert and delete options explained post. We will meet again with another interesting post. Till then goodbye!

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.

Leave a Reply