无头单向非循环链表及部分接口(Java实现)

12 篇文章 0 订阅
5 篇文章 0 订阅

链表

存储在不连续的内存空间上 更擅长插入和删除

此处我们在结尾以链表的插入接口为例 用图的形式详解该操作

public class TestLinkedlist {
    public static void main(String[] args) {
        testAddFirst();
        testAddLast();
        testAddIndex();
        testContains();
        testRemove();
    }

    public static void testAddFirst() {
        System.out.println("测试头插");
        LinkedList list = new LinkedList();
        list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        list.addFirst(4);
        list.display();
    }

    public static void testAddLast() {
        System.out.println("测试尾插");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.display();
    }

    public static void testAddIndex() {
        System.out.println("测试插入任意位置");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addIndex(2,5);

        list.display();
    }

    public static void testContains() {
        System.out.println("测试是否存在");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.contains(3);

        list.display();
    }

    public static void testRemove() {
        System.out.println("测试删除链表中的元素");
        LinkedList list = new LinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.remove(3);

        list.display();
    }
}

 

import com.sun.org.apache.bcel.internal.generic.LNEG;
import sun.awt.image.ImageWatched;

class LinkedNode {
    public int data = 0;
    public LinkedNode next = null;
    public  LinkedNode(int data) {
        this.data = data;
    }
}

public class LinkedList {
    //头结点
    private LinkedNode head = null;

    public void display() {
        //打印
        System.out.print("[");
        for (LinkedNode node = this.head;
             node != null;
             node = node.next) {
            System.out.print(node.data);
            if (node.next != null) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    //头插
    public void addFirst(int elem) {
        //先创建一个节点 让这个节点的值就是elem
        LinkedNode node = new LinkedNode(elem);
        if(this.head == null) {
            //链表为空时
            this.head = node;
            return;
        }
        //如果不是空链表 就把新的节点放到链表的开始位置
        node.next = head;
        this.head = node;
        return;

    }

    //尾插
    public void addLast(int elem) {
        LinkedNode node = new LinkedNode(elem);
        if (this.head == null) {
            //空链表
            this.head = node;
            return;
        }
        //非空 需要先找到最后一个节点
        LinkedNode cur = this.head;
        //循环结束 cur就是最后一个结点
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入
    public void addIndex(int index,int elem) {
        LinkedNode node = new LinkedNode(elem);

        //1.判断
        int len = size();
        if (index < 0 || index > len) {
            return;
        }
        //2.头插
        if (index == 0) {
            addFirst(elem);
            return;
        }
        //3.尾插
        if (index == len) {
            addLast(elem);
            return;
        }
        //4.中间位置 需要找到 index - 1 这个位置
        LinkedNode prev = getIndexPos(index - 1);
        node.next = prev.next;
        prev.next = node;
    }



    private LinkedNode getIndexPos(int index) {

        LinkedNode cur = this.head;
        for (int i = 0;i < index;i++) {
            cur = cur.next;
        }
        return cur;
    }

    public int size() {
        int size = 0;
        for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
            size++;
        }
        return size;
    }

    //判断是否存在
    public boolean contains(int toFind) {
        for (LinkedNode cur = this.head;cur != null;cur = cur.next) {
            if (cur.data == toFind) {
                return true;
            }
        }
        return false;
    }

    //删除第一次出现的元素
    public void remove(int toRemove) {
        //1.考虑空链表的情况
        if (head == null) {
            return;
        }
        //2.先判断是否是头结点
        if (head.data == toRemove) {
            this.head = this.head.next;
            return;
        }
        //3.删除中间结点 找到前一个结点
        LinkedNode prev = searchPrev(toRemove);
        prev.next = prev.next.next;
    }

    private LinkedNode searchPrev(int toRemove) {
        if (this.head == null) {
            return null;
        }
        LinkedNode prev = this.head;
        while (prev.next != null) {
            if (prev.next.data == toRemove) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

}

链表插入(int index , int elem):

我们最基础的思想就是对输入进行判断,即判断要插入位置的情况

1.判定是否有效 

即 index < 0 || index >len

2.头插

3.尾插

4.中间位置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值