Basic Java algorithm - detailed explanation of single linked list (supporting video at the end of the text)

Posted by joecooper on Sun, 10 Oct 2021 13:21:30 +0200

Cough, I'm Xiaobai. Yes, the main plot is back again. Now I'm in trouble. My boss asked me to design a class that can be used to save the data of multiple customers.

Can step 1 be implemented only with Java classes?

With my current Java skills, I only know that I can create a new Java class, and then write properties and methods in the Java class. Now I have a customer class. I can create many customer objects through this class new. But the question is, how do I save these objects?

For example, today there are 10 customers to register information. I have to have something to hold these customer objects.

Oh, I really don't understand what the boss thinks. Just write it down in a small book. I have to do something about the system.

But now I can only think about it. emmm, can I implement it only with Java classes?

Step 2: there are customer attributes in Category 2

Well, the goal is clear. I need to write a Java class that can store customer information. I casually wrote in the tool folder (which means the customer's node class):

The code is as follows:

package tool;
import entity.Customer;

class CustNode{
	public Customer data; 


CustNode class maintains the attributes of a customer. For convenience, we use public directly. Anyway, it doesn't matter if we use it ourselves.

Step 3 line up for rice

But it's useless. This class is to maintain the information of one customer at most. There's no way to save multiple customers. I'm worried to death. I haven't come up with a way after thinking for a long time. So I went to ask my boss. The boss was cooking and didn't have time to talk to me, but he didn't drive me away.

"Well, there will be many customers coming to pick up the meal. If you can't think of it, put it first. You can watch it for me here," the boss said.

"Let me see the queue. What's the meaning of this?" although I was full of complaints, I still did it. While taking out my mobile phone and brushing my microblog, I watched customers line up to pick up food.

Unexpectedly, looking at it, I suddenly had an inspiration.

Step 4 connect from one customer to another

Customers A, B and C are in line. Customer A knows that customer B is in front of him, and customer B knows that customer C is in front of him. From the perspective of the program, there must be A reference to B in A and A reference to C in B.

Then, as long as I maintain the reference of the next data in CustNode, can I save multiple customers in one class?

Step 5 add a next field

class CustNode{
	public Customer data; 
	public CustNode next;

Next represents the next data.

Step 6 parametric construction method

For convenience, I write a construction method with parameters. The advantage is that data can be assigned directly when new.

class CustNode{
	public Customer data; 
	public CustNode next;

	public CustNode(Customer data){ = data;

Step 7 maintain CustNode with a new class

After staring at the CustNode class for a long time, I found that if I use this class, I can only install two customers at most, one data and one next. If you install it again, it will be a little difficult. Therefore, I need to write another class to maintain CustNode, and then provide an add method and a display method to add and display customer data respectively.

I held it for a long time and finally wrote the code.

Step 8

package tool;
import entity.Customer;
import tool.CustNode;

public class TuziLinkedList{
	public CustNode firstNode; //First node 
	public CustNode currentNode;//Current node

	//New method
	public void add(Customer cst){
		//Wrap the data with node classes so that the next data can be pointed to
		CustNode data = new CustNode(cst);

		//First judge whether it is the first node
		if(this.firstNode == null){
			this.firstNode = data;
			this.currentNode = data;
			//If it is not the first node, it points to the next node of the current node, that is, = data;
			//The current node should also be moved because it points to the next node
			this.currentNode = data;

	//Show all nodes
	public void display(){
		//The first step must be to show the first node (this can actually be omitted)
		if(firstNode != null){

			//Then loop and keep looking for whether next is empty
			CustNode node =;
			while(node != null ){
				String name =;
				//At the end of the loop, point to the next node to continue the next round
				node =;

Step 9 design ideas

Step 10 it turns out that this is a single linked list

Later, I checked the data to know that, oh, it turned out that this belongs to a data structure, called a linked list structure.

Step 11

I modified the access permissions and codes of some classes. The customer class added get methods and set methods for name.

package entity;
public class Customer {
	String name;		// Customer name
	String sex;			//Gender
	String birthDate;	//birthday
	String phoneNumber;	//Telephone number
	int status;	//Customer status (1: normal, 2: abnormal)
	static String version = "1.0";

	public Customer(){


	public void eat(){
	   System.out.println( + " eating...");

	public static void pay(){
		System.out.println(" paying...");

	public String getName(){
		return name;

	public void setName(String name){ = name;

Step 12

import static tool.StringUtil.*;
import entity.*;
import tool.*;

public class Application {
   public static void main(String[] args){

		newLine("******Welcome to the rabbit restaurant membership system******");

		Customer c1 = new Customer();
		c1.setName("Luban 7" );
		Customer c2 = new Customer();
		Customer c3 = new Customer();
		c3.setName("Marco Polo");

		TuziLinkedList list = new TuziLinkedList();


Finally, here is a video explanation:

Topics: Java Algorithm linked list