Skip to main content

Single Linked List | Demo Project | Learn Java

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

Popular

Tkinter Login System | Demo Project | Learn Python

Python practical Requirement Simple user registration and login application. Three interfaces minimum Sign-in: This is the main interface. Enter email and password, then log in. And the option to create accounts for new users. Sign-up: This interface is used to register new users. All user details are saved in a text file. You can build your own structure to save data. Dashboard: This interface will pop up when the user sign-in is successful. This interface has a menu bar with submenu items and a simple text file. You can customize this as your own. All registered users are saved in a text file called “users”. Read the text file when the user is sign-in and write when the new user sign-up. Interfaces This is the main interface of the program also the sign-in interface. Enter email and password and press the sign-in button. The dashboard will be pop up if login is successful else show an error message. This is the interface when the user wants to create a new account. This inter...

Chatting Application | Demo Project | Learn Java

How to make a simple chatting application Java Web? REQUIREMENT Simple chat application. Users can register their names to the application. After login, the user can find any registered friend and start a chat. If a user had previous chat with a selected friend, it should load in chat view. Sent messages and received messages must show without affecting the chat page. ASSUMPTIONS for DEMO PROJECT Usernames do not duplicate. Register different names, login, and chat with others. No need of sending pictures, videos, etc. Just create a text message chat. This demo project is not about user registration, login, file uploading… etc. This demo project is about chatting between two users and how to keep and update chats using a data structure. IMPROVEMENTS Develop better user registration and login system with generating unique user ids to avoid duplicate issues. Develop privilege management for users. Develop chat that can send images, videos too. OPTIONAL Do not use MYSQL or any other DBMS...

How to get a proper Affidavit in Sri Lanka?

What is an Affidavit? An affidavit is a written statement by a person under an oath or affirmation. An affidavit is administered, signed, and sealed by a registered lawyer, the high court, and the ministry. An affidavit includes a related person's personal details, family details, document details, and a comparison of each document detail.  An affidavit includes an oath or affirmative statement to each document's comparison is true and it states about the same person even if there are some differences. An affidavit includes several signs and seals at the bottom of the statement. 1. Related person's signature. 2. The lawyer's name, signature, and seal. 3. The ministry's signature, and seal. The lawyer,  1. Should be registered in the high court. 2. Should have a valid seal. 3. Should able to issue high court receipts. The lawyer should meet all the requirements above to certify a valid affidavit What is the routine of collecting a proper affidavit in Sri Lanka? First...