栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java数据结构之简单链表的定义与实现方法示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java数据结构之简单链表的定义与实现方法示例

本文实例讲述了Java数据结构之简单链表的定义与实现方法。分享给大家供大家参考,具体如下:

一、概述:

1、原理:

只有一个数据项(链接点link),每个数据插入时都是对第一个数据的引用。

2、插入数据说明:

当链表没有数据时,插入的值就是第一个数据,如果链表里有数据,就把当前的数据的next指针指向第一个数据。

3、插入数据图:

4、特点:先进后出

5、实现功能:

数据插入,指定位置插入,显示,查询,删除等

6、删除原理

7、插入头节点原理

二、实现:

1、创建节点


public class Node {
  public long data;
  public Node next;
  public long getData() {
    return data;
  }
  public void display(){
    System.out.print(data + " ");
  }
  public Node(long data) {
    this.data = data;
  }
  public void setData(long data) {
    this.data = data;
  }
  public Node getNext() {
    return next;
  }
  public void setNext(Node next) {
    this.next = next;
  }
}

2、链表实现


public class linkList {
  private Node first;
  public linkList(){
    first = null;
  }
  
  public void insertFirst(long value){
    Node newNode = new Node(value);
    if (first == null) {
      first = newNode;
    }else {
      //把first节点往下移动
      newNode.next = first;
      //把插入的节点作为新的节点
      first = newNode;
    }
  }
  
  public Node deleteFirst(){
    if (first == null) {
      throw new RuntimeException("链表数据不存在");
    }
    Node temp = first;
    first = temp.next;
    return temp;
  }
  public Node deleteByKey(long key){
    Node current = first;
    Node last = first;
    while(current.data != key){
      if (current.next == null) {
 System.out.println("没找到节点");
 return null;
      }
      last = current;
      current = current.next;
    }
    if (current == first) {
      //return deleteFirst();
      //指向下个就表示删除第一个
      first = first.next;
    }else {
      last.next = current.next;
    }
    return current;
  }
  
  public void display(){
    if (first == null) {
      //throw new RuntimeException("链表数据不存在");
      return;
    }
    Node current = first;
    while(current != null){
      current.display();
      current = current.next;
    }
    System.out.println("---------------");
  }
  
  public Node findByValue(long value){
    Node current = first;
    while(current != null){
      if (current.data != value) {
 current = current.next;
      }else {
 break;
      }
    }
    if (current == null) {
      System.out.println("没找到");
      return null;
    }
    return current;
  }
  
  public Node findByKey(long key) {
    Node current = first;
    while (current.data != key) {
      if (current.next == null) {
 System.out.println("没找到");
 return null;
      }
      current = current.next;
    }
    return current;
  }
  
  public Node findByPosition(int position){
    Node current = first;
    //为什么是position - 1,因为要使用遍历,让current指向下一个, 所以position - 1的下个node就是要找的值
    for (int i = 0; i < position - 1 ; i++) {
      current = current.next;
    }
    return current;
  }
  public static void main(String[] args) {
    linkList linkList = new linkList();
    linkList.insertFirst(21);
    linkList.insertFirst(22);
    linkList.insertFirst(23);
    linkList.insertFirst(24);
    linkList.insertFirst(25);
    linkList.insertFirst(26);
    linkList.insertFirst(27);
    System.out.println("考高分网测试结果:");
    linkList.display();
    System.out.println("---查找-------------------------------------");
    linkList.findByKey(25).display();
    System.out.println("--删除first-------------------------------------");
    //linkList.deleteFirst().display();
    ///linkList.deleteFirst().display();
    //linkList.deleteFirst().display();
    //linkList.deleteFirst().display();
    System.out.println("-删除指定值---------------------------------------");
    linkList.deleteByKey(27).display();
    linkList.deleteByKey(21).display();
    System.out.println("----------------------------------------");
    linkList.display();
  }
}

显示结果:

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/143619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号