欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 20:53:34
复制代码 代码如下:/** * 单向链表 * */public class nodelist { private...

复制代码 代码如下:

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