java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例代碼詳解
更新時間:2019年08月30日 08:13:06 作者:曾聰聰
在本篇文章里小編給大家整理了關(guān)于java實(shí)現(xiàn)單鏈表增刪改查的實(shí)例內(nèi)容,需要的朋友們可以參考下。
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; //指向下一個節(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; //計算被修改的次數(shù)
public MyLinkedList() {
head = new Node<E>();//實(shí)例化頭結(jié)點(diǎn)
last = head;
}
/*
*返回單鏈表中存儲的元素總數(shù)
*/
public int size() {
return size;
}
/*
*獲取指定索引位置的節(jié)點(diǎn)對象
*/
public Node<E> get(int index) {
if (index < 0 || index > size - 1)
return null;
Node<E> node = head.getNext();//將頭結(jié)點(diǎn)的下一個節(jié)點(diǎn)賦給Node
for (int i = 0; i < index; i++) {
node = node.getNext();//獲取node的下一個節(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í)例化一個節(jié)點(diǎn)
last.setNext(node);//往尾節(jié)點(diǎn)后追加節(jié)點(diǎn)
last = node;//該節(jié)點(diǎn)設(shè)為最后一個節(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í)例化一個節(jié)點(diǎn)
//找到插入的原節(jié)點(diǎn)
Node<E> oldNode = get(index);
if (index == 0) {//當(dāng)索引為0時
head.setNext(node);
} else {
//找到插入節(jié)點(diǎn)的上一個
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)的下一個節(jié)點(diǎn)為頭結(jié)點(diǎn)
Node<E> node = head.getNext();
head.setNext(node.getNext());
}
//獲取要刪除節(jié)點(diǎn)的前一個節(jié)點(diǎn)
Node<E> bNode = get(index - 1);
//獲取要刪除的節(jié)點(diǎn)
Node<E> Node = bNode.getNext();
//獲取要刪除節(jié)點(diǎn)的下一個節(jié)點(diǎn)
Node<E> nNode = Node.getNext();
//刪除該節(jié)點(diǎn)
bNode.setNext(nNode);
//清除Node的下一個節(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)算法.鏈表;
/*
*定義測試類
*/
public class MyLinkedListTest {
public static void main(String[] args) {
MyLinkedList<String> list = new MyLinkedList<>();
//測試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)+" ");
}
//測試set
System.out.println();
list.set(0, "111");
list.set(1, "222");
System.out.println(list.getValue(0) + " " + list.getValue(1));
//測試delete
System.out.println();
list.delete(1);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.getValue(i)+" ");
}
}
}
運(yùn)行結(jié)果如下:

以上就是全部知識點(diǎn)內(nèi)容,感謝大家對腳本之家的支持。
相關(guān)文章
利用canvas中toDataURL()將圖片轉(zhuǎn)為dataURL(base64)的方法詳解
這篇文章主要給大家介紹了關(guān)于利用canvas中toDataURL()將圖片轉(zhuǎn)為dataURL(base64)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
微信小程序onShareTimeline()實(shí)現(xiàn)分享朋友圈
這篇文章主要給大家介紹了關(guān)于微信小程序onShareTimeline()實(shí)現(xiàn)分享朋友圈的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
JS用斜率判斷鼠標(biāo)進(jìn)入DIV四個方向的方法
在網(wǎng)上去搜判斷鼠標(biāo)移入div移入移出的方法大同小異,下面小編給大家分享一篇文章關(guān)于js判斷鼠標(biāo)進(jìn)入div方向的代碼,感興趣的朋友一起看看吧2016-11-11
js setTimeout()函數(shù)介紹及應(yīng)用以倒計時為例
setTimeout() 方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計算表達(dá)式,下面有個倒計時的示例,需要的朋友可以學(xué)習(xí)下2013-12-12

