java 手写单链表。

学习数据结构的最好方法就是手动写一次,这样才能更好的理解原理。

public class Linker {
    private final int FLAG = -9999;//标记为头元素。
    private int value;
    private Linker next;

    public Linker() {
        super();
        this.value = FLAG;
    }

    @Override
    public String toString() {
        return "Linker{" +
                "FLAG=" + FLAG +
                ", value=" + value +
                ", next=" + next +
                '}';
    }

    public int getFLAG() {
        return FLAG;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Linker getNext() {
        return next;
    }

    public void setNext(Linker next) {
        this.next = next;
    }
    /*
    *<span style="font-family: Arial, Helvetica, sans-serif;">添加方法,把头元素的指针赋值给插入元素的指针。这样做有个问题就是插入进来的数据会始终排在第一个。所以会造成倒序。</span>
    */
    public boolean add(int value){
        Linker Linker = new Linker();
        Linker.setValue(value);
        Linker.next = this.next;
        this.next = Linker;
        return true;
    }

    public boolean remove(int index){
        Linker temp = this.next;
        int count = 0;
        Linker prev = this;
        while (temp != null && count <= index) {
            if (count != index) {
                count++;
                prev = temp;
                temp = temp.next;
                continue;
            }else {
                prev.next = temp.next;
                temp.next = null;
                return true;
            }
        }
        return false;
    }

    public boolean set(int index,int newValue){
        Linker temp = this.next;
        int count = 0;

        while (temp != null && count <= index) {
            if(count!=index){
                temp = temp.next;
                count++;
                continue;
            }else {
                temp.setValue(newValue);
                return true;
            }
        }
        if (index > count)
            throw new IndexOutOfBoundsException();
        return false;
    }

    public Linker findAll(){
        Linker temp = this.next;
        while (temp != null) {
            System.out.println(temp.value);
            temp = temp.next;
        }
        return temp;
    }

    public int get(int index){
        Linker temp = this.next;
        int count = 0;
        while (temp != null && count <= index) {
            if (count != index) {
                count++;
                temp = temp.next;
                continue;
            } else {
                return temp.value;
            }
        }
        if (index > count)
            throw new IndexOutOfBoundsException();

        return -9999;
    }

    public static void main(String[] args) {
        Linker Linker = new Linker();
        Linker.add(1);
        Linker.add(2);
        Linker.add(3);
        Linker.add(4);
        Linker.add(5);
        Linker.add(6);
//        Linker.remove(0);
        Linker.set(9, 8);
        Linker.findAll();
//        System.out.println(Linker.get(0));

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值