Java - 单链表
阅读原文时间:2023年07月15日阅读:1

链表是一种常见的基础数据结构,是一种有序的列表,但不会按照线性顺序存储数据,而是在每一个节点里存储下一个节点的指针(next)。链表适合插入、删除,不宜过长,否则会导致遍历性能下降。

  • 以节点方式存储;
  • 每个节点包含data域,next域:指向下一个节点;
  • 链表的各个节点不一定是连续存储的;

代码实现:

节点类

public class HeroNode {

protected Integer no;  
protected String name;  
protected HeroNode next;

public HeroNode(Integer no, String name) {  
    this.no = no;  
    this.name = name;  
}

@Override  
public String toString() {  
    return "HeroNode\[" +  
            "no=" + no +  
            ", name='" + name + '\\'' +  
            '\]';  
}  

}

SingleLinkedList

public class SingleLinkedList {

private HeroNode root;  
private Integer size = 0;

/\*\*  
 \* 添加至链表头  
 \* @param hero  
 \*/  
public void addFirst(HeroNode hero) {  
    if (root == null) {  
        root = hero;  
    } else {  
        hero.next = root;  
        root = hero;  
    }  
    size++;  
}

/\*\*  
 \* 添加至链表尾  
 \* @param hero  
 \*/  
public void addLast(HeroNode hero) {  
    if (root == null) {  
        root = hero;  
    } else {  
        HeroNode temp = root;  
        while (temp.next != null) {  
            temp = temp.next;  
        }  
        temp.next = hero;  
    }  
    size++;  
}

/\*\*  
 \* 按照某属性的顺序添加  
 \* @param hero  
 \*/  
public void addByOrder(HeroNode hero) {  
    if (root == null) {  
        root = hero;

    } else {  
        HeroNode tmp = root;  
        // 新节点比头节点小  
        if (hero.no < tmp.no) {  
            root = hero;  
            root.next = tmp;  
            return;  
        }  
        // 找到next节点编号大于等于新节点的编号的节点,该节点与它的next节点之间就是新节点所在位置,  
        // 等于时不添加,控制台进行提示  
        while (tmp.next != null && tmp.next.no < hero.no) {  
            tmp = tmp.next;  
        }  
        if (tmp.next == null) {  
            tmp.next = hero;  
            return;  
        }

        if (tmp.next.no.equals(hero.no)) {  
            System.out.println("编号为" + hero.no + "已存在");  
            return;  
        }  
        hero.next = tmp.next;  
        tmp.next = hero;  
    }  
    size++;  
}

/\*\*  
 \* 修改  
 \* @param hero  
 \*/  
public void modify(HeroNode hero) {  
    HeroNode tmp = root;  
    while(tmp !=null) {  
        if (tmp.no.equals(hero.no)) {  
            tmp.name = hero.name;  
            break;  
        }  
        tmp = tmp.next;  
    }  
}

/\*\*  
 \* 根据编号获取节点  
 \* @param no  
 \* @return  
 \*/  
public HeroNode query(int no) {  
    HeroNode tmp = root;  
    while (tmp != null) {  
        if (tmp.no.equals(no)) {  
            return tmp;  
        }  
        tmp = tmp.next;  
    }  
    return null;  
}

/\*\*  
 \* 根据编号删除节点  
 \* @param no  
 \*/  
public void remove(int no) {  
    if (root == null) {  
        return;  
    }  
    //根节点  
    if (no == root.no) {  
        if (null != root.next) {  
            root = root.next;  
        } else {  
            root = null;  
        }  
        size--;  
        return;  
    }

    // 非根节点  
    HeroNode temp = root;  
    while (temp.next != null) {  
        if (no == temp.next.no) {  
            break;  
        }  
        temp = temp.next;  
    }  
    // 节点不存在  
    if (temp.next == null) {  
        return;  
    }  
    temp.next = temp.next.next;  
    size--;  
}

/\*\*  
 \* 打印  
 \*/  
public void display() {  
    if (root == null) {  
        System.out.println("This SingleLinkedList is null.");  
        return;  
    }  
    HeroNode temp = root;  
    while(temp != null) {  
        System.out.println(temp.toString());  
        temp = temp.next;  
    }  
}

/\*\*  
 \* 获取链表长度  
 \* @return  
 \*/  
public Integer getSize() {  
    return size;  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章