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

 更新时间:2014年03月17日 09:25:09   作者:  
这篇文章主要介绍了java数据结构实现单链表示例,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

复制代码 代码如下:

/**
 * 单向链表
 *
 */
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();
  }
 }
}

相关文章

  • java中生成任意之间数的随机数详解

    java中生成任意之间数的随机数详解

    这篇文章主要介绍了java中生成任意之间数的随机数详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Kryo框架使用方法代码示例

    Kryo框架使用方法代码示例

    这篇文章主要介绍了Kryo框架的相关内容,文中向大家分享了Kryo框架使用方法代码示例,小编觉得挺不错的,希望能给大家一个参考。
    2017-10-10
  • Java数据结构及算法实例:冒泡排序 Bubble Sort

    Java数据结构及算法实例:冒泡排序 Bubble Sort

    这篇文章主要介绍了Java数据结构及算法实例:冒泡排序 Bubble Sort,本文直接给出实现代码,代码中包含详细注释,需要的朋友可以参考下
    2015-06-06
  • Java阻塞队列的实现及应用

    Java阻塞队列的实现及应用

    这篇文章主要介绍了剖析Java中阻塞队列的实现原理及应用场景,这里也对阻塞和非阻塞队列的不同之处进行了对比,需要的朋友可以参考下
    2021-10-10
  • 详解Java的内置异常以及创建自定义异常子类的方法

    详解Java的内置异常以及创建自定义异常子类的方法

    这篇文章主要介绍了详解Java的内置异常以及创建自定义异常子类的方法,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • Java异常体系非正常停止和分类

    Java异常体系非正常停止和分类

    这篇文章主要介绍了Java异常体系非正常停止和分类,指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止更多相关内容需要的朋友可以参考一下
    2022-06-06
  • 浅谈springmvc的DispatcherServlet分析

    浅谈springmvc的DispatcherServlet分析

    本篇文章主要介绍了浅谈springmvc的DispatcherServlet分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • java程序员必会的远程debug教程

    java程序员必会的远程debug教程

    这篇文章主要为大家介绍了java程序员必会的远程debug教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • springboot获取properties属性值的多种方式总结

    springboot获取properties属性值的多种方式总结

    这篇文章主要介绍了springboot获取properties属性值的多种方式总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java实现文件点击没反应的方法

    Java实现文件点击没反应的方法

    jsp页面链接,点击访问action用IO流去下载服务器上的文件,问题是任凭怎么点击都没反应,日志也不报错。这篇文章给大家介绍Java实现文件点击没反应的方法,需要的朋友参考下吧
    2018-07-07

最新评论

?


http://www.vxiaotou.com