java實現(xiàn)數(shù)據(jù)結構單鏈表示例(java單鏈表)
/**
* 單向鏈表
*
*/
public class NodeList<E> {
private static class Node<E> { // 節(jié)點類
E data; // 節(jié)點上的數(shù)據(jù)
Node<E> next; // 指向下一個節(jié)點
Node(E e) {
this.data = e;
this.next = null;
}
}
private Node<E> head; // 鏈表的頭節(jié)點
private Node<E> last; // 鏈表的尾節(jié)點
private Node<E> other = null;
private int length = 0; // 節(jié)點數(shù)量
/**
* 無參構造方法
*/
public NodeList() {
// 默認節(jié)點為空
this.head = new Node<E>(null);
}
/**
* 初始化時創(chuàng)建一個節(jié)點
*
* @param data
* 數(shù)據(jù)
*/
public NodeList(E data) {
this.head = new Node<E>(data);
this.last = head;
length++;
}
/**
* 添加一個節(jié)點(尾插法)
*
* @param data
* 數(shù)據(jù)
*/
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;
}
}
/**
* 獲得索引處的數(shù)據(jù)(索引輸入錯誤拋出越界異常)
* @param index 索引
* @return 索引處數(shù)據(jù)
*/
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; //臨時變量,用于保存前一個節(jié)點
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;
}
/**
* 輸出所有節(jié)點
*/
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中的有限狀態(tài)機(設計模式——狀態(tài)模式)
這篇文章主要介紹了Java中的有限狀態(tài)機(設計模式——狀態(tài)模式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Java實現(xiàn)赫夫曼樹(哈夫曼樹)的創(chuàng)建
給定N個權值作為N個葉子結點,構造一棵二叉樹,若該樹的帶權路徑長度(WPL)達到最小,稱這樣的二叉樹為最優(yōu)二叉樹,也稱為哈夫曼樹(Huffman Tree)。這篇文章主要就是為大家介紹如何通過Java實現(xiàn)赫夫曼樹,需要的朋友可以參考一下2021-12-12
Java的MyBatis框架項目搭建與hellow world示例
MyBatis框架為Java程序的數(shù)據(jù)庫操作帶來了很大的便利,這里我們就從最基礎的入手,來看一下Java的MyBatis框架項目搭建與hellow world示例,需要的朋友可以參考下2016-06-06

