Java单链表插入基本操作(头插法、尾插法、任意位置插入法)

17 篇文章 1 订阅

一、单链表的头插法 

1、创建一个NODE节点的类,写一个public void addFirst()的函数来实现链表的头部插入,最后写一个public void display()的函数实现链表的打印输出

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @User:Mingaho
 * @Date:2021/04/10
 * @Time:16:50
 */

class NODE {      //构造一个节点的类
    public int data;
    public NODE next;
    public NODE(int data) {
        this.data = data;
        this.next = null;
    }
}
public class LinkedList {
    public NODE head;  //保存单链表的头节点的引用  代表的是整个链表的头 所以定义在这个地方
    //头插法
    public void addFirst(int data) {
        NODE node = new NODE(data);
        if(this.head == null) {
            this.head = node;
            return;
        }
        node.next = this.head;
        this.head = node;
    }


    //display  打印该链表
    public void display() {
        if(this.head == null) {
            System.out.println("The List is empty.");
            return;
        }
        NODE cur = this.head;
        while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }




    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addFirst(12);    //进行头插
        linkedList.addFirst(13);
        linkedList.addFirst(14);   
        linkedList.addFirst(15);
        linkedList.addFirst(16);
        linkedList.display();
    }
}

2、以下是头插法代码运行结果 

二、单链表的尾插法

1、创建一个NODE节点的类,写一个public void addLast()的函数来实现链表的尾部插入,最后写一个public void display()的函数实现链表的打印输出

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @User:Mingaho
 * @Date:2021/04/10
 * @Time:16:50
 */

class NODE {      //构造一个节点的类
    public int data;
    public NODE next;
    public NODE(int data) {
        this.data = data;
        this.next = null;
    }
}
public class LinkedList {
    public NODE head;  //保存单链表的头节点的引用  代表的是整个链表的头 所以定义在这个地方

    //尾插法
    public void addLast(int data) {
        NODE node = new NODE(data);
        if(this.head == null) {
            this.head = node;
            return;
        }
        NODE cur = this.head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    
    //display  打印该链表
    public void display() {
        if(this.head == null) {
            System.out.println("The List is empty.");
            return;
        }
        NODE cur = this.head;
        while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();   
        linkedList.addLast(11);       //进行尾插
        linkedList.addLast(10);
        linkedList.addLast(9);   
        linkedList.addLast(8);
        linkedList.display();

    }
}

2、以下是尾插法的代码运行结果 

 

 三、单链表任意位置的插入

 1、创建一个NODE节点的类,利用public NODE searchIndex()函数先找到插入位置的前驱节点,再利用public void addIndex()的函数来实现链表的任意插入,最后写一个public void display()的函数实现链表的打印输出

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @User:Mingaho
 * @Date:2021/04/10
 * @Time:16:50
 */

class NODE {      //构造一个节点的类
    public int data;
    public NODE next;
    public NODE(int data) {
        this.data = data;
        this.next = null;
    }
}
public class LinkedList {
    public NODE head;  //保存单链表的头节点的引用  代表的是整个链表的头 所以定义在这个地方

    //查找index的前驱位置
    public NODE searchIndex(int index) {
        if(index < 0 || index > this.size()) {
            throw new RuntimeException("index's location is not right.");
        }
        NODE prev = this.head;
        //NODE cur = this.head.next;
        while(index > 1) {
            prev = prev.next;
            index--;
        }
        return prev;
    }
    //任意位置插入data
    public void addIndex(int index, int data) {
        NODE node = new NODE(data);
        if(this.head == null) {
            this.head = node;
            return;
        }
        NODE prev = searchIndex(index);
        //NODE cur = prev.next;
        node.next = prev.next;
        prev.next = node;
    }

    //display  打印该链表
    public void display() {
        if(this.head == null) {
            System.out.println("The List is empty.");
            return;
        }
        NODE cur = this.head;
        while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }




    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();

        linkedList.addIndex(0,90);    //index位置插入data数据
        linkedList.addIndex(1,91);
        linkedList.addIndex(2,92);
        linkedList.addIndex(3,93);
        linkedList.addIndex(4,94);
        linkedList.addIndex(5,95);
        linkedList.addIndex(6,96);

        linkedList.addIndex(6,100);
        linkedList.addIndex(6,123);
        linkedList.addIndex(6,234);
        linkedList.display();
    }
}

2、以下是任意位置插入的代码运行结果 

 

 

 

 

  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值