根据自己的学习项目写得单向链表CRUD。学习单向链表是为了更好学习HashMap底层原理。
蚂蚁课堂—学习HashMap底层源码
代码语言:javascript复制/** * 单向链表练习 * * @author zx * @date 2021年05月03日 14:17 */
public class SingleLinkedListDemo {
private Boolean accessOrder;
//表明该节点是头节点
private Node header = new Node(0, "", "");
public SingleLinkedListDemo() {
this.accessOrder = false;
}
public SingleLinkedListDemo(Boolean accessOrder) {
this.accessOrder = accessOrder;
}
public static void main(String[] args) {
SingleLinkedListDemo list = new SingleLinkedListDemo(true);
list.addNode(new Node(4, "唐僧", "老大"));
list.addNode(new Node(2, "孙悟空", "老二"));
list.addNode(new Node(3, "猪八戒", "老三"));
list.addNode(new Node(1, "沙和尚", "老四"));
list.list();
System.out.println("修改:-----------------------------------------------");
list.updateByNodeId(new Node(4,"玉皇大帝","终极BOSS"));
list.list();
System.out.println("删除:-----------------------------------------------");
list.del(1);
list.list();
}
/** * 按照添加数据顺序往链表节点末尾进行添加 * * @param node 新增节点对象 */
public void addNode(Node node) {
Node temp = header;
if (accessOrder) {
boolean falg = false;//记录添加位置的标识位置
//比如按照ID 进行顺序添加到链表中
while (true) {
if (temp.next == null) {
//头节点 。第一次往头结点 下进行添加
//尾节点 。往尾节点 后面添加
falg = true;
break;
}
if (temp.next.id > node.id) {
//新增的数据位置 就是 temp位置
falg = true;
break;
}
temp = temp.next;
}
//不同引用执向同一个栈区
node.next = temp.next;
temp.next = node;
} else {
//每添加一个节点,就直接加入到链表的最后[在添加的时候需要找到最后一个节点]
while (temp.next != null) {
//指针往下移动
temp = temp.next;
}
temp.next = node;
}
}
/** * 链表的遍历 */
public void list() {
if (header.next == null) {
return;
}
Node temp = header;
while (true) {
if (temp.next == null) {
break;
} else {
temp = temp.next;
}
System.out.println(temp);
}
}
/** * 根据ID 修改 * @param node 需要修改的Node对象 */
public void updateByNodeId(Node node){
if(header.next == null){
throw new MyException("没有该节点,请核实处理");
}
Node temp = header.next;
boolean falg = false;
while(true){
if(temp.id == node.id){
falg = true;
break;
}
//尾节点
if(temp.next == null){
break;
}
temp = temp.next;
}
if(falg){
temp.name = node.name;
temp.nickName = node.nickName;
}
}
public void del(Integer nodeId){
Node temp = header;
boolean falg = false;
while(true){
if(temp.next == null){
break;
}
if(temp.next.id == nodeId){
falg = true;
break;
}
temp = temp.next;
}
if(falg){
temp.next = temp.next.next;
}else{
throw new MyException("没有该节点,请核实处理");
}
}
/** * 节点数据对象 */
private static class Node {
private Integer id;
private String name;
private String nickName;
//下一个节点
private Node next;
public Node(Integer id, String name, String nickName) {
this.id = id;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "Node{"
"id=" id
", name='" name '''
", nickName='" nickName '''
'}';
}
}
}
代码语言:javascript复制/** * 自定义异常 * * @author zx * @date 2021年05月03日 9:40 */
public class MyException extends RuntimeException{
public MyException() {
}
public MyException(String message) {
super(message);
}
public MyException(Throwable cause) {
super(cause);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100718.html原文链接:https://javaforall.cn