java实现单链表

public class SinglyLinkedList {
    private Node head;
    private int size;
    SinglyLinkedList(){
        head = null;
        size = 0;
    }
    SinglyLinkedList(Node head,int size){
        this.head = head;
        this.size = size;
    }
    //向表头插入一个值为x的结点
    public void insertHead(int x){
        insertNth(x,0);
    }
    //向表尾插入一个值为x的结点
    public void insert(int x){
        insertNth(x,size);
    }
    //检查position是不是在所给定的范围内
    public void checkBounds(int position,int low,int high){
        if (position < low || position > high){
            throw new IndexOutOfBoundsException(position+" ");
        }
    }
    //在position的位置插入一个数据为data的结点
    public void insertNth(int data,int position){
        checkBounds(position,0,size);
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
            size++;
            return ;
        }
        else if(position == 0){
            newNode.next = head;
            head = newNode;
            size++;
            return ;
        }
        Node cur = head;
        for (int i=0;i<position-1;i++){
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        size++;
    }

    public void deleteNth(int position){
        checkBounds(position,0,size-1);
        if(position == 0){
            Node destory = head;
            head = destory.next;
            destory = null;
            size--;
            return ;
        }
        Node cur = head;
        for(int i=0;i<position-1;i++){
            cur = cur.next;
        }
        Node destory = cur.next;
        cur.next = destory.next;
        destory = null;
        size--;
    }
    //删除一个头结点
    public void deleteHead(){
        deleteNth(0);
    }
    //删除一个尾结点
    public void delete(){
        deleteNth(size-1);
    }
    public void clear(){
        Node cur = head;
        while(cur != null){
            Node prev = cur;
            cur = cur.next;
            prev = null;
        }
        head = null;
        size = 0;
    }
    public Boolean isEmpty(){
        return (size == 0);
    }
    public int size(){
        return size;
    }
    public Node getHead(){
        return head;
    }
    public Boolean search(int key){
        Node cur = head;
        while(cur != null){
            if(cur.value == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    public int getNth(int index){
        checkBounds(index,0,size-1);
        Node cur = head;
        for (int i=0;i<index;i++){
            cur = cur.next;
        }
        return cur.value;
    }
    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner("->");
        Node cur = head;
        while(cur != null){
            joiner.add(cur.value+"");
            cur = cur.next;
        }
        return joiner.toString();
    }

    public static void main(String[] args) {
//        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
//        singlyLinkedList.insertHead(0);
//        singlyLinkedList.insertHead(1);
//        singlyLinkedList.insertHead(2);
//        singlyLinkedList.insertHead(3);
//        singlyLinkedList.insertHead(4);
//        singlyLinkedList.insert(90);
//        singlyLinkedList.deleteHead();
//        System.out.println(singlyLinkedList.size());
//        System.out.println(singlyLinkedList.getHead().value);
//        System.out.println(singlyLinkedList.toString());
    }
}
class Node{
    int value;
    Node next;
    Node(){};
    Node(int value){
        this.value = value;
    }
    Node(int value,Node next){
        this.value = value;
        this.next = next;
    }
}

 说实话,代码是我抄的,不过我现在会了。

posted @ 2021-03-25 19:14  吃心王  阅读(85)  评论(0)    收藏  举报