Singly Linked list using java
Hello folks! this blog is going to smash the link linked list topic and i assure you that after reading this fully you will definitely learn the linked list fully.
Let’s dive into the topic
Linked list is approached as an alternative to array. The disadvantage of arrays is that they occupy memory statically. Example : whenever you are assigning memory of array as Array[100] this programmatically allocates 100*4 bytes of memory. So what if we require only 20*4 bytes or n*4 bytes of memory. How can we do it? So, linked list comes into play
Last node points to null object
Here head points to Node and each node has two components → One consisting of data and another which is pointing to nextnode
Step 1 : create a file named Main.java
public class Main
{
public static class Node{
int data;
Node next;
}
public static void main(String args[])
{
Node newnode1=new Node();
Node newnode2=new Node();
Node newnode3=new Node();
Node newnode4=new Node();
newnode1.data=1;
newnode2.data=2;
newnode3.data=3;
newnode4.data=4;
newnode1.next=newnode2;
newnode2.next=newnode3;
newnode3.next=newnode4;
newnode4.next=null;
for (int i=1;i<=4;i++){
System.out.print(newnode1.data+” “);
newnode1=newnode1.next;
}
}
}
This is simple structure of linked list in java, we create a class called Node and then pass data and Node itself as parameters.
Here node next has address of next node and so until the value is null
Now how to do this for n number of values to be assigned?
The code only have to change in main part, rest all are same
Our code is little bit messy right?
Let’s have an object to insert element
Algorithm :
create dummy node and assign data to it
if head is null then newnode points to null and head points to newnode
else newnode next is head and head is newnode
Diagrammatic explanation :
Here we are inserting at beginning.
Code →
import java.util.*;
public class Main
{
public static class Node
{
int data;
Node next;
}
public static Node head;
}
public static void newnodes(int data){
Node newnode = new Node();
newnode.data = data;
if(head == null){
newnode.next = null;
head = newnode;
}
else{
newnode.next = head;
head = newnode;
}
}
public static void main(String args[])
{
newnodes(2);
newnodes(8);
}
}
Now how to insert the data at the end?
Inserting data at end of linked list
Algorithm :
Assign newnode and data
if head is null same as insert at beggining else,
Assign a dummy Node which is equal to head
and loop till dummy of next is null and change the dummy to pointer of dummy (ie next node)
set newnode pointer to null
and dummy of next is pointing towards newnode
Diagrammatic Explanation :
Code :
public static void endnode(int data){
Node newnode = new Node();
newnode.data = data;
if(head == null){
newnode.next = null;
head = newnode;
}
else{
Node last = head;
while(last.next != null){
last = last.next;
}
newnode.next = null;
last.next = newnode;
}
}
Now, what if we want to insert at any position? How can it be done?
Before that we want you to think for a minute and go to next section
Insert at any position
Algorithm:
Set new node and assign data to it
Set dummy node
for loop till pos and dummy = dummy.next
set newnode of next to dummy next and dummy next to newnode
Diagrammatic Explanation :
Code :
public static void atanypos(int val,int data){
Node newnode = new Node();
newnode.data = data;
Node temp = head;
for(int i=0;pos-1>i;i++){
temp = temp.next;
}
newnode.next = temp.next;
temp.next = newnode;
}
Now what if position entered is beyond the length of linked list?
How will you overcome it?
Answer : simply check if temp.next is not null and print invalid
This will print the invalid statement and error? Now how to avoid error?
temp.next goes in advance to one node, so inorder to avoid it, use temp. If you have identified this error beforehand or taught of some mismatch then you are going in a right track, else it’s not a big task buddy, understand the code.
It’s definitely somewhat difficult for you, but the difficulty is because you haven’t seen the output, once if you see the output then your feeling will climb up the mountain!
How to show the elements of linkedlist?
This is a task for you!
I will give you algo but you have to get the answer
Algorithm :
Assign dummy Node and make it equal to head( Node dummy = head)
while dummy is not null print dummy data and make dummy equal to dummy next.
Hurray! Only two lines of algo. And if you could solve this, it’s really great, give a pat on your back.
If you are unable to get the expected result then i have provide the link for github repo at the end of this blog, check there!
You have passed all the elementary tasks of linked list
Topics you have learn’t
→Create linked list node
→Insert at beginning, end and at any position
→Traversal of linked list
Topics to learn
→Deletion of element from linked list
→Other types of linked list (this topic is not covered in this blog)
- Doubly linked list (Link)
Deletion of elements from linked list :
Now deletion is last topic to see, if you have confidence then please test it!
How can be the algorithm for it?
Ok, it should get value to be deleted/position to be deleted?
I’m gonna try with position and you try with value
Assign dummy node and equal to head
for loop till the position and accordingly change ???
now for this we require current node and next node
temp is equal to dummy and dummy is equal to dummy next
temp of next is equal to dummy next
Code :
public static void delete(int position){
Node temp = head;
Node prev = new Node();
for(int i=0;position>i;i++){
prev = temp;
temp = temp.next;
}
prev.next = temp.next;
}
Its really not that we have leant everything about linked list, we have just now leant how to operate linked list, it’s really neccessary to practice more problems in it. So, Visit this link (GEEKSFORGEEKS) and start your journey!
Github Repo → https://github.com/LogicFirstTamil/DSA-in-C-CPP/blob/main/LinkedList.c
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
-yours Mathav krishnan