一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java實現數據結構單鏈表示例(java單鏈表)

java實現數據結構單鏈表示例(java單鏈表)

2019-11-14 15:52java教程網 JAVA教程

這篇文章主要介紹了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();
  }
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: segou视频在线观看 | 国产成人久久精品区一区二区 | 菠萝视频在线完整版 | 福利视频一区二区思瑞 | 韩国三级大全 | 欧美人禽杂交av片 | 国产精品久久久天天影视香蕉 | 亚洲国产综合精品 | 垫底辣妹免费观看完整版 | 国产乱叫456在线 | 国产在线欧美日韩精品一区二区 | 亚洲精品欧洲久久婷婷99 | 成人免费视频一区二区 | 三级黄色片在线观看 | 女女性恋爱免费 | 日本无遮挡拍拍拍凤凰 | 毛片在线观看网站 | 美女私人影院 | 寡妇快点好大好爽视频 | 日本红色高清免费观看 | 清纯漂亮女友初尝性过程 | 插鸡视频在线观看 | 午夜一区二区免费视频 | 98成人网| 日本乱人伦中文在线播放 | 激情综合| 亚洲视频第一页 | 午夜精品久久久久 | 女子张腿让男人桶免费 | 91香蕉依人综合久久 | 亚洲欧美日韩中文字幕网址 | 国产精品视频人人做人人爱 | 男女一级特黄a大片 | 国产99精品免费视频看6 | 青草视频网 | 久久伊人电影 | 国产成人黄网在线免 | 九九久久精品 | 大叔在线观看 | 成人影院免费在线观看 | 日本天堂影院在线播放 |