java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表;
/*
*定義節(jié)點(diǎn)
* 鏈表由節(jié)點(diǎn)構(gòu)成
*/
public class Node<E> {
private E e; //數(shù)據(jù)data
private Node<E> next; //指向下一個(gè)節(jié)點(diǎn)
public Node() {
}
public Node(E e) {
this.e = e;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
}
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表;
/*
* 定義實(shí)現(xiàn)類MyLinkedList
* 實(shí)現(xiàn)鏈表的基本功能:增刪改查
*/
public class MyLinkedList<E> {
//聲明頭節(jié)點(diǎn)尾節(jié)點(diǎn)
private Node<E> head;
private Node<E> last;
//鏈表的大小
private int size;
private int modcount; //計(jì)算被修改的次數(shù)
public MyLinkedList() {
head = new Node<E>();//實(shí)例化頭結(jié)點(diǎn)
last = head;
}
/*
*返回單鏈表中存儲(chǔ)的元素總數(shù)
*/
public int size() {
return size;
}
/*
*獲取指定索引位置的節(jié)點(diǎn)對(duì)象
*/
public Node<E> get(int index) {
if (index < 0 || index > size - 1)
return null;
Node<E> node = head.getNext();//將頭結(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)賦給Node
for (int i = 0; i < index; i++) {
node = node.getNext();//獲取node的下一個(gè)節(jié)點(diǎn)
}
return node;
}
/*
*獲取指定索引位置的數(shù)據(jù)
*/
public E getValue(int index) {
if (index < 0 || index > size - 1)
return null;
Node<E> node = get(index);
return node.getE();
}
/*
*增加元素
*/
public void add(E e) {
Node<E> node = new Node<E>(e); //以e實(shí)例化一個(gè)節(jié)點(diǎn)
last.setNext(node);//往尾節(jié)點(diǎn)后追加節(jié)點(diǎn)
last = node;//該節(jié)點(diǎn)設(shè)為最后一個(gè)節(jié)點(diǎn)
size++;
modcount++;
}
/*
*指定位置插入元素,返回插入的節(jié)點(diǎn)數(shù)據(jù)
*/
public E add(int index, E e) {
if (index < 0 || index > size - 1)
return null;
Node<E> node = new Node<E>(e); //實(shí)例化一個(gè)節(jié)點(diǎn)
//找到插入的原節(jié)點(diǎn)
Node<E> oldNode = get(index);
if (index == 0) {//當(dāng)索引為0時(shí)
head.setNext(node);
} else {
//找到插入節(jié)點(diǎn)的上一個(gè)
Node<E> bNode = get(index - 1);
bNode.setNext(node);
}
node.setNext(oldNode);
size++;
modcount++;
return oldNode.getE();
}
/*
*刪除指定的節(jié)點(diǎn)e,并返回刪除節(jié)點(diǎn)的數(shù)據(jù)
*/
public E delete(int index) {
if (index < 0 || index > size - 1)
return null;
if (index == 0) {//當(dāng)索引為1,令頭結(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)為頭結(jié)點(diǎn)
Node<E> node = head.getNext();
head.setNext(node.getNext());
}
//獲取要?jiǎng)h除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)
Node<E> bNode = get(index - 1);
//獲取要?jiǎng)h除的節(jié)點(diǎn)
Node<E> Node = bNode.getNext();
//獲取要?jiǎng)h除節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
Node<E> nNode = Node.getNext();
//刪除該節(jié)點(diǎn)
bNode.setNext(nNode);
//清除Node的下一個(gè)節(jié)點(diǎn)
Node.setNext(null);
size--;
modcount++;
return Node.getE();//返回節(jié)點(diǎn)中的數(shù)據(jù)域
}
/*
*修改指定位置的數(shù)據(jù)域并返回修改后的數(shù)據(jù)
*/
public E set(int index, E e) {
if (index < 0 || index > size - 1)
return null;
//獲取指定位置的原節(jié)點(diǎn)
Node<E> node = get(index);
node.setE(e);
modcount++;
return node.getE();
}
}
package 數(shù)據(jù)結(jié)構(gòu)算法.鏈表;
/*
*定義測(cè)試類
*/
public class MyLinkedListTest {
public static void main(String[] args) {
MyLinkedList<String> list = new MyLinkedList<>();
//測(cè)試add
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add(0,"newone");
list.add(1,"newtwo");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.getValue(i)+" ");
}
//測(cè)試set
System.out.println();
list.set(0, "111");
list.set(1, "222");
System.out.println(list.getValue(0) + " " + list.getValue(1));
//測(cè)試delete
System.out.println();
list.delete(1);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.getValue(i)+" ");
}
}
}
運(yùn)行結(jié)果如下:

以上就是全部知識(shí)點(diǎn)內(nèi)容,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
利用canvas中toDataURL()將圖片轉(zhuǎn)為dataURL(base64)的方法詳解
這篇文章主要給大家介紹了關(guān)于利用canvas中toDataURL()將圖片轉(zhuǎn)為dataURL(base64)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
微信小程序onShareTimeline()實(shí)現(xiàn)分享朋友圈
這篇文章主要給大家介紹了關(guān)于微信小程序onShareTimeline()實(shí)現(xiàn)分享朋友圈的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
JS+HTML5 FileReader對(duì)象用法示例
這篇文章主要介紹了JS+HTML5 FileReader對(duì)象用法,結(jié)合具體實(shí)例形式分析了FileReader對(duì)象的常用方法及簡(jiǎn)單使用技巧,需要的朋友可以參考下2017-04-04
PhotoSwipe異步動(dòng)態(tài)加載圖片方法
這篇文章主要為大家詳細(xì)介紹了PhotoSwipe異步動(dòng)態(tài)加載圖片方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
JS用斜率判斷鼠標(biāo)進(jìn)入DIV四個(gè)方向的方法
在網(wǎng)上去搜判斷鼠標(biāo)移入div移入移出的方法大同小異,下面小編給大家分享一篇文章關(guān)于js判斷鼠標(biāo)進(jìn)入div方向的代碼,感興趣的朋友一起看看吧2016-11-11
js setTimeout()函數(shù)介紹及應(yīng)用以倒計(jì)時(shí)為例
setTimeout() 方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計(jì)算表達(dá)式,下面有個(gè)倒計(jì)時(shí)的示例,需要的朋友可以學(xué)習(xí)下2013-12-12
canvas實(shí)現(xiàn)愛(ài)心和彩虹雨效果
本文主要介紹了canvas實(shí)現(xiàn)愛(ài)心和彩虹雨效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
JavaScript寫的一個(gè)自定義彈出式對(duì)話框代碼
最近閑來(lái)無(wú)事,用js自己做了一個(gè)彈出式對(duì)話框,需要應(yīng)用彈出式對(duì)話框的朋友可以參考下。2010-01-01

