java链表的查找,插入与删除

public class LinkedList {

  private Node root = null;
  int size = 0;

  public LinkedList() {
  }

  // 数组创建一个链表
  public static Node createLinkedLNode(int[] array) {
    // 创建一个根节点
    Node root = null;
    // 从末尾元素开始依次创建Node节点
    for (int i = array.length - 1; i >= 0; i--) {
      Node node = new Node(array[i], null);
      // 创建两个节点的连接关系
      if (root != null) {
        node.setNext(root);
        root = node;
      } else {
        root = node;
      }
    }
    return root;
  }

  public LinkedList(int[] array) {
    this.root = LinkedList.createLinkedLNode(array);
    this.size = array.length;
  }

  // 获取长度
  public int size() {
    return this.size;
  }

  // 获取某个索引节点内容
  public int get(int index) {
    int i = 0;
    Node node = this.root;
    // 依次往前推进
    while (i < index) {
      node = node.getNext();
      i++;
    }
    return node.getContent();
  }

  // 查找某个值的索引值,默认不存在为-1
  public int find(int value) {
    Node node = this.root;
    // index 保存当前的索引
    int index = 0;
    // 依次遍历链表,找到内容等于value的node,返回index
    while (node != null) {
      if (node.getContent() == value) {
        return index;
      }
      node = node.getNext();
      index++;
    }
    return -1;
  }

  // 末尾添加元素
  public boolean add(int value) {
    return this.add(this.size - 1, value);
  }

  // 头部插入元素
  public boolean addFirst(int value) {
    return this.add(-1, value);
  }

  // 插入元素
  public boolean add(int index, int value) {
    if (index < -1 || index > this.size - 1){
      return false;
    }
    if (index == -1) {
      if (this.root != null) {
        this.root = new Node(value, this.root);
      } else {
        this.root = new Node(value, null);
      }
    } else {
      Node pre = this.root;
      while (index > 0) {
        if (pre.getNext() == null) {
          return false;
        }
        pre = pre.getNext();
        index--;
      }

      if (pre == null) {
        return false;
      }

      Node newNode = new Node(value, pre.getNext());
      pre.setNext(newNode);
    }
    this.size++;
    return true;
  }

  // 删除最后一个元素
  public boolean removeLast() {
    return this.remove(this.size - 1);
  }

  // 删除第一个元素
  public boolean removeFirst() {
    return this.remove(0);
  }

  // 删除元素
  // index = 0, 表示删除第 1 个元素
  // index = n,表示删除第 n+1 个元素
  public boolean remove(int index) {
    if (index < 0 || index > this.size - 1){
      return false;
    }
    // 判断整个列表为空情况,删除失败
    if (this.root == null) {
      return false;
    }
    if (index == 0) {
      // 删除第一个元素,必须先保留 this.root,因为紧接着 this.root 会被修改
      Node node = this.root;
      this.root = node.getNext();
      node.setNext(null);
    } else {
      // 判断越界问题
      if(this.size < index + 1){
        return false;
      }
      // 遍历获取index的前置节点
      Node pre = this.root;
      while (index > 1) {
        pre = pre.getNext();
        index--;
      }
      // 修改next指针,需要先保存 pre.next,因为紧接着下一步会修改 pre.next 指针
      Node next = pre.getNext();
      pre.setNext(next.getNext());
      next.setNext(null);
    }
    this.size--;
    return true;
  }

  // 链表中的元素字符串形式
  public String toString() {
    if (this.root == null) {
      return "";
    }

    StringBuilder str = new StringBuilder();
    Node node = this.root;
    while (node != null) {
      str.append(node.getContent()).append(" ");
      node = node.getNext();
    }
    return str.toString();
  }

  public static void main(String[] args) {
    int[] array = {10, 9, 2, 4};
    LinkedList linkedList = new LinkedList(array);
  }
}

链表类

public class Node {

  private int content;

  private Node next;

  public Node() {
  }

  public Node(int content, Node next) {
    this.content = content;
    this.next = next;
  }

  public int getContent() {
    return content;
  }

  public void setContent(int content) {
    this.content = content;
  }

  public Node getNext() {
    return next;
  }

  public void setNext(Node next) {
    this.next = next;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值