Java之链表的基本操作

1.相关基本操作

//实现链表的基本操作
public class Mysiglelist {


    //节点类
    static class LNode{
        int val;
        LNode next;

        public LNode(int data){
            this.val=data;
        }
    }
    public LNode head;


    /**
     * 头插法
     */

    public void addFirst(int data){
        LNode node=new LNode(data);
        node.next=head;
        head=node;
    }


    /**
     * 尾插法
     */
    public void addLast(int data){
        LNode node=new LNode(data);
        LNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    /**
     * 打印
     */
    public void show(){
        LNode cur=head;
        while(cur!=null){
            System.err.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }


    //计算链表长度
    public int size(){
        LNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    
    /**
     * 查询是否包含key元素
     */
    public boolean contain(int key){
        LNode cur=head;
        while (cur.next!=null){
            if(cur.val==key){
                return true;
            }
        }
        return false;
    }

    /**
     * 指定位置插入data
     */
    public void addIndex(int data,int index){
        //1.插入位置为1,为头插法
        if(index-1==0){
            addFirst(data);
            return;
        }
        //2.插入位置等于链表长度,为尾插法
        if(index==size()){
            addLast(data);
            return;
        }
        //3.插入位置错误大于链表长度或者小于0
        if(index<0||index>size()){
            return;
        }
        //3.链表中插入
        LNode node=new LNode(data);
        LNode fast=head;
        LNode slow=head;
        while(index-1!=0){
            fast=fast.next;
            while (index-2!=0){
                slow=slow.next;
            }
            index--;
        }
        node.next=fast;
        slow.next=node;
    }

    /**
     * 删除指定第一次出现的元素key
     */
    public void delVal(int key){
        LNode cur=head;
        //1.第一个元素就是要删除的元素
        if(head.val==key){
            head=head.next;
        }
        while (cur!=null){
            //2.最后一个元素是要删除的元素
            if(cur.next.next==null&&cur.next.val==key){
                cur.next=null;
            }
            //3.删除的元素不在链表的头尾两端
            if(cur.val==key){
                cur.next=cur.next.next;
            }
            cur=cur.next;
        }
    }

    //头删
    public void delFirst(){
        head=head.next;
    }

    //尾删
    public void delLast(){
        LNode cur=head;
        while(cur.next.next!=null){
            cur=cur.next;
        }
        cur.next=null;
    }

    /**
     * 删除指定位置元素
     */
    public void deliIndex(int index){
        //1.删除第一个元素,为头删
        if(index==1){
            delFirst();
            return;
        }
        //2.删除最后一个元素,为尾删
        if(index==size()){
            delLast();
            return;
        }
        //3.删除的元素在链表中间
        LNode fast=head.next;
        LNode slow=head;
        while(index-1!=0){
            fast=fast.next;
            while(index-2!=0){
                slow=slow.next;
            }
            index--;
        }
        slow.next=fast;
    }
}

2.操作的调用

public class Test {
    public static void main(String[] args) {
        Mysiglelist mysiglelist=new Mysiglelist();
        mysiglelist.addFirst(1);
        mysiglelist.addFirst(2);
        mysiglelist.addFirst(3);
        mysiglelist.addFirst(4);
        mysiglelist.addLast(66666);
        mysiglelist.show();
        mysiglelist.addIndex(10,1);
        mysiglelist.show();
        mysiglelist.deliIndex(1);
        mysiglelist.deliIndex(2);
        mysiglelist.delVal(66666);
        mysiglelist.show();
        mysiglelist.addIndex(10000,2);
        mysiglelist.show();
        System.out.println(mysiglelist.contain(4));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值