Realization of Java Singly Linked List

This blog will introduce the following:

-What is a java linked list and what are its characteristics?
-How to realize the basic functions of java linked list
-what is generic? What is the use of generics?

Linked list introduction

  • A linked list is a structure for storing data. The storage space of our commonly used arrays is continuous, while the storage space of linked lists is discrete and discontinuous. The discontinuous nature of linked lists makes the use of linked lists more flexible than arrays, and there is no need to define its size at the beginning.

Insert picture description here

  • From the above figure, we can see that, except for the head and tail nodes, each node is composed of two parts: the data field and the pointer field. The data field is used to store the required data, and the pointer field stores the location of the next node. When we need to find a certain node on the linked list, we need the position of the head node and the relative position of the target node, and then start from the head node and look back node by node until the target node is found. (Because each node in the singly linked list only knows the position of the next node, it is not possible to skip any node, you can only look back one by one)
  • Now we have a general understanding of the structure of the linked list. If we want to implement the function of the linked list, we first need to create a Node class to store data and store the location of the next node.
	public class Node<E>{
    
    //这里的E是指泛型,后面有讲解
		//数据
		E data;
		//下一个节点
		Node<E>next;
		//要往节点中存入数据时调用此方法
		Node(E data){
    
    this.data=data;}
		//创建首个头节点时调用此方法
		Node(){
    
    }
	}

Generics
when we want to store data in the list, but not yet know what type of data need to put time, can be used to temporarily replace the generic might want to place our future classes. Therefore, generics can refer to any class, and what kind of class it specifically refers to, only needs to be defined when it is used. For example, in the above example, when we create a Node object, we need to define the generic as the class we need to use. Node<Integer> head()=new Node();You can create a node to store Integer. Generics are represented by uppercase letters. Commonly used letters are E (representing element), K (representing key), and V (representing value).

Create a linked list

  • At the beginning of the creation of the linked list, we must first define the head node and the tail node of the linked list, because there is no data in the linked list at this time, the head node and the tail node are empty, and the head node and the tail node are the same node. Then we have to define the length of the linked list, the initial value is 0.
    At the beginning of the creation of the linked list, the head node and the tail node are the same node
public class LinkList<E> {
    
    
	private Node<E> head,tail;//创建头尾节点
	private int size;
	
	public LinkList(){
    
    
		size=0;
		head=new Node<>();
		tail=head;
	}

The basic functions provided by the linked list should include: query the length of the linked list, obtain the element at a certain position of the linked list, add an element to the end of the linked list, modify the element at a certain position of the linked list, delete an element at a certain position of the linked list, and insert an element at a specified position. Let's implement them one by one.

Query length and elements

The query operation does not need to modify the linked list, which is relatively simple. The query length only needs to return the size value of our linked list.

public int size(){
    
    
		return size;
	}

Querying the element at a certain position of the linked list, as mentioned earlier, we only know what the next node of the current node is, so we need to look for it node by node.

public E get(int index){
    
    
		if(index>=0&index<=size-1){
    
    //先判断链表是否为空或索引是否超出链表长度范围
			Node<E>temp=head.next;
			//因为头节点是不储存数据的,从头节点的后一个节点才开始储存数据
			//所以我们需要从头节点的后一个节点遍历链表
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;
			}
			return temp.data;
		}else{
    
    
			System.out.println("索引超出表格范围!");
			return null;
		}
	}

Add element

Insert picture description here

	public void add(E e){
    
    
		Node<E>node=new Node<>(e);//创建新节点存放元素
		tail.next=node;//把该节点放到原尾节点的最后
		tail=node;//把新节点定义为尾节点
		size++;//更新长度
	}

Modify elements

The modification element is similar to the query element, except that the modification element is one more step to modify the data at the specified location, and the method of modifying the element does not set a return value.

public void set(int index,E e){
    
    
		if(index>=0&index<=size-1){
    
    
			Node<E>temp=head.next;
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;
			}
			temp.data=e;//将该节点的元素更新
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}

Delete element

Deleting elements is a slightly more complicated operation, let’s take a look at the schematic diagram
Insert picture description here

