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

2021-03-31 18:27

阅读:458

标签: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){
                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


评论


亲,登录后才可以留言!