Java--单链表

Java–单链表

上篇:Java–顺序表:https://blog.csdn.net/qq_42701294/article/details/103756662

结点类

package pers.xu.ds2.linertable.node;

/**
 * @author a_apple
 * @create 2019-11-13 22:52
 */
public class Node {

    public Object data;
    public Node next;

    public Node(){}
    public Node(Object data) {
        this.data = data;
    }
}

单链表类

package pers.xu.ds2.linertable;

import pers.xu.ds2.List;
import pers.xu.ds2.linertable.node.Node;

/**单链表实现
 * @author a_apple
 * @create 2019-11-13 22:51
 */
public class SingleLinkedList implements List {

    //头结点
    private Node head;
    //链表内元素的个数
    private int size;

    public SingleLinkedList(){
        //初始化头结点
        head = new Node();
        size = 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int index) {
        Node p = head;
        for (int i = 0; i <= index; i++) {
            p = p.next;
        }
        return p.data;
    }

    @Override
    public boolean isEmpty() {
        return head.next == null;
    }

    @Override
    public boolean contains(Object e) {
        Node p = head.next;
        if(isEmpty()){
            return false;
        }
        while (p != null){
            if(p.data.equals(e)){
                return true;
            }
            p = p.next;
        }
        return false;
    }

    @Override
    public int indexOf(Object e) {
        if(isEmpty()){
            return -1;
        }
        int i = 0;
        Node p = head.next;
        while (p != null){
            if(p.data.equals(e)){
                return i;
            }
            p = p.next;
            i++;
        }
        return i;
    }

    @Override
    public void add(int i, Object e) {
        //i的有效判断
        if(i<0 || i>size){
            throw new RuntimeException("数组越界异常:"+i);
        }
        //创建新节点,赋值
        Node node = new Node();
        node.data = e;

        //遍历到第i个位置
        Node p = head;
        for (int j = 0; j < i; j++) {
            p = p.next;
        }
        node.next = p.next;
        p.next = node;
        //size++
        size++;
    }

    @Override
    public void add(Object e) {
        this.add(size,e);
    }

    @Override
    public boolean addBefore(Object obj, Object e) {
        //先找到那个元素的坐标
        int index = indexOf(obj);
        //使用add(i,e)
        if(index >0){
            add(index,e);
            return true;
        }
        return false;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        //先找到那个元素的坐标
        int index = indexOf(obj);
        //使用add(i,e)
        if(index >0){
            add(index+1,e);
            return true;
        }
        return false;
    }

    @Override
    public Object remove(int i) {
        if (i<0 || i>size || isEmpty()){
            throw new RuntimeException("数组越界异常");
        }
        Node p = head.next;
        Node pre = head;
        //找到前驱
        for (int j = 0; j < i; j++) {
            pre = p;
            p = p.next;
        }
        if(p != null){
            pre.next = p.next;
            //size--
            size--;
            return p.data;
        }
        return null;
    }

    /**
     * 删除某个元素
     * @param obj
     * @return
     */
    @Override
    public Object remove(Object obj) {
        if(isEmpty()){
            throw new RuntimeException("数组越界异常");
        }
        Node p = head.next;
        Node pre = head;
        while (p != null){
            if(p.data.equals(obj)){
                pre.next = p.next;
                size--;
                return p.data;
            }
            pre = p;
            p=p.next;
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node p = head.next;
        for (int i = 0; i < size; i++) {
            if(i != size-1){
                sb.append(p.data).append(",");
            }else {
                sb.append(p.data);
            }
            p = p.next;
        }
        sb.append("]");
        return sb.toString();
    }
}

测试demo

package pers.xu.ds2.test;

import pers.xu.ds2.linertable.SingleLinkedList;

/**
 * @author a_apple
 * @create 2019-11-13 23:10
 */
public class SingleLLTest {

    public static void main(String[] args) {

        SingleLinkedList list = new SingleLinkedList();
        list.add("a");
        list.add("b");
        list.add(4);
        list.add("h");

        System.out.println(list);

        list.add(1,"c");
        System.out.println(list);
        System.out.println("------------------------");

        Object o = list.get(2);
        System.out.println("坐标为2的元素= "+o);

        System.out.println("isEmpty: "+list.isEmpty());

        System.out.println("------------------------");
        System.out.println(list);
        System.out.println("------------------------");
        System.out.println("contains 'b' ?: "+list.contains("b"));
        System.out.println("contains 'e' ?: "+list.contains("e"));


        System.out.println("------------------------");
        System.out.println(list);
        System.out.println("------------------------");
        System.out.println("indexOf 'h' = "+list.indexOf("h"));
        System.out.println("indexOf 'b' = "+list.indexOf("b"));

        System.out.println("-------------------------");
        //System.out.println("remove 坐标为8的元素 = "+list.remove(8));
        System.out.println("remove 坐标为2的元素 = "+list.remove(2));
        System.out.println(list);
        System.out.println("-------------------------");

        System.out.println("在元素c后面添加'A'");
        boolean b = list.addAfter("c", "A");
        System.out.println(list);
        System.out.println("--------------------------");

        System.out.println("在元素h前面添加'B'");
        boolean d = list.addBefore("h", "B");
        System.out.println(list);
        System.out.println("--------------------------");

        System.out.println("size = "+list.size());
        System.out.println("--------------------------");

        System.out.println("删除元素'A' :"+list.remove("A"));
        System.out.println(list);
        System.out.println("---------------------------");
    }
}

输出结果:

[a,b,4,h]
[a,c,b,4,h]
------------------------
坐标为2的元素= b
isEmpty: false
------------------------
[a,c,b,4,h]
------------------------
contains 'b' ?: true
contains 'e' ?: false
------------------------
[a,c,b,4,h]
------------------------
indexOf 'h' = 4
indexOf 'b' = 2
-------------------------
remove 坐标为2的元素 = b
[a,c,4,h]
-------------------------
在元素c后面添加'A'
[a,c,A,4,h]
--------------------------
在元素h前面添加'B'
[a,c,A,4,B,h]
--------------------------
size = 6
--------------------------
删除元素'A' :A
[a,c,4,B,h]
---------------------------

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值