手动实现一个链表

/**
 * @author xwm
 * @email 2825902051@qq.com
 * @date 2021/4/28 0028
 * @time 14:34
 */
public class MyLinkedList<T> {
    private Node head;
    private int size;

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public void setSize(int size) {
        this.size = size;
    }

    class Node<T> {
        private T t;
        private Node next;

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        public Node(T t, Node next) {
            this.t = t;
            this.next = next;
        }

        public Node() {
        }

        @Override
        public String toString() {
            if(this==head.next){
                return "LinkedList{"+this.t;
            }
            if(this.next==null){
                return this.t+"}";
            }
            return " "+this.t+" ";
        }
    }
    public MyLinkedList(){
        head=new Node(null,null);
        size=0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size==0;
    }

    public void add(T t){
        if(size==0){
            head.next=new Node(t,null);
        }
        else{
            Node n=head;
            while (n.next!=null){
                n=n.next;
            }
            n.next=new Node(t,null);
        }
        size++;
    }

    public T getByIndex(int index){
        if(index>size-1){
            throw new IndexOutOfBoundsException();
        }
        Node n=head;
        for(int i=0;i<index;i++){
            n=n.next;
        }
        return (T) n.t;
    }

    public MyLinkedList reverse(){
       Node[] arr=new Node[this.size];
       Node node=this.head.next;
       int index=arr.length-1;
       while (node!=null){
           arr[index--]=node;
           node=node.next;
       }
       Node head=new Node(null,null);
       Node node1=head;
       for(int i=0;i<arr.length;i++){
           node1.next=arr[i];
           node1=node1.next;
       }
       this.setHead(head);
       return this;
    }

    public void addFirst(T t){
        if(size==0){
            head.next=new Node(t,null);
        }
        else {
            head.next=new Node(t,head.next);
        }
        size++;
    }

    /**
     * 根据索引插入
     * @param t
     * @param index
     */
    public void addByIndex(T t,int index){
        if(index>size-1){
            throw new IndexOutOfBoundsException();
        }
        Node n=head.next;
        for(int i=0;i<index-1;i++){
            n=n.next;
        }
        n.next=new Node(t,n.next);
        size++;
    }

    public boolean remove(T t) throws Exception {
        if(t==null){
            throw new NullPointerException();
        }
        Node n=head;
        while (true){
            if(n.next.t==t){
                Node mid=n.next;
                n.next=mid.next;
                size--;
                mid.next=null;
                return true;
            }
            if(n.next==null){
                return false;
            }
            n=n.next;
        }
    }

    public T removeByIndex(int index){
        if(index>size-1){
            throw new IndexOutOfBoundsException();
        }
        Node n=head.next;
        for(int i=0;i<index-1;i++){
            n=n.next;
        }
        Node mid=n.next;
        n.next=mid.next;
        size--;
        T t= (T) mid.t;
        mid.next=null;
        return (T) t;
    }

    public void bianLi(){
        Node n=head;
        for(int i=0;i<size;i++){
            System.out.print(n.next+" ");
            n=n.next;
        }
    }


    public boolean contain(T t) throws Exception {
        if(t==null){
            throw new NullPointerException();
        }
        else {
            Node n=head;
            while (head!=null){
                if(head.t==t){
                    return true;
                }
                head=head.next;
            }
            return false;
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小呆萌熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值