java 单链表无头不循环的实现

class Node{
     public int val;
     public Node next ;
    public Node(int val){
         this.val=val;
     }
}
public class SingleLinkedList {//单链表
    public Node head=null;
    public int usedSize=0;
    public void  myToString(){//打印
        Node cur=this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
    public boolean contains(int key){//是否存在关键字key
        Node cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    public int size(){//求长度
        return usedSize;
    }
    public int size1(){
        Node cur=this.head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    public void addFirst(int val){//头插法
        Node node=new Node(val);
        if(this.head==null){
            this.head=node;
        }
        else {
            node.next = head;
            this.head = node;
        }
        this.usedSize++;
    }
    public void addLast(int val){//尾插法
        Node node=new Node(val);
        if(this.head==null){
            this.head=node;
        }
        else{
            Node cur=this.head;
            while(cur.next!=null){
                cur=cur.next;
            }
            cur.next=node;
        }
        usedSize++;
    }
    public void addIndex(int index,int val){//在index位置插
        Node node=new Node(val);
         if(index==0){
            addFirst(val);
        }
        else if(index==usedSize){
            addLast(val);
        }
        else if(index<0||index>usedSize){
            return;
         }
        else
        {
            Node cur = this.head;
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
          node.next=cur.next;
            cur.next=node;
            usedSize++;
        }
    }
    public void remove(int key){
        if(head==null){//删除第一个key
            return;
        }
        if(head.val==key){
            this.head=head.next;
            usedSize--;
            return;
        }
       Node cur= this.prev(key);
        if(cur==null){
            System.out.println("没找到");
            return;
        }
        cur.next=cur.next.next;
        this.usedSize--;
    }
    public Node prev(int key){//查找key的前驱
        Node cur=this.head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    public void allRemove(int key){
        if(this.head==null){//key全部删除
            return;
        }
        Node prev=this.head;
        Node cur=this.head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
                this.usedSize--;
            }
            else{
                prev=cur;
                cur=cur.next;
            }
        }
  if(this.head.val==key){
      this.head=head.next;
      this.usedSize--;
  }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值