单向链表

一:单向链表

图1

1.创建链表节点Node
//链节点: 包括 数据域和节点域
public class Node {
    //数据域:保存当前节点的数据
    long data;
    //指针域:保存下一个节点的引用
    Node next;

    //构造方法
    public  Node(long value){
        this.data = value;
    }

    //显示方法
    public void display(){
        System.out.println(data);
    }

}

2.创建链表和操作链表
//链表
public class LinkList {

    //头节点
    private  Node first;

    //初始化链表
    public LinkList(){
        first = null;
    }

    //从头节点first处插入节点
    public void insert(long value){
        //创建待插入的新节点,并且初始化值
        Node node = new Node(value);
        //新节点的next 指向 first节点的next,但是如果first==null则没有next节点
        if (first == null){
            first = node;
        }else{
            node.next = first;
            first = node;
        }
    }

    //删除头节点
    public Node removeFirst(){
        Node next = first.next;
        Node tmp = first;
        first = next;
        return tmp;
    }

    //删除第二个节点
    public Node remove(){
        //先获取被删除的节点
        Node tmp = first.next;
        //把头节点的next指向被删除节点的next
        first.next = tmp.next;
        //返回被删除的节点
        return tmp;
    }

    //从头节点开始显示节点
    public void  show(){
        //遍历
        //先获取当前节点为头节点
        Node current = first;
        //判断当前节点是否存在
        while (current != null){
            //显示第一个节点
            current.display();
            //把下一个节点赋值给当前节点,如果不为null则会被显示,如果为null则结束循环
            current = current.next;
        }
    }

    //查找节点
    public Node find(long value){
        //从头开始查找
        Node current = first;
        while (current != null && current.data != value){
            if (current.next == null){
                return null;
            }
            //如果找不到就从下一个节点开始
            current = current.next;
        }
        return current;
    }

    //根据数据域删除节点
    public Node delete(long value){
        //先查找到指定节点
        Node current = first;
        //新增临时节点,用于保存被找到的节点的前一个节点
        Node pre = first;
        while (current != null && current.data != value){
            if (current.next == null){
                //找不到返回null
                return null;
            }
            //保存当前节点,也就是被找的前一个节点
            pre = current;
            //如果找不到就从下一个节点开始
            current = current.next;
        }
        //删除
      if (current == first){
            first = first.next;
      }else{
            pre.next = current.next;
      }
      return current;
    }

}

3.测试链表
public class Test {
    public static void main(String[] args) {

        LinkList linkList = new LinkList();
        linkList.insert(1);
        linkList.insert(6);
        linkList.insert(8);
        linkList.insert(0);  //最后插入的是first节点

        linkList.show();  //0 8 6 1

        Node first = linkList.removeFirst();
        System.out.println("删除头节点: "+first.data); //删除头节点: 0
        linkList.show();  //8 6 1

        Node remove = linkList.remove();
        System.out.println("删除中间节点: "+remove.data); //删除中间节点: 6

        linkList.show();  //8 1

        //查找节点
        Node node = linkList.find(0);
      if (node != null){
          System.out.println("找到指定节点");
          node.display();
      }else{
          System.out.println("找不到节点:"+node);
      }

      //删除节点
        Node delete = linkList.delete(0);
        if (delete != null){
            System.out.println("找到被删除节点");
            delete.display();
        }else{
            System.out.println("找不到删除节点:"+delete);
        }
        linkList.show();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值