加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:15:45 所属栏目:Java 来源:网络整理
导读:复制代码 代码如下: /** * 单向链表 * */ public class NodeListE { private static class NodeE { // 节点类 E data; // 节点上的数据 NodeE next; // 指向下一个节点 Node(E e) { this.data = e; this.next = null; } } private NodeE head; // 链表的头节

复制代码 代码如下:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读