单双向链表的实现

无头单向链表:

class Node {
    public int val;
    public Node next;
    public Node (int val) {
        this.val = val;
    }
}

public class SingleLinkedList {
    Node head;
    public SingleLinkedList() {
        this.head = null;
    }
    public void addFirst(int val) {
        Node node = new Node(val);
        node.next = head;
        head = node;
    }
    public void addLast(int val) {
        Node cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        Node node = new Node(val);
        cur.next = node;
    }

    public boolean addIndex(int index, int val) {
        if(index == 0) {
            addFirst(val);
            return true;
        }
        if(index == size()) {
            addLast(val);
            return true;
        }
        if(index < 0 || index > size()) {
            return false;
        }
        Node cur = head;
        for(int i = 0; i < index; i++) {
            cur = cur.next;
        }
        Node node = new Node(val);
        node.next = cur.next;
        cur.next = node;
        return true;
    }
    public boolean contains (int val) {
        Node cur = this.head;
        while(cur != null) {
            if(cur.val == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public boolean remove(int val) {
        Node cur = this.head;
        Node pre = null;
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
                return true;
            }
            pre = cur;
            cur = cur.next;
        }
        return false;
    }


    public void removeAllKey (int val) {
        Node cur = this.head;
        Node prev = cur;
        if(head.val == val) {
            head = head.next;
        }
        while(cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
                continue;
            }
            prev = cur;
            cur = cur.next;
        }
    }
    public void clear() {
        this.head = null;
    }


    public int size() {
        Node cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
}

带头双向链表

package Java_0630.MyLinkedList.DoubleLinkedList;

class Node {
    int val;
    Node prev;
    Node next;

    public Node(int val) {
        this.val = val;
    }
}
public class DoubleLinkedList {
    Node pHead;
    Node head;
    Node last;

    public DoubleLinkedList() {
        pHead = new Node(0);
        pHead.next = head;
    }

    public void addFirtst(int val) {
        Node node = new Node(val);
        if(head == null) {
            this.last = node;
            this.head = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    public void addLast (int val) {
        Node node = new Node(val);
        if( this.head == null) {
            addFirtst(val);
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    public void addIndex(int index, int val) {
        if(index == 0) {
            addFirtst(val);
        } else if(index == size()) {
            addLast(val);
        } else if(index < 0 || index > size()) {
            return;
        }
        Node cur = this.head;
        for(int i = 0; i < index; i++) {
            cur = cur.next;
        }
        Node node = new Node(val);
        node.next = cur.next;
        node.prev = cur;
        cur.next.prev = node;
        cur.next = node;
    }



    public int size() {
        Node cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值