java實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)單鏈表示例(java單鏈表)
/**
* 單向鏈表
*
*/
public class NodeList<E> {
private static class Node<E> { // 節(jié)點(diǎn)類
E data; // 節(jié)點(diǎn)上的數(shù)據(jù)
Node<E> next; // 指向下一個(gè)節(jié)點(diǎn)
Node(E e) {
this.data = e;
this.next = null;
}
}
private Node<E> head; // 鏈表的頭節(jié)點(diǎn)
private Node<E> last; // 鏈表的尾節(jié)點(diǎn)
private Node<E> other = null;
private int length = 0; // 節(jié)點(diǎn)數(shù)量
/**
* 無(wú)參構(gòu)造方法
*/
public NodeList() {
// 默認(rèn)節(jié)點(diǎn)為空
this.head = new Node<E>(null);
}
/**
* 初始化時(shí)創(chuàng)建一個(gè)節(jié)點(diǎn)
*
* @param data
* 數(shù)據(jù)
*/
public NodeList(E data) {
this.head = new Node<E>(data);
this.last = head;
length++;
}
/**
* 添加一個(gè)節(jié)點(diǎn)(尾插法)
*
* @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ù)(索引輸入錯(cuò)誤拋出越界異常)
* @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;
}
/**
* 在指定元素后插入一個(gè)元素
*
* @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; //臨時(shí)變量,用于保存前一個(gè)節(jié)點(diǎn)
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é)點(diǎn)
*/
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實(shí)現(xiàn)單鏈表中是否有環(huán)的方法詳解
- Java單鏈表基本操作的實(shí)現(xiàn)
- Java實(shí)現(xiàn)單鏈表翻轉(zhuǎn)實(shí)例代碼
- java實(shí)現(xiàn)單鏈表之逆序
- Java單鏈表的實(shí)現(xiàn)代碼
- Java數(shù)據(jù)結(jié)構(gòu)之簡(jiǎn)單鏈表的定義與實(shí)現(xiàn)方法示例
- Java模擬單鏈表和雙端鏈表數(shù)據(jù)結(jié)構(gòu)的實(shí)例講解
- java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼
- java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
- Java實(shí)現(xiàn)單鏈表的操作
相關(guān)文章
java中生成任意之間數(shù)的隨機(jī)數(shù)詳解
這篇文章主要介紹了java中生成任意之間數(shù)的隨機(jī)數(shù)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:冒泡排序 Bubble Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:冒泡排序 Bubble Sort,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06Java阻塞隊(duì)列的實(shí)現(xiàn)及應(yīng)用
這篇文章主要介紹了剖析Java中阻塞隊(duì)列的實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景,這里也對(duì)阻塞和非阻塞隊(duì)列的不同之處進(jìn)行了對(duì)比,需要的朋友可以參考下2021-10-10詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法
這篇文章主要介紹了詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09淺談springmvc的DispatcherServlet分析
本篇文章主要介紹了淺談springmvc的DispatcherServlet分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-09-09java程序員必會(huì)的遠(yuǎn)程debug教程
這篇文章主要為大家介紹了java程序員必會(huì)的遠(yuǎn)程debug教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08springboot獲取properties屬性值的多種方式總結(jié)
這篇文章主要介紹了springboot獲取properties屬性值的多種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java實(shí)現(xiàn)文件點(diǎn)擊沒反應(yīng)的方法
jsp頁(yè)面鏈接,點(diǎn)擊訪問action用IO流去下載服務(wù)器上的文件,問題是任憑怎么點(diǎn)擊都沒反應(yīng),日志也不報(bào)錯(cuò)。這篇文章給大家介紹Java實(shí)現(xiàn)文件點(diǎn)擊沒反應(yīng)的方法,需要的朋友參考下吧2018-07-07