Java实现单链表头插法、尾插法

Java实现单链表头插法、尾插法

直接看代码

package com.zqq;

public class SingleLinkList {
    public static void main(String[] args) {
        // 头插法
        headInsert();
        // 尾插法
        rearInset();
        // 头插法-无头指针
        headInsertNoHead();
        // 尾插法-无头指针
        rearInsetNohead();
    }

    /**
     * 头插法插入一个数组a
     * s1:初始化空链表
     * s2: 创建新结点,并赋值
     * s3: 将新结点放到头结点后面
     */
    public static void headInsertNoHead() {
        int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        // s1:创建空链表
        ListNode linkList = null;
        for (int i : a) {
            // s2:创建新结点,并赋值数据域
            ListNode newNode = new ListNode(i);
            // s3:将新结点插入头结点后面
            newNode.setNext(linkList);
            linkList = newNode;
        }
        // 遍历
        iteratorList(linkList);
    }

    /**
     * 头插法插入一个数组a
     * s1:初始化空链表
     * s2: 创建新结点,并赋值
     * s3: 将新结点放到头结点后面
     */
    public static void headInsert() {
        int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        // s1:创建空链表
        LinkList linkList = new LinkList();
        for (int i : a) {
            // s2:创建新结点,并赋值数据域
            ListNode newNode = new ListNode(i);
            // s3:将新结点插入头结点后面
            newNode.setNext(linkList.getHead().getNext());
            linkList.getHead().setNext(newNode);
        }
        // 遍历
        iteratorList(linkList.getHead().getNext());
    }

    /**
     * 尾插法插入一个数组a
     * s1:初始化空链表,尾结点指向空链表头结点
     * s2: 创建新结点,并赋值
     * s3: 将新结点放到尾结点r后面
     * s4: 重新指定尾结点
     */
    public static void rearInsetNohead() {
        int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        // s1:创建空链表及尾结点
        ListNode linkList = null;
        ListNode rear = null;
        for (int i : a) {
            if (linkList == null) {
                linkList = new ListNode(i);
                rear = linkList;
            } else {
                // s2:创建新结点,并赋值数据域
                ListNode newNode = new ListNode(i);
                // s3:将新结点插入尾结点后面
                rear.setNext(newNode);
                // S4: 重新指向尾结点
                rear = newNode;
            }
        }
        // 遍历
        iteratorList(linkList);
    }

    /**
     * 尾插法插入一个数组a
     * s1:初始化空链表,尾结点指向空链表头结点
     * s2: 创建新结点,并赋值
     * s3: 将新结点放到尾结点r后面
     * s4: 重新指定尾结点
     */
    public static void rearInset() {
        int[] a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        // s1:创建空链表及尾结点
        LinkList linkList = new LinkList();
        ListNode rear = linkList.getHead();
        for (int i : a) {
            // s2:创建新结点,并赋值数据域
            ListNode newNode = new ListNode(i);
            // s3:将新结点插入尾结点后面
            rear.setNext(newNode);
            // S4: 重新指向尾结点
            rear = newNode;
        }
        // 遍历
        iteratorList(linkList.getHead().getNext());
    }

    /**
     * 遍历单链表
     *
     * @param listNode
     */
    private static void iteratorList(ListNode listNode) {
        if (listNode == null) {
            System.out.println("空链表");
        }
        StringBuilder result = new StringBuilder();
        while (listNode != null) {
            result.append(listNode.getData()).append(",");
            listNode = listNode.getNext();
        }
        System.out.println(result);
    }
}

class ListNode {
    private ListNode next;
    private int data;

    public ListNode(int data) {
        this.data = data;
    }

    public ListNode(ListNode next) {
        this.next = next;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}

class LinkList {
    private ListNode head;
    private ListNode rear;

    public ListNode getRear() {
        return rear;
    }

    public void setRear(ListNode rear) {
        this.rear = rear;
    }

    public LinkList() {
        // 初始化链表
        initLinkList();
    }

    private void initLinkList() {
        head = new ListNode(null);
    }

    public ListNode getHead() {
        return head;
    }

    public void setHead(ListNode head) {
        this.head = head;
    }
}

执行结果截图:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值