How to program a collection of data without using any array or list?
This is an earlier way to keep the collection of data.There are some special points in the structure of a linked list.A linked list is made by keeping objects called “nodes”.
There are two partitions in a single node. Data and Link.
This is a must. Every node must have these two partitions.
The data partition is used to keep user data.
We can use generic type to keep the same data type or change as required.
We can keep any data type if we use an object as the generic type.
Link partition is used to keep the link to the next node.
As a structure, every node has data and links to the next node.
The last node of the linked list is linked to the null node.
Every linked list has a head. Also known as the first node of the linked list.
When we want to read data. We have to start from the first node and loop next.
We want to start from the head(first node) and next (second node) and next (third node) if we want to get third node data.
So this linked list is reading only forward way. We call this “Traverse Forward”.
And that's why this structure is called a single-linked list.
A graphical explanation of a single linked list is given below.

HOW TO PROGRAM A LINKED LIST IN JAVA?
Required knowledge before program,Class and inner class.
Objects, instance data, and methods.
Generic types.
If else condition and while loop.
Foundation of the custom linked list class,
public class customLinkedList<A> {private node<A> head;
private int size;
private static class node<A> {
A a;
node<A> next;
public node(A a, node<A> next) {
this.a = a;
this.next = next;
}
}
}
Create a class called custom linked list with generic type.
Create an inner class called node with the same generic type used before.
Create two instances inside node inner class.
The first one is A a (data) and node next instance of the same node class (link to next node).
Write a constructor and set these instances.
Create two instances inside the custom linked list class.
The first one is node head (first node) and int size (for calculating node count)
HOW TO ADD & READ DATA?
The programmable explanation for add data to the linked list.
public void add(A a) {if (head is null) {
head = new node;
head.next = new node(a, new null node);
size++;
} else {
node temp = head.next;
while (true) {
if (temp.next.a is not null) {
temp = temp.next;
} else {
temp.next = new node(a, new null node);
break;
}
}
size++;
}
}
Programmable explanation of print data from the linked list.
public void taverseforward() {node<A> temp = head.next;
while (true) {
print (temp.a);
if (temp.next.a is null) {
break;
} else {
temp = temp.next;
}
}
}
That's all !!!
Make your own project and practice. Memorize main standards, logic and keep thinking while developing.
I have also added,
how to search given value?
how to delete a specified node?
how to get the size of the linked list?
functions/methods to the demo project.
Follow and try to understand the demo project given.
It's not hard to think if you understand the logic of add and read data.
The demo project is made using an IDE called Netbeans.
Find the src folder and copy files/ java classes if you use another IDE.
Hope you enjoyed it, learned something new, and helped for better work.
Thank you for reading. See you with another practical.
Download demo project here
Comments
Post a Comment