国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 15:36:12
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桓仁| 久治县| 新丰县| 清水河县| 开鲁县| 且末县| 晋江市| SHOW| 白山市| 大埔区| 新邵县| 信丰县| 修武县| 民丰县| 崇州市| 呈贡县| 德格县| 保德县| 西峡县| 类乌齐县| 新晃| 大兴区| 达尔| 开封县| 金坛市| 逊克县| 彩票| 泗水县| 池州市| 深州市| 新巴尔虎右旗| 额尔古纳市| 曲周县| 高碑店市| 社旗县| 东海县| 唐山市| 双牌县| 灌南县| 彭州市| 曲麻莱县|