?
package com.demo.linkedlist;
/**
* description
* @author xiangqian
* @since 2022/05/16 10:18
*/
public class HeroNode {
/**
* 编号
*/
private int no;
/**
* 姓名
*/
private String name;
/**
* 昵称
*/
private String nikeName;
/**
* 下一个节点
*/
private HeroNode next;
public HeroNode(int no, String name, String nikeName) {
this.no = no;
this.name = name;
this.nikeName = nikeName;
}
@Override
public String toString() {
return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nikeName='" + nikeName + '\'' + '}';
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNikeName() {
return nikeName;
}
public void setNikeName(String nikeName) {
this.nikeName = nikeName;
}
public HeroNode getNext() {
return next;
}
public void setNext(HeroNode next) {
this.next = next;
}
}
package com.demo.linkedlist;
/**
* description
* @author xiangqian
* @since 2022/05/16 10:21
*/
public class LinkedListDemo {
/**
* 头节点
*/
public HeroNode head = new HeroNode(0, "", "");
/**
* 新增数据
*/
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.getNext() == null) {
break;
}
temp = temp.getNext();
}
temp.setNext(heroNode);
}
/**
* 新增数据并排序
*/
public void addSort(HeroNode heroNode) {
HeroNode temp = head;
//标识添加的编号是否存在
boolean flag = false;
while (true) {
if (temp.getNext() == null) {
break;
}
if (temp.getNext().getNo() > heroNode.getNo()) {
break;
} else if (temp.getNext().getNo() == heroNode.getNo()) {
flag = true;
break;
}
temp = temp.getNext();
}
if (flag) {
throw new RuntimeException("准备插入的编号已经存在");
} else {
heroNode.setNext(temp.getNext());
temp.setNext(heroNode);
}
}
/**
* 修改链表数据
*/
public void update(HeroNode heroNode) {
HeroNode temp = head;
//表示是否查询到节点
boolean flag = false;
while (true) {
if (temp.getNext() == null) {
break;
}
if (temp.getNext().getNo() == heroNode.getNo()) {
flag = true;
break;
}
temp = temp.getNext();
}
if (flag) {
temp.getNext().setName(heroNode.getName());
temp.getNext().setNikeName(heroNode.getNikeName());
} else {
throw new RuntimeException("准备插入的编号不存在");
}
}
/**
* 删除链表数据
*/
public void delete(int no) {
HeroNode temp = head;
//标识添加的编号是否存在
boolean flag = false;
while (true) {
if (temp.getNext() == null) {
break;
}
if (temp.getNext().getNo() == no) {
flag = true;
break;
}
temp = temp.getNext();
}
if (flag) {
temp.setNext(temp.getNext().getNext());
} else {
throw new RuntimeException("准备删除的编号不存在");
}
}
/**
* 遍历数据
*/
public void list() {
if (head.getNext() == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.getNext();
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.getNext();
}
}
public static void main(String[] args) {
LinkedListDemo linkedListDemo = new LinkedListDemo();
HeroNode heroNode1 = new HeroNode(1, "东", "小东");
HeroNode heroNode2 = new HeroNode(2, "南", "小南");
HeroNode heroNode3 = new HeroNode(3, "西", "小西");
HeroNode heroNode4 = new HeroNode(4, "北", "小北");
linkedListDemo.addSort(heroNode1);
linkedListDemo.addSort(heroNode3);
linkedListDemo.addSort(heroNode2);
linkedListDemo.addSort(heroNode4);
HeroNode heroNode5 = new HeroNode(3, "2西", "2小西");
linkedListDemo.update(heroNode5);
linkedListDemo.delete(4);
linkedListDemo.list();
}
}
|