Single linked list of leading nodes implemented in Java

Characteristics of linked list

1. It is stored in node mode and is a chain structure.

2. Each node contains the data field, and the next field: points to the next node.

3. Each node of the linked list is not necessarily stored continuously.

4. The linked list is divided into two types: the leading node and the non leading node.

Implementation principle

Add node: as shown in the figure below, first traverse the original linked list, find the last node, and add the node to be added to the back of the node. The following describes how to find the last node.

The idea is like this. First traverse the whole linked list and define an auxiliary variable temp to temporarily store the traversed nodes. First, assign the head node to temp (traverse from the beginning of the node), and continuously traverse the next of the node through an endless loop until temp. Next = = null, the node temp is the last node in the linked list. Just point the next of the node to the new node.

Modify node: first traverse the entire linked list, match the number of the original linked list through the incoming number, find the corresponding number, and replace the data in the node.

Delete node: as shown in the figure, to delete a node, you need to traverse the entire linked list, find the corresponding number of the node, and then point the next of the previous node to the next node after the node to be deleted, that is (temp. Next = temp. Next. Next). Since the deleted node is not referenced, it will be recycled by the garbage collection mechanism.

Main code

package cn.mrlij.linkedlist;
/***
 * 单链表的实现
 * @author dreamer
 *
 */
public class SingleLinkedList {
 public static void main(String[] args) {
 SingleLinkedListDemo s = new SingleLinkedListDemo();
 HeroNode h1 = new HeroNode(1,"宋江","及时雨");
 HeroNode h2 = new HeroNode(3,"卢俊义","玉麒麟");
 HeroNode h3 = new HeroNode(4,"吴用","智多星");
 HeroNode h4 = new HeroNode(2,"林冲","豹子头");
 s.addByOrder(h1);
 s.addByOrder(h2);
 s.addByOrder(h3);
 s.addByOrder(h4);
 System.out.println("修改前————");
 s.list();
// HeroNode h5 = new HeroNode(4,"有用","超星星");
// s.update(h5);
 s.del(1);
 s.del(4);
 s.del(2);
 s.del(3);
 System.out.println("删除后————");
 s.list();
 }

}
class SingleLinkedListDemo{
 //创建一个头结点,初始化数据,头结点不要动,不放具体的数据
 private HeroNode head = new HeroNode(0,"","");
 //添加英雄
 public void add(HeroNode node) {
 //先找出最后的一个节点,把新加的节点放在最后一个节点的后面
 HeroNode temp = head;
 while(true) {
  if(temp.next == null) {
  break;
  }
  temp = temp.next;
 }
 temp.next = node;
 }
 public void addByOrder(HeroNode node) {
 HeroNode temp = head;
 boolean flag = false;
 while(true) {
  if(temp.next == null) {
  break;
  }
  if(temp.next.no>node.no) {
  break;
  }else if(temp.next.no == node.no) {
  flag = true;
  break;
  }
  temp = temp.next;
 }
 if(flag) {
  System.out.println("编号"+node.no+"已经存在了!");
 }else {
  node.next = temp.next;
  temp.next = node;
 }
 }
 public void update(HeroNode node ) {
 if(head.next == null) {
  System.out.println("链表为空!");
  return;
 }
 HeroNode temp = head.next;
 boolean flag = false;
 while(true) {
  if(temp == null) {
  break;
  }
  if(temp.no == node.no) {
  flag = true;
  break;
  }
  temp = temp.next;
 }
 if(flag) {
  temp.name = node.name;
  temp.nickname = node.name;
 }else {
  System.out.println("不存在该节点!");
 }
 }
 //删除节点
 public void del(int no) {
 if(head.next == null) {
  System.out.println("链表为空!");
  return;
 }
 HeroNode temp = head;
 boolean flag = false;
 while(true) {
  if(temp.next == null) {
  break;
  }
  if(temp.next.no == no) {
  flag = true;
  break;
  }
  temp = temp.next;
 }
 if(flag) {
  temp.next = temp.next.next;
 }else {
  System.out.println("该节点不存在!");
 }
 }
 public void list() {
 HeroNode temp = head;
 if(temp.next == null) {
  System.out.println("链表为空!");
  return;
 }
 while(true) {
  if(temp.next == null) {
  break;
  }
  System.out.println(temp.next);
  temp = temp.next;
 }
 }
}
class HeroNode{
 public int no;//英雄编号
 public String name;//人名
 public String nickname;//绰号
 public HeroNode next;//下一个节点
 public HeroNode(int no,String name,String nickname) {
 this.no = no;
 this.name = name;
 this.nickname = nickname;
 }
 @Override
 public String toString() {
 return "HeroNode [no=" + no + ",name=" + name + ",nickname=" + nickname + "]";
 }

}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>