java单链表

class Listnode{
    public int data;
    public Listnode next;
    public Listnode(int data){
        this.data = data;
        this.next = null;
    }
}
class MyList{
    public Listnode head;
   /* public MyList() {
        this.head = null;
    }*/

    //头插法
    public void addFirst(int data){
        Listnode node = new Listnode(data);
        if(this.head == null){
            this.head = node;
        }else{
            node.next = this.head;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        Listnode node = new Listnode(data);
        Listnode prev = this.head;
        if(this.head == null) {//判断是否是第一次插入
            this.head = node;
        }while (prev.next != null){//找尾巴
            prev = prev.next;
        }
        prev.next = node;//进行插入
    }

    public Listnode Serchindex(int index){//找到index-1位置的节点  返回当前节点的引用
        Listnode prev = this.head;
        for (int i = 0; i < index -1 ; i++) {
            prev = prev.next;
        }return prev;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        Listnode node = new Listnode(data);
        if (index < 0 || index > size()){
            System.out.println("超范围插入");
            return false;
        }
        if (index == 0){
            addFirst(data);
            return true;
        }
        Listnode prev = Serchindex(index);
        node.next = prev.next;
        prev.next = node;
        return true;
    }
    //打印单链表
    public void display(){
        Listnode cur = this.head;
         if (this.head == null){
             return;
         }
         while(cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println(" ");
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Listnode prev = this.head;
        while(prev != null){
            if(prev.data == key){
                return true;
            }
            prev = prev.next;
        }
        return false;
    }
    public Listnode contains2(int key){
        Listnode cur = this.head;
        while (cur != null) {
            if(cur.data == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //得到单链表的长度
    public int size(){
        Listnode prev = this.head;
        int count = 0;
        while(prev != null){
            count++;
            prev = prev.next;
        }
        return count;
    }
    
    public Listnode Serchprev(int key){
        Listnode prev = this.head;
        while(prev.next != null){
            if (prev.next.data == key){
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
      //删除第一次出现关键字为key的节点
    public void remove(int key){
        //Listnode prev = this.head;
        if(this.head.data == key){//1.判断是否是头结点
            this.head = this.head.next;
            return;
        }
        Listnode prev = Serchprev(key);//2.找到删除的节点的前驱  如果找不到  返回null
        if (prev == null){
            System.out.println("没有你要找的节点");
            return;
        }
        Listnode node = prev.next;
        prev.next = node.next;//进行删除
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Listnode prev = this.head;
        Listnode cur = this.head.next;
        while(cur != null){
            if (cur.data == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后处理
        if(this.head.data == key){
            this.head = this.head.next;
        }

    }
    public void clear(){
        this.head = null;
    }
}

public class Test3 {
    public static void main(String[] args) {
       MyList myList = new MyList();
       myList.addFirst(18);
        myList.addFirst(3);
        myList.addFirst(15);
        myList.addFirst(18);
        myList.addFirst(46);
        myList.display();
        myList.addIndex(7,99);
        myList.display();
        boolean flg = myList.contains(798);
        System.out.println(flg);
        int a = myList.size();
        myList.removeAllKey(18);
        myList.display();
        System.out.println(a);
        myList.remove(18);
        myList.display();
       // myList.clear();
        //myList.display();
        Listnode node = myList.contains2(15);
        System.out.println(node);//5节点的地址*/

    }
}
========================================
输出结果:
46 18 15 3 18  
超范围插入
46 18 15 3 18  
false
46 15 3  
5
没有你要找的节点
46 15 3  
Listnode@1540e19d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值