JAVA带头结点的单链表功能实现

JAVA带头结点的单链表功能实现,第1张

JAVA带头结点的单链表功能实现

第一次写,不废话,直接上源代码,注释自己写的:

package Array;

public class SingleChainTable {
    public static void main(String[] args) {
        Heronode hero1 = new Heronode(1, "宋江", "及时雨");
        Heronode hero2 = new Heronode(6, "武松", "行者");
        Heronode hero3 = new Heronode(5, "孙三娘", "母夜叉");
        Heronode hero4 = new Heronode(2, "李逵", "黑旋风");
        Heronode hero5 = new Heronode(2, "李易燃", "大脾气");
        SinglelinkedList singlelinkedList = new SinglelinkedList();
//        singlelinkedList.add(hero1);测试,可自己试试看
//        singlelinkedList.add(hero2);
//        singlelinkedList.add(hero3);
//        singlelinkedList.add(hero4);
        singlelinkedList.addPosition(hero1);
        singlelinkedList.addPosition(hero2);
        singlelinkedList.addPosition(hero3);
        singlelinkedList.addPosition(hero4);
        singlelinkedList.list();
        singlelinkedList.update(hero5);
        System.out.println("修改后的单链表为");
        singlelinkedList.list();
        singlelinkedList.deletePosition(hero2);
        System.out.println("删除编号为" + hero2.num + "的单链表为");
        singlelinkedList.list();
        singlelinkedList.delete();
        System.out.println("删除尾结点后的单链表为");
        singlelinkedList.list();
    }
}

class SinglelinkedList {
    //初始化头结点,不存放数据
    private Heronode head = new Heronode(0, "", "");

    public void add(Heronode hnode) {
        Heronode temp = head;
        while (true) {
            //因为尾部节点肯定指向空节点,所以就可以找到
            if (temp.next == null) {
                break;
            }
            //此时temp指向最后;
            temp = temp.next;//让temp节点变成自己指向的节点
        }
        temp.next = hnode;//等找到最后的节点,我们就让其指向新节点
    }

    
    public void addPosition(Heronode hnode) {
        Heronode temp = head;
        boolean flag = false;//标记是否插入成功
        while (true) {
            if (temp.next == null) {
                
                break;
            }
            if (temp.next.num > hnode.num) {
                
                break;
            } else if (temp.next.num == hnode.num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("添加失败");
        } else {
            
            hnode.next = temp.next;
            temp.next = hnode;
        }
    }

    //修改指定节点
    public void update(Heronode newhnode) {
        Heronode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.num == newhnode.num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newhnode.name;
            temp.nickname = newhnode.nickname;
        } else {
            System.out.println("未找到该编号未" + newhnode.num + "的节点");
        }
    }

    //删除指定位置的节点
    public void deletePosition(Heronode hnode) {
        Heronode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.num == hnode.num) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            
            temp.next = hnode.next;
            hnode.next = null;
        } else {
            System.out.println("该单链表中不存在编号为" + hnode.num + "的节点");
        }
    }

    //删除尾结点
    public void delete() {
        Heronode temp = head;
        while (true) {
            //找到尾结点的前一个结点
            if (temp.next.next == null) {
                break;
            }
            temp = temp.next;
        }
        
        temp.next = null;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("该单链表为空");
        }
        Heronode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            //因为已经重写了toString方法,所以可以直接打印信息;
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class Heronode {
    
    int num;
    String name;
    String nickname;
    Heronode next;

    Heronode(int num, String name, String nickname) {
        this.num = num;
        this.name = name;
        this.nickname = nickname;
    }

    
    @Override
    public String toString() {
        return "HeroNode{" +
                "num=" + num +
                ", name='" + name + ''' +
                ", nickname='" + nickname +
                '}';
    }
}
运行结果:

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697101.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存