java代码:单链表的实现

1、代码

package LinkList;

public class Linklist {
    //定义节点,内部类只为其外部类使用
    //要创建嵌套类的对象,并不需要其外围类的对象,直接使用.next
    static class ListNode{
        int val;//数据域
        ListNode next;//指针,指向下一个结点的位置(java里面是引用数据类型——地址)
        public ListNode(int val){
            this.val = val;
        }
    }

    //队首指针和队尾指针
    ListNode head;
    ListNode tail;
    int size;//链表长度

    //定义链表(初始化链表)
    public Linklist(){
        head = null;
        tail = null;
        size = 0;
    }

    public static void main(String[] args) {
        //初始化链表
        Linklist newList = new Linklist();
        newList.appendIntail(5);
        newList.appendIntail(7);
        newList.appendIntail(4);
        newList.appendIntail(2);
        newList.printList();
        newList.appendInHead(30);
        newList.appendInHead(27);

        newList.insert(3,9);

        newList.printList();
        newList.deleteNode(9);
        newList.printList();

        newList.findInNumber(4);
        newList.findInNumber(1);

        newList.findInIndex(9);

        newList.update(11,2);
        newList.printList();
    }



    //增加新元素至表尾
    public void appendIntail(int number){
        //创建一个值为number的结点
        ListNode newNode = new ListNode(number);
        //链表为空时,申请的结点就是第一个结点也是表尾结点
        if(tail == null){
            tail = newNode;
            head =newNode;
        }else {
            //链表不空直接加入表尾
            tail.next = newNode;
            tail = newNode;
        }
        size++;
        System.out.println("值为" + number +"的结点成功加入链表!");

    }

    //头插法建立单链表
    public void appendInHead(int number){
        ListNode newNode = new ListNode(number);

        if(tail == null){
            //初试链表为空
            head = newNode;
            tail = newNode;
            System.out.println("头插法成功!");
        }else {
            //链表不空,放在首节点之前
            newNode.next = head;
            head = newNode;
            System.out.println("头插法成功!");
        }

    }

    //在position插入值为number的结点
    public void insert(int position,int number){
        if(position >size){
            //插入位置不合法
            return;
        }
        ListNode newNode = new ListNode(number);
        if(position == 0){
            //在首节点之前插入
            newNode.next=head;
            head = newNode;
            if(tail == null){
                //说明新结点是该链表第一个插入的结点
                tail = newNode;
            }
            size++;
            System.out.println("插入成功!");


        } else if (position == size){
            //插入位置在最后一个的后一个,直接加载末尾
            this.appendIntail(number);
            System.out.println("插入成功!");

        }else {
            //找到要插入位置的前一个位置
            //从头结点开始遍历,直到找到position-1的位置
            ListNode pre = head;
            for(int i = 0;i < position-1;i++){
                pre = pre.next;
            }
            //插入
            newNode.next=pre.next;
            pre.next = newNode;
            size++;
            System.out.println("插入成功!");
        }

    }

    //打印链表
    public void printList(){
        System.out.print("链表:[");
        ListNode cur = head;
        while (cur != null){
            if(cur.next == null){
                System.out.println(cur.val +"]");
                cur = cur.next;
                return;
            }
            System.out.print(cur.val + ",");
            cur = cur.next;

        }
        System.out.println(" ");
    }


    //按值删除
    public void deleteNode(int number){
        //第一个节点是要删除的结点的情况下,直接删除
        if(head !=null && head.val == number){
            head = head.next;
            size--;
            if(size == 0){
                //表为空的情况下
                tail = head;
            }
            System.out.println("删除成功,成功删除值为" + number + "的结点");


        }else {
            //否则就遍历链表找到值为number的结点的前一个结点
            ListNode pre = head;
            ListNode cur = head;
            //遍历
            while (cur != null){
                if(cur.val == number){
                    //要删除的结点是表尾结点,修改tail
                    if (cur == tail){
                        pre.next = null;
                        tail = pre;
                        size--;
                        System.out.println("删除成功,成功删除值为" + number + "的结点");
                        return;

                    }else {
                        //删除的不是表尾结点,正常删除
                        pre.next = cur.next;
                        size--;
                        System.out.println("删除成功,成功删除值为" + number + "的结点");
                        return;
                    }
                }else {
                    //不是要删除的结点,继续向后滑动遍历
                    pre = cur;
                    cur = cur.next;
                }
            }
            //while结束后还没成功删除,说明没有值为number的结点
            System.out.println("删除失败,没有该结点");
            return;
        }

    }


    //按值查找
    public void findInNumber(int number){
        ListNode p = head;
        while (p != null){
            if(p.val == number){
                System.out.println("查找成功!");
                return;
            }else {
                p = p.next;
            }
        }
        System.out.println("不存在该数,查找失败!");
        return;
    }

    //按位查找
    public void findInIndex(int index){
        if(index > size){
            System.out.println("查找失败,位置非法!");
        }else {
            ListNode cur = head;
            int i = 0;
            while (i < index){
                cur = cur.next;
                i++;
            }
            System.out.println("查找成功,查找到的数值为:" + cur.val);
        }
    }

    //更新值为oldvalue的结点为newValue,并返回更新结点的位置
    public int update(int oldValue,int newValue){
        int i = 1;
        ListNode cur = head;
        while (cur != null){
            if(cur.val == oldValue){
                cur.val = newValue;
                System.out.println("更新成功!");
                return i;
            }else {
                i++;
                cur = cur.next;
            }
        }
        System.out.println("更新失败!不存在值为" + oldValue + "的结点");
        return -1;
    }

}


2、结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值