Java实现单链表

简单实现带有头结点的单链表以及操作链表的基本方法

package 链表;

import java.util.Stack;

public class SingleLinkedListDemo {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add(2);
        singleLinkedList.add(3);
        singleLinkedList.add(1, 1);
        singleLinkedList.show();
        System.out.println("=========================");

        singleLinkedList.add(5, 3);
        singleLinkedList.add(4, 123);
        singleLinkedList.show();
        System.out.println("=========================");

        singleLinkedList.update(0, 3);
        singleLinkedList.show();
        System.out.println("=========================");

        singleLinkedList.delete(3);
        singleLinkedList.show();
        System.out.println("=========================");

        System.out.println(singleLinkedList.find(-1));
        System.out.println(singleLinkedList.find(4));
        System.out.println(singleLinkedList.findLast(1));
        System.out.println("=========================");

        System.out.println(singleLinkedList.getLength());
        System.out.println("=========================");

        SingleLinkedList reverse = singleLinkedList.reverse();
        reverse.show();
        reverse.showReverse();
        System.out.println("=========================");

        singleLinkedList.mergeList(reverse);
        singleLinkedList.show();

    }
}

/**
 * 链表类,包含对链表的增删改查方法
 */
class SingleLinkedList {
    //初始化头结点,头结点val存放列表长度
    private final SingleLinkedListNode headNode = new SingleLinkedListNode(0);

    /**
     * 添加一个节点到链表末尾
     *
     * @param val 新插入节点的值
     */
    public void add(int val) {
        SingleLinkedListNode temp = headNode;
        while (temp.nextNode != null)
            temp = temp.nextNode;
        temp.nextNode = new SingleLinkedListNode(val);
        //插入节点后,链表长度+1
        headNode.val++;

    }

    /**
     * 添加节点到指定位置,超出链表长度则插入链表尾部
     *
     * @param val 节点值
     * @param loc 添加位置
     */
    public void add(int val, int loc) {
        SingleLinkedListNode temp = headNode;
        for (int i = 1; i < loc; i++) {
            if (temp.nextNode == null)
                break;
            temp = temp.nextNode;
        }
        SingleLinkedListNode newNode = new SingleLinkedListNode(val);
        newNode.nextNode = temp.nextNode;
        temp.nextNode = newNode;
        headNode.val++;
    }

    /**
     * 删除节点
     *
     * @param loc 待节点位置
     */
    public void delete(int loc) {
        if (loc > headNode.val)
            throw new RuntimeException("超出链表长度");
        SingleLinkedListNode temp = headNode;
        for (int i = 1; i < loc; i++)
            temp = temp.nextNode;
        temp.nextNode = temp.nextNode.nextNode;
        headNode.val--;
    }

    /**
     * 修改某个节点的值
     *
     * @param val 新值
     * @param loc 修改节点的位置
     */
    public void update(int val, int loc) {

        SingleLinkedListNode temp = headNode.nextNode;
        for (int i = 1; i < loc; i++)
            temp = temp.nextNode;
        temp.val = val;

    }

    /**
     * 查询指定位置节点的值(-1返回末尾节点的值)
     *
     * @param loc 节点位置
     * @return 节点的值
     */
    public int find(int loc) {
        if (loc > headNode.val || loc == 0)
            throw new RuntimeException("输入位置错误");

        SingleLinkedListNode temp = headNode.nextNode;
        if (loc == -1)
            for (int i = 1; i < headNode.val; i++)
                temp = temp.nextNode;
        else
            for (int i = 1; i < loc; i++)
                temp = temp.nextNode;

        return temp.val;

    }

    /**
     * 查询倒数第K个节点的值
     *
     * @param lastLoc 倒数节点位置
     * @return 节点的值
     */
    public int findLast(int lastLoc) {
        if (lastLoc > headNode.val || lastLoc <= 0)
            throw new RuntimeException("超出链表长度");
        SingleLinkedListNode temp = headNode.nextNode;
        for (int i = 1; i <= getLength() - lastLoc; i++)
            temp = temp.nextNode;
        return temp.val;
    }


    /**
     * 输出当前链表所有元素
     */
    public void show() {
        if (headNode.nextNode == null) {
            System.out.println("链表为空!");
            return;
        }
        SingleLinkedListNode temp = headNode.nextNode;
        while (temp != null) {
            System.out.print(temp.val + "  ");
            temp = temp.nextNode;
        }
        System.out.println();
    }

    /**
     * 从尾到头输出链表
     */
    public void showReverse() {
        if (headNode.val == 0)
            System.out.println("链表为空");
        SingleLinkedListNode temp = headNode.nextNode;
        //使用栈保存当前元素的值
        Stack<Integer> integers = new Stack<>();
        //将链表元素压栈
        for (int i = 0; i < getLength(); i++) {
            integers.push(temp.val);
            temp = temp.nextNode;
        }
        //出栈
        while (!integers.empty())
            System.out.print(integers.pop() + "  ");
        System.out.println();
    }

    /**
     * 获取列表长度
     *
     * @return 列表长度
     */
    public int getLength() {
        return headNode.val;
    }

    /**
     * 将链表反转(头插法)
     *
     * @return 新的反转链表
     */
    public SingleLinkedList reverse() {
        SingleLinkedList reverseList = new SingleLinkedList();
        if (headNode.val == 1)
            reverseList.add(headNode.nextNode.val);
        if (headNode.val > 1) {
            SingleLinkedListNode temp = headNode;
            for (int i = 0; i < getLength(); i++) {
                temp = temp.nextNode;
                reverseList.add(temp.val, 1);
            }
        }
        return reverseList;
    }

    /**
     * 将另一条链表合并(合并入队尾)
     *
     * @param singleLinkedList 待并入链表
     */
    public void mergeList(SingleLinkedList singleLinkedList) {
        while (singleLinkedList.getLength() == 0)
            return;
        SingleLinkedListNode temp = headNode;
        for (int i = 0; i < getLength(); i++)
            temp = temp.nextNode;
        temp.nextNode = singleLinkedList.headNode.nextNode;
    }
}


/**
 * 节点类,每一个对象就是单链表的一个节点
 */
class SingleLinkedListNode {
    public int val;
    public SingleLinkedListNode nextNode;

    public SingleLinkedListNode(int val) {
        this.val = val;
        this.nextNode = null;
    }

    @Override
    public String toString() {
        return "SingleLinkedListNode{" +
                "val=" + val +
                ", nextNode=" + nextNode +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值