当前位置: 移动技术网 > IT编程>开发语言>Java > Java数据结构之简单链表的定义与实现方法示例

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

一、概述:

1、原理:

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

2、插入数据说明:

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

3、插入数据图:

4、特点:先进后出

5、实现功能:

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

6、删除原理

7、插入头节点原理

二、实现:

1、创建节点

/**
 * @描述     节点
 * @项目名称   java_datastruct
 * @包名     com.struct.linklist
 * @类名     node
 * @author   chenlin
 * @date    2010年6月26日 上午7:58:59
 * @version   1.0 
 */
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、链表实现

/**
 * @描述     链表
 * @项目名称   java_datastruct
 * @包名     com.struct.linklist
 * @类名     linklist
 * @author   chenlin
 * @date    2010年6月26日 上午8:00:28
 * @version   1.0 
 */
public class linklist {
  private node first;
  public linklist(){
    first = null;
  }
  /**
   * 插入数据
   * @param value
   */
  public void insertfirst(long value){
    node newnode = new node(value);
    if (first == null) {
      first = newnode;
    }else {
      //把first节点往下移动
      newnode.next = first;
      //把插入的节点作为新的节点
      first = newnode;
    }
  }
  /**
   * 删除头节点
   * @param value
   * @return
   */
  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("---------------");
  }
  /**
   * 查找节点1
   * @param value
   * @return
   */
  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;
  }
  /**
   * 查找节点2
   * 
   * @param key
   * @return
   */
  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;
  }
  /**
   * 根据索引查找对应的值
   * @param position
   * @return
   */
  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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网