欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用带 head 头的单向链表实现

程序员文章站 2022-07-05 09:41:47
package com.example.dataStructureAndAlgorithm.LinkedList;public class SingleLinkedListDemo { public static void main(String[] args) { Node node1=new Node(1,"1"); Node node2=new Node(2,"2"); Node node3=new Node(3,"3");...
package com.example.dataStructureAndAlgorithm.LinkedList;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        Node node1=new Node(1,"1");
        Node node2=new Node(2,"2");
        Node node3=new Node(3,"3");
        Node node4=new Node(4,"4");
        SingleLinkedList singleLinkedList=new SingleLinkedList();
//        singleLinkedList.addNode(node1);
//        singleLinkedList.addNode(node3);
//        singleLinkedList.addNode(node2);
//        singleLinkedList.addNode(node4);
        singleLinkedList.addNodeByNo(node1);
        singleLinkedList.addNodeByNo(node3);
        singleLinkedList.addNodeByNo(node2);
        singleLinkedList.addNodeByNo(node4);
  //      singleLinkedList.update(new Node(5,"222"));
        singleLinkedList.show();
        System.out.println("删除后");
        singleLinkedList.delete(1);
        singleLinkedList.delete(4);
        singleLinkedList.show();
    }
}

class SingleLinkedList {
    private Node head = new Node(0, "");

    public void addNode(Node node) {
        Node temp = head;
        while (true) {
            if (temp.next == null) {//说明找到了链表的末尾,将最后这个节点的next指向要添加的节点,退出循环
                temp.next = node;
                return;
            }
            temp = temp.next;//如果没有找到链表的末尾,则将temp后移
        }
    }

    /***
     * 添加节点,考虑编号顺序
     * @param node
     */
    public void addNodeByNo(Node node){
       boolean flag=false;
        Node temp=head;
        while (true){
            if (temp.next==null){//说明temp已经在链表的末尾,直接添加到temp后面
                break;
            }else if (temp.next.no>node.no){//temp的下一个节点的no>node.no,说明找到要添加的位置,插入到temp的后面
                break;
            }else if (temp.next.no==node.no){//说明要添加的节点编号已经存在
                flag=true;
                break;
            }
            temp=temp.next;//temp后移,遍历链表
        }
        if (flag){
            System.out.println("此编号节点已存在");
        }else {
            //插入到链表中, temp 的后面
            node.next=temp.next;
            temp.next=node;

        }

    }
    public void update(Node node){
        Node temp=head;
        if (temp.next!=null){
            boolean flag=false;
            while (true){
                if (temp.next==null){
                    break;
                }
                if (temp.next.no==node.no){
                    flag=true;
                    break;
                }
                temp=temp.next;
            }
            if (flag){
                temp.next.name=node.name;
            }else {
                System.out.println("未找到相关编号节点");
            }

        }else
        System.out.println("链表为空");
    }

    public void delete(int no){
        if (head.next==null){
            System.out.println("链表为空");
        }
        Node temp=head;
        boolean flag=false;
        while (true){
            if (temp.next==null){
                break;
            }else if (temp.next.no==no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            //删除。删除temp的下一个节点就是将temp的next指向下一个节点的下一个
            temp.next=temp.next.next;
        }else {
            System.out.println("未找到编号节点");
        }
    }
    public void show() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        Node temp = head.next;
        while (true) {
            if (temp.next == null) {
                System.out.println(temp);
                return;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class Node {
    public int no;
    public String name;
    Node next;

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

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name=" + name +
                '}';
    }
}

本文地址:https://blog.csdn.net/Paranoid_94/article/details/109579193