java实现数据结构单链表示例(java单链表)

 更新时间:2014年03月17日 09:25:09   作者:  
这篇文章主要介绍了java数据结构实现单链表示例,需要的朋友可以参考下

复制代码 代码如下:

/**
 * 单向链表
 *
 */
public class NodeList<E> {
 private static class Node<E> { // 节点类
  E data; // 节点上的数据
  Node<E> next; // 指向下一个节点

  Node(E e) {
   this.data = e;
   this.next = null;
  }
 }

 private Node<E> head; // 链表的头节点
 private Node<E> last; // 链表的尾节点
 private Node<E> other = null;
 private int length = 0; // 节点数量

 /**
  * 无参构造方法
  */
 public NodeList() {
  // 默认节点为空
  this.head = new Node<E>(null);
 }

 /**
  * 初始化时创建一个节点
  *
  * @param data
  *            数据
  */
 public NodeList(E data) {
  this.head = new Node<E>(data);
  this.last = head;
  length++;
 }

 /**
  * 添加一个节点(尾插法)
  *
  * @param data
  *            数据
  */
 public void add(E data) {
  if (isEmpty()) {
   head = new Node<E>(data);
   last = head;
   length++;
  } else {
   Node<E> newNode = new Node<E>(data);
   last.next = newNode;
   last = newNode;
  }
 }

 /**
  * 获得索引处的数据(索引输入错误抛出越界异常)
  * @param index 索引
  * @return 索引处数据
  */
 public E get(int index){
  if(index<0 || index>length){
   throw new IndexOutOfBoundsException("索引越界:"+index);
  }
  other = head;
  for(int i=0;i<index;i++){
   other = other.next;
  }
  return other.data;
 }

 /**
  * 新值替换旧值
  * @return 成功为true,未找到为false
  */
 public boolean set(E oldValue,E newValue){
  other = head;
  while(other!=null){
   if(other.data.equals(oldValue)){
    other.data = newValue;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 在指定元素后插入一个元素
  *
  * @param data
  *            指定的元素
  * @param insertData
  *            需要插入的元素
  * @return false为未找到元素,true为插入成功
  */
 public boolean add(E data, E insertData) {
  other = head;
  while (other != null) {
   if (other.data.equals(data)) {
    Node<E> newNode = new Node<E>(insertData);
    Node<E> temp = other.next;
    newNode.next = temp;
    other.next = newNode;
    length++;
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 链表中是否包含此元素
  * @return 包含为true,不包含为false
  */
 public boolean contains(E data){
  other = head;
  while(other!=null){
   if(other.data.equals(data)){
    return true;
   }
   other = other.next;
  }
  return false;
 }

 /**
  * 移除指定的元素
  * @param data 需要移除的元素
  * @return 不存在为false,成功为true
  */
 public boolean remove(E data){
  other = head;
  Node<E> temp = head;  //临时变量,用于保存前一个节点
  while(other!=null){
   if(other.data.equals(data)){
    temp.next = other.next;
    length--;
    return true;
   }
   temp = other;
   other = other.next;
  }
  return false;
 }

 /**
  * 判断链表是否为空
  *
  * @return 空为true,非空为false
  */
 public boolean isEmpty() {
  return length == 0;
 }

 /**
  * 清空链表
  */
 public void clear() {
  this.head = null;
  this.length = 0;
 }

 /**
  * 输出所有节点
  */
 public void printLink() {
  if(isEmpty()){
   System.out.println("空链表");
  }else{
   other = head;
   while (other != null) {
    System.out.print(other.data);
    other = other.next;
   }
   System.out.println();
  }
 }
}

相关文章

  • SpringCloud-Config分布式配置代码示例

    SpringCloud-Config分布式配置代码示例

    这篇文章主要介绍了SpringCloud-Config分布式配置代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Java中的有限状态机(设计模式——状态模式)

    Java中的有限状态机(设计模式——状态模式)

    这篇文章主要介绍了Java中的有限状态机(设计模式——状态模式),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • idea设置@Author文件头注释的实现步骤

    idea设置@Author文件头注释的实现步骤

    本文主要介绍了idea设置@Author文件头注释的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • struts2框架的登录制作图文教程

    struts2框架的登录制作图文教程

    下面小编就为大家带来一篇struts2框架的登录制作图文教程。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • IDEA GIT 忽略文件的最佳方式推荐

    IDEA GIT 忽略文件的最佳方式推荐

    这篇文章主要介绍了IDEA GIT 忽略文件的最佳方式推荐,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Java实现赫夫曼树(哈夫曼树)的创建

    Java实现赫夫曼树(哈夫曼树)的创建

    给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度(WPL)达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree)。这篇文章主要就是为大家介绍如何通过Java实现赫夫曼树,需要的朋友可以参考一下
    2021-12-12
  • spring的13个经典面试题

    spring的13个经典面试题

    Spring框架是一个开放源代码的J2EE应用程序框架,是针对bean的生命周期进行管理的轻量级容Spring解决了开发者在J2EE开发中遇到的许多常见的问题,我们这篇文章就来了解一下spring的面试题
    2021-06-06
  • Java设计模式之工厂方法和抽象工厂

    Java设计模式之工厂方法和抽象工厂

    本文详细讲解了Java设计模式之工厂方法和抽象工厂,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • 详解Java如何利用位操作符创建位掩码

    详解Java如何利用位操作符创建位掩码

    在本文中,我们来看看如何使用位操作符实现低级别的位掩码。我们将看到我们如何将一个单一的int变量作为一个单独的数据容器,感兴趣的可以跟随小编一起学习一下
    2022-10-10
  • Java的MyBatis框架项目搭建与hellow world示例

    Java的MyBatis框架项目搭建与hellow world示例

    MyBatis框架为Java程序的数据库操作带来了很大的便利,这里我们就从最基础的入手,来看一下Java的MyBatis框架项目搭建与hellow world示例,需要的朋友可以参考下
    2016-06-06

最新评论