We need to find the previous node and the next node of the node that needs to be deleted, and then connect these two nodes.

public void del(int index){
    
    
		if(index>0&index<=size-1){
    
    
			Node<E>temp1=head.next;//从头节点的后一个节点开始遍历
			Node<E>temp2,temp3;
			for(int i=0;i<index-1;i++){
    
    
				temp1=temp1.next;//找到需删除的节点的前一个节点
			}
			temp2=temp1.next;//temp2即为需要删除的节点
			temp3=temp2.next;//找到需要删除的节点的后一个节点
			temp1.next=temp3;//将节点1和节点3进行连结
			size--;//删除操作完成后记得更新链表长度
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}

Insert element

Insert picture description here

public void input(int index,E e){
    
    
		if(index>=0&index<=size-1){
    
    
			Node<E>temp=head.next;
			Node<E>temp2=new Node<>(e);//需要插入的新节点
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;//找到需要插入的位置(新节点插入于该节点之后)
			}
			temp.next=temp2;
			temp2.next=temp.next;//此处的temp.next请看示意图
			size++;//插入操作完成后记得更新链表长度
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}

have a test

	public static void main(String[]arguments){
    
    
		LinkList<Integer> linkList=new LinkList<>();//创建一个存放Integer的链表
		
		for(int i=0;i<10;i++){
    
    
			linkList.add(i);//往链表中加入10个元素
		}
		
		linkList.del(5);//删除链表中的第6个元素
		linkList.set(2,88);//将链表中第3个元素更改为88
		linkList.input(3,77);//在链表第4个元素的后面插入一个元素
		
		for(int i=0;i<linkList.size();i++){
    
    //将操作后的链表逐一输出
			System.out.println(linkList.get(i));
		}
	}

operation result
Insert picture description here

Complete code

public class LinkList<E> {
    
    
	
	private Node<E> head,tail;
	private int size;
	
	public LinkList(){
    
    
		size=0;
		head=new Node<>();
		tail=head;
	}
	
	public int size(){
    
    
		return size;
	}
	
	public void add(E e){
    
    
		Node<E>node=new Node<>(e);
		tail.next=node;
		tail=node;
		size++;
	}
	
	public void del(int index){
    
    
		if(index>0&index<=size-1){
    
    
			Node<E>temp1=head.next;
			Node<E>temp2,temp3;
			for(int i=0;i<index-1;i++){
    
    
				temp1=temp1.next;
			}
			temp2=temp1.next;
			temp3=temp2.next;
			temp1.next=temp3;
			size--;
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}
	
	public E get(int index){
    
    
		if(index>=0&index<=size-1){
    
    
			Node<E>temp=head.next;
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;
			}
			return temp.data;
		}else{
    
    
			System.out.println("索引超出表格范围!");
			return null;
		}
	}
	
	public void set(int index,E e){
    
    
		if(index>=0&index<=size-1){
    
    
			Node<E>temp=head.next;
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;
			}
			temp.data=e;
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}
	
	public void input(int index,E e){
    
    
		if(index>=0&index<=size-1){
    
    
			Node<E>temp=head.next;
			Node<E>temp2=new Node<>(e);
			for(int i=0;i<index;i++){
    
    
				temp=temp.next;
			}
			temp2.next=temp.next;
			temp.next=temp2;
			size++;
		}else{
    
    
			System.out.println("索引超出表格范围!");
		}
	}
	
	class Node<E>{
    
    
		//数据
		E data;
		//下一个节点
		Node<E>next;
		
		Node(E data){
    
    
			this.data=data;
		}
		
		Node(){
    
    }
	}
	
	public static void main(String[]arguments){
    
    
		LinkList<Integer> linkList=new LinkList<>();
		
		for(int i=0;i<10;i++){
    
    
			linkList.add(i);
		}
		
		linkList.del(5);
		linkList.set(2,88);
		linkList.input(3,77);
		
		for(int i=0;i<linkList.size();i++){
    
    
			System.out.println(linkList.get(i));
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_44710412/article/details/113706528