码迷,mamicode.com
首页 > 编程语言 > 详细

【Java数据结构】带头节点的单链表的增删改查

时间:2020-08-31 13:22:17      阅读:45      评论:0      收藏:0      [点我收藏+]

标签:bool   string   lis   err   private   ext   boolean   null   顺序   

/**
 * @author ZhiYi Li
 * @create 2020/8/25 11:37
 * 带头节点的单链表
 * 头节点不存放数据
 */

//管理单链表
class SingleLinkedList {
    //初始化一个头节点
    private final HeroNode head = new HeroNode(0,"");
    //添加节点到单项链表,无顺序添加
    public void add(HeroNode heroNode){
        HeroNode temp = head;
        while (temp.next!=null){
            temp = temp.next;
        }
        temp.next = heroNode;
    }
    //按顺序添加,编号可重复
    public void addNodeByNo(HeroNode heroNode){
        HeroNode temp = head;
        while (temp.next != null&&temp.next.no <= heroNode.no) {
            temp = temp.next;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }
    //按顺序添加,编号不可重复
    public void addNodeByNo2(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;
        while (temp.next != null) {
            if(temp.next.no == heroNode.no){
                flag = true;
                break;
            }else if(temp.next.no > heroNode.no){
                break;
            }
            temp = temp.next;
        }
        if(flag){
            System.out.println("数据编号重复!");
            return;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }
    //根据编号修改节点的信息
    public void update(HeroNode newHeroNode){
        if(head.next == null){
            System.out.println("链表为空!");
            return;
        }
        HeroNode temp = head.next;
        while(temp!=null&&temp.no!=newHeroNode.no){
            temp = temp.next;
        }
        if(temp!=null){
            temp.name = newHeroNode.name;
        }else {
            System.out.println("节点不存在!");
        }

    }

    //根据编号删除节点
    public void deleteByNo(int no){
        if(head.next==null){
            System.out.println("链表为空!");
            return;
        }
        HeroNode temp = head;
        while (temp.next!=null&&temp.next.no!=no){
            temp = temp.next;
        }
        if(temp.next==null){
            System.out.println("节点不存在!");
            return;
        }
        temp.next = temp.next.next;
    }
    //根据编号显示节点
    public HeroNode getNodeByNo(int no){
        if(head.next==null){
            System.out.println("链表为空!");
            return null;
        }
        HeroNode temp = head;
        while (temp.next!=null&&temp.next.no!=no){
            temp = temp.next;
        }
        if(temp.next==null){
            System.out.println("节点不存在!");
            return null;
        }
        return temp.next;
    }
    //查找第k个节点
    public HeroNode getNode(int k){
        if(head.next==null){
            System.out.println("链表为空!");
            return null;
        }
        HeroNode temp = head;
        int num = 1;
        while (num!=k){
            temp = temp.next;
            num++;
        }
        return temp.next;
    }

    //删除单链表的第k个节点
    public void deleteNo(int k){
        if(head.next==null){
            System.out.println("链表为空!");
            return;
        }
        HeroNode temp = head;
        int num = 1;
        while (num!=k){
            temp = temp.next;
            num++;
        }
        temp.next = temp.next.next;
    }


    //显示链表
    public void show(){
        HeroNode temp =head.next;
        if(temp == null){
            System.out.println("链表无数据!");
        }
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
//数据结构
class HeroNode {
    public int no;
    public String name;
    public HeroNode next;

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}
//测试
public class SingleLinked {
    public static void main(String[] args) {
        SingleLinkedList linkedList  =new SingleLinkedList();
        linkedList.show();
        linkedList.addNodeByNo2(new HeroNode(0,"张三"));
        linkedList.addNodeByNo2(new HeroNode(1,"李四"));
        linkedList.addNodeByNo2(new HeroNode(4,"王五"));
        linkedList.addNodeByNo2(new HeroNode(2,"赵六"));
        linkedList.addNodeByNo2(new HeroNode(2,"赵六"));
        linkedList.addNodeByNo2(new HeroNode(3,"吴七"));
        linkedList.addNodeByNo2(new HeroNode(2,"丽丽"));
        linkedList.show();
        linkedList.update(new HeroNode(7,"莉莉"));
        linkedList.show();
        linkedList.deleteByNo(4);
        linkedList.show();
        linkedList.deleteNo(4);
        linkedList.show();
        System.out.println(linkedList.getNodeByNo(2));
        System.out.println(linkedList.getNode(2));
    }
}

 

【Java数据结构】带头节点的单链表的增删改查

标签:bool   string   lis   err   private   ext   boolean   null   顺序   

原文地址:https://www.cnblogs.com/lilice/p/13560665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!