单向链表

import java.util.Stack;

class Node {//该节点省略data域
    public int no;
    public Node next;

    public Node(int no) {
        if (no >= 0) {//要求no要大于0
            this.no = no;
        }
    }

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

public class SingleLinkedList {
    public Node head = new Node(0);

    public void add(Node node) {//节点加入链表尾
        Node temp = head;
        while (true) {
            if (temp.no == node.no) {//该编号节点已存在
                return;
            } else if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = node;
    }

    public void addByOrder(Node node) {//根据节点编号升序加入
        Node temp = head;
        while (true) {
            if (temp.no == node.no) {//该编号节点已存在
                return;
            } else if (temp.next == null) {//加入链尾
                break;
            } else if (temp.next.no > node.no) {//插入第一个编号比node大节点的前一位
                break;
            }
            temp = temp.next;
        }
        node.next = temp.next;
        temp.next = node;
    }

    public void update(Node node) {//更新节点
        Node temp = head;
        while (true) {
            if (temp.next == null) {//该编号节点不存在
                return;
            } else if (temp.next.no == node.no) {//找到该编号节点
                node.next = temp.next.next;
                temp.next = node;
                return;
            }
            temp = temp.next;
        }
    }

    public void delete(int no) {
        Node temp = head;
        while (true) {
            if (temp.next == null) {//该编号节点不存在
                return;
            }
            if (temp.next.no == no) {
                temp.next = temp.next.next;//被跳过的节点没有引用,被回收
                return;
            }
            temp = temp.next;
        }
    }

    public void show() {//打印链表
        Node temp = head.next;
        while (true) {
            if (temp == null) {
                return;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public int getLength() {//获取链表有效节点个数
        int length = 0;
        Node temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    public Node lastIndexNode(int index) {//获取倒数第index个节点
        Node temp = head.next;
        int size = getLength();
        if (temp == null) {
            return null;
        }
        if (index > size || index <= 0) {
            return null;
        }
        for (int i = 0; i < size - index; i++) {
            temp = temp.next;
        }
        return temp;
    }

    public void reverse() {//反转链表
        Node cur = head.next;
        Node next = null;
        Node pre = new Node(0);
        if (cur == null || cur.next == null) {
            return;
        }
        while (cur != null) {
            next = cur.next;
            cur.next = pre.next;
            pre.next = cur;
            cur = next;
        }
        head.next = pre.next;
    }

    public void invertPrint() {//倒序打印
        if (head.next == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        Node cur = head.next;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }
    
    //用一条空的单向链表调用,传入两条待合并单向链表各自的第一个有效节点
    public void merge(Node n1, Node n2) {
        Node cur = head;
        while (n1 != null && n2 != null) {
            if (n1.no <= n2.no) {
                cur.next = n1;
                n1 = n1.next;
            } else {//n1.no > n2.no
                cur.next = n2;
                n2 = n2.next;
            }
            cur = cur.next;
        }//若n1全部比较合并后,n2剩余节点加入链尾;反之亦然
        cur.next = n1 == null ? n2 : n1;
    }
}
posted @   半条咸鱼  阅读(18)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示