java实现单链表

java实现单链表,第1张

java实现单链表
public class Node {
    public int no;
    public String name;
    public Node next;

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

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


public class SinglelinkedList {
    // 头节点
    private Node head = new Node(0,"");

    public void addNode(Node node) {
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = node;
    }

    // 打印链表数据
    public void show() {
        if (head.next == null) {
            return;
        }
        Node tmp = head.next;
        while (tmp != null) {
            System.out.println(tmp.toString());
            tmp = tmp.next;
        }
    }

    // 更新数据
    public void update(Node node) {
        if (head.next == null) {
            throw new RuntimeException("链表为空");
        }
        Node tmp = head.next;
        while (tmp != null && tmp.no != node.no) {
            tmp = tmp.next;
        }
        if (tmp == null) {
            throw new RuntimeException("没有此数据");
        } else {
            tmp.no = node.no;
            tmp.name = node.name;
        }
    }

    // 删除数据
    public void delete(int no) {
        if (head.next == null) {
            throw new RuntimeException("链表为空");
        }
        // 标识被删除结点的前一个结点
        Node tmp = head;
        while (tmp.next != null && tmp.next.no != no) {
            tmp = tmp.next;
        }
        if (tmp.next == null) {
            throw new RuntimeException("无此节点");
        } else {
            tmp.next = tmp.next.next;
        }
    }

    // 按序插入数据
    public void insert(Node node) {
        // 标识插入位置的前一个结点
        Node tmp = head;
        // 是否已经存在此节点
        boolean flag = false;
        while (true) {
            if (tmp.next == null) {
                break;
            }
            if (tmp.next.no > node.no) {
                break;
            } else if (tmp.next.no == node.no) {
                flag = true;
                break;
            }
            tmp = tmp.next;
        }
        if (flag) {
            throw new RuntimeException("已经存在此节点,不可重复插入");
        }
        node.next = tmp.next;
        tmp.next = node;
    }

    // 获取链表有效结点个数
    public int getNodeCount() {
        if (head.next == null) {
            return 0;
        }
        int cnt = 0;
        Node tmp = head.next;
        while (tmp != null) {
            cnt++;
            tmp = tmp.next;
        }
        return cnt;
    }

    // 单链表反转
    public void reverse() {
        // 链表为空或者只有一个结点,无需反转
        if (head.next == null || head.next.next == null) {
            return;
        }
        Node tmp = head.next; // 当前结点
        Node reverselinkedListHead = new Node(0, "");
        Node tmpNext = null; // 当前结点的下一个结点
        while (tmp != null) {
            tmpNext = tmp.next;
            tmp.next = reverselinkedListHead.next;
            reverselinkedListHead.next = tmp;
            tmp = tmpNext;
        }
        head.next = reverselinkedListHead.next;
    }

    // 查找单链表倒数第K个结点
    public Node getNode(int k) {
        if (head.next == null) {
            throw new RuntimeException("链表为空");
        }
        int length = getNodeCount();
        if (k <=0 || k > length) {
            throw new RuntimeException("参数不合理");
        }
        int i = 0;
        Node tmp = head;
        while (i <= (length - k)) {
            tmp = tmp.next;
            i++;
        }
        return tmp;
    }

    // 逆序打印单链表
    public void reversePrint() {
        if (head.next == null) {
            throw new RuntimeException("链表为空");
        }
        Node tmp = head.next;
        Stack stack = new Stack<>();
        while (tmp != null) {
            stack.push(tmp);
            tmp = tmp.next;
        }
        while (!stack.isEmpty()) {
            System.out.println(stack.pop().toString());
        }
    }
}

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存