链表的基本操作(单向,不循环,不带头)

class Node5{
    public  int  data;
    public Node5 next;

    public  Node5 (int data){
        this.data = data;
    }
}
public class MyTryTry {
    public Node5 head ;
    //头插入;
    public void addFrist(int data){
        Node5 node = new Node5(data);
        node .next = this .head;
        this.head = node;
    }
    //打印链表
    public void disPlay (){
        Node5 cur = this.head;
        while (cur != null){
            System.out.print( cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //尾插入
    public void addLast(int data){
        Node5 node = new Node5(data);
        if(this.head == null){
            addFrist(data);
            return;
        }
        Node5 cur = this.head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //打印链表长度
    public int size(){
        int count = 0;
        Node5 cur = this.head;
        while (cur != null){
            count ++;
            cur = cur.next;
        }
        return count ;
    }
    //任意位置插入元素
    public void  addIndex (int index , int data){
        Node5 note = new Node5(data);
        if (index < 0 || index > this.size()){
            System.out.println("插入错误");
            return;
        }
        if (index == 0){
            addFrist(data);
        }
        Node5 cur = reaschPrev(index);
        note.next = cur .next;
        cur.next = note;
    }
    // 寻找插入数字的节点;
    public Node5  reaschPrev( int index){
        Node5 cur = this.head;
        int count = 0;
        while (count < index - 1){
            cur = cur.next;
            count ++ ;
        }
        return cur;
    }
    //寻找关键字key是否存在;
    public  boolean contains(int key){
        Node5 cur = this.head;
        while (cur != null){
            if (cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return  false;
    }
    //删除第一个关键字key
    public void reMove(int key){
        if(this.head.data == key){
            this.head = this.head.next;//删除头数字,这个数字的next就是ta的头
            return;
        }
        Node5 cur = reasechPrmv2(key);
        if(cur == null){
            return;
        }
        Node5 asc = cur.next;
        cur.next = asc.next;
    }
    //寻找key的前驱
    public Node5 reasechPrmv2(int key){
        Node5 cur = this.head;
        while (cur.next != null){
            if (cur.next.data == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    public static void main (String[] args){
        MyTryTry myTryTry = new MyTryTry();
        myTryTry.addFrist(1);
        myTryTry.addFrist(4);
        myTryTry.addFrist(5);
        myTryTry.disPlay();
        myTryTry.addLast(6);
        myTryTry.addLast(9);
        myTryTry.disPlay();
        System.out.println(myTryTry.size());
        myTryTry.addIndex(3,5);
        myTryTry.addIndex(0,7);
        myTryTry.addIndex(10,5);
        myTryTry.disPlay();
        System.out.println(myTryTry.contains(10));
        System.out.println(myTryTry.contains(9));
        myTryTry.reMove(9);
        myTryTry.reMove(5);
        myTryTry.reMove(20);
        myTryTry.disPlay();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值