日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java单链表例子_写一个java链表的例子?随便举例说一下。

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java单链表例子_写一个java链表的例子?随便举例说一下。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

展開全部

//單鏈表類

package dataStructure.linearList;

import dataStructure.linearList.Node; //導入單鏈表結點類

import java.util.Iterator; //導入迭代器接口

public class SinglyLinkedList extends AbstractList implements LList //單鏈表類,實現線性表接口

{

protected Node head; //頭指針,指向單鏈表第32313133353236313431303231363533e59b9ee7ad94313332393064661個結點

public SinglyLinkedList() //構造空單鏈表

{

this.head = null;

}

public SinglyLinkedList(Node head) //構造指定頭指針的單鏈表

{

this.head = head;

}

public boolean isEmpty() //判斷單鏈表是否為空,O(1)

{

return this.head==null;

}

public int length() //返回單鏈表長度

{ //單鏈表遍歷算法,O(n)

int i=0;

Node p=this.head;

while (p!=null)

{

i++;

p = p.next;

}

return i;

}

public E get(int index) //返回序號為index的對象,index初值為0

{ //若單鏈表空或序號錯誤返回null,O(n)

if (this.head!=null && index>=0)

{

int j=0;

Node p=this.head;

while (p!=null && j

{

j++;

p = p.next;

}

if (p!=null)

return (E)p.data;

}

return null;

}

public E set(int index, E element) //設置序號為index的對象為element,O(n)

{ //若操作成功返回原對象,否則返回null

if (this.head!=null && index>=0 && element!=null)

{

int j=0;

Node p=this.head;

while (p!=null && j

{

j++;

p = p.next;

}

if (p!=null)

{

E old = (E)p.data;

p.data = element;

return old; //若操作成功返回原對象

}

}

return null; //操作不成功

}

public boolean add(int index, E element) //插入element對象,插入后對象序號為index

{ //若操作成功返回true,O(n)

if (element==null)

return false; //不能添加空對象(null)

if (this.head==null || index<=0) //頭插入

this.head = new Node(element, this.head);

else //單鏈表不空且index>=1

{

int j=0;

Node p=this.head;

while (p.next!=null && j

{

j++;

p = p.next;

}

p.next = new Node(element, p.next);//中間/尾插入

}

return true;

}

public boolean add(E element) //在單鏈表最后添加對象,重載,O(n)

{

return add(Integer.MAX_VALUE, element);

}

public E remove(int index) //移去序號為index的對象,O(n)

{ //若操作成功返回被移去對象,否則返回null

E old = null;

if (this.head!=null && index>=0)

if (index==0) //頭刪除

{

old = (E)this.head.data;

this.head = this.head.next;

}

else //中間/尾刪除

{

int j=0;

Node p=this.head;

while (p.next!=null && j

{

j++;

p = p.next;

}

if (p.next!=null)

{

old = (E)p.next.data; //操作成功,返回原對象

p.next = p.next.next; //刪除p的后繼結點

}

}

return old;

}

public void clear() //清空單鏈表,O(1)

{

this.head = null;

}

public String toString() //返回顯示單鏈表所有元素值對應的字符串

{ //單鏈表遍歷算法,O(n)

String str="(";

Node p = this.head;

while (p!=null)

{

str += p.data.toString();

p = p.next;

if (p!=null)

str += ", ";

}

return str+")";

}

//以上實現LList接口,第2章

//以下2.4 迭代器內容

public Iterator iterator() //返回一個迭代器對象

{

return new Itr();

}

private class Itr implements Iterator //私有內部類,實現迭代器接口

{

private Node cursor = head;

public boolean hasNext() //若有后繼元素,返回true

{

return cursor!=null && cursor.next!=null;

}

public E next() //返回后繼元素

{

if (cursor != null && cursor.next!=null)

{

E element = cursor.next.data;

cursor = cursor.next;

return element;

}

return null;

}

public void remove() //不支持該操作

{

throw new UnsupportedOperationException();

}

}//內部類Itr結束

//以下第8章 8.2.1 順序查找,散列表中用

public Node search(E element, Node start) //從單鏈表結點start開始順序查找指定對象

{ //若查找成功返回結點,否則返回null

if (this.head==null || element==null)

return null;

Node p=start;

while (p!=null && !element.equals(p.data))

{

System.out.print(p.data+"? ");

p = p.next;

}

return p;

}

public Node search(E element) //順序查找指定對象

{

return search(element, head);

}

/*

public boolean contain(E element) //以查找結果判斷單鏈表是否包含指定對象

{ //若包含返回true,否則返回false

return this.search(element)!=null;

}

*/

public boolean remove(E element) //移去首次出現的指定對象,O(n)

{ //若操作成功返回true

if (this.head==null || element==null)

return false;

if (element.equals(this.head.data))

{

this.head = this.head.next; //頭刪除

return true;

}

Node front=this.head, p=front.next; //中間/尾刪除

while (p!=null && !element.equals(p.data))

{

front = p;

p=p.next;

}

if (p!=null)

{

front.next = p.next;

return true;

}

return false;

}

//以下是第2章習題

public SinglyLinkedList(E[] element) //由指定數組中的多個對象構造單鏈表

{

this.head = null;

if (element!=null && element.length>0)

{

this.head = new Node(element[0]);

Node rear=this.head;

int i=1;

while (i

{

rear.next = new Node(element[i++]);

rear = rear.next;

}

}

}

public void concat(SinglyLinkedList list) //將指定單鏈表list鏈接在當前單鏈表之后

{

if (this.head==null)

this.head = list.head;

else

{

Node p=this.head;

while (p.next!=null)

p = p.next;

p.next = list.head;

}

}

public SinglyLinkedList(SinglyLinkedList list) //以單鏈表list構造新的單鏈表

{ //復制單鏈表

this.head = null;

if (list!=null && list.head!=null)

{

this.head = new Node(list.head.data);

Node p = list.head.next;

Node rear = this.head;

while (p!=null)

{

rear.next = new Node(p.data);

rear = rear.next;

p = p.next;

}

}

}

//遞歸方法

// public SinglyLinkedList(SinglyLinkedList list) //以單鏈表list構造新的單鏈表

public void copy(SinglyLinkedList list) //復制單鏈表

{

this.head = copy(list.head);

}

private Node copy(Node p) //復制單鏈表,遞歸方法

{

Node q=null;

if (p!=null)

{

q = new Node(p.data);

q.next = copy(p.next);

}

return q;

}

/*//遞歸方法

public String toString()

{

return "("+ this.toString(this.head) +")";

}

public String toString(Node p) //遞歸方法

{

if (p!=null)

return p.data.toString() + ", " + this.toString(p.next); //遞歸調用

return "";

}

public SinglyLinkedList(E[] element) //由指定數組中的多個對象構造單鏈表

{

this.head = null;

if (element!=null)

this.head = create(element,0);

}

private Node create(E[] element, int i) //由指定數組構造單鏈表

{ //遞歸方法

Node p=null;

if (i

{

p = new Node(element[i]);

p.next = create(element, i+1);

}

return p;

}

*/

public boolean equals(Object obj) //比較兩條單鏈表是否相等

{

if (obj == this)

return true;

if (obj instanceof SinglyLinkedList)

{

SinglyLinkedList list = (SinglyLinkedList)obj;

return equals(this.head, list.head);

}

return false;

}

private boolean equals(Node p, Node q) //比較兩條單鏈表是否相等,遞歸方法

{

if (p==null && q==null)

return true;

if (p!=null && q!=null)

return p.data.equals(q.data) && equals(p.next, q.next);

return false;

}

//以下是第8章習題

public boolean replace(Object obj, E element)//將元素值為obj的結點值替換為element,O(n)

{ //若替換成功返回true,否則返回false

if (obj==null || element==null)

return false;

Node p=this.head;

while (p!=null)

{

if (obj.equals(p.data))

{

p.data = element;

return true;

}

p = p.next;

}

return false;

}

public boolean replaceAll(Object obj, E element) //將所有元素值為obj的結點值替換為element,O(n)

{ //若替換成功返回true,否則返回false

boolean done=false;

if (obj!=null && element!=null)

{

Node p=this.head;

while (p!=null)

{

if (obj.equals(p.data))

{

p.data = element;

done = true;

}

p = p.next;

}

}

return done;

}

public boolean removeAll(Object obj) //將所有元素值為obj的結點刪除

{

if (this.head==null || obj==null)

return false;

boolean done=false;

while (this.head!=null && obj.equals(this.head.data))

{

this.head = this.head.next; //頭刪除

done = true;

}

Node front=this.head, p=front.next;

while(p!=null)

{

if (obj.equals(p.data))

{

front.next = p.next; //刪除p結點

p = front.next;

done = true;

}

else

{

front = p;

p = p.next;

}

}

return done;

}

public static void main(String[] args)

{

String[] letters={"A","B","C","D","E","F"};

SinglyLinkedList list1 = new SinglyLinkedList(letters);

SinglyLinkedList list2 = new SinglyLinkedList(list1);

list2.copy(list1);

System.out.println(list2.toString());

System.out.println("equals(), "+list2.equals(list1));

}

}

/*

程序運行結果如下:

(A, B, C, D, E, F)

*/

/* 第2章 //可行,但效率低,時間復雜度是O(n*n)。

public String toString()

{

String str="{";

if (this.length()!=0)

{

for(int i=0; i

str += this.get(i).toString()+", ";

str += this.get(this.length()-1).toString();

}

return str+"}";

}

*/

已贊過

已踩過<

你對這個回答的評價是?

評論

收起

總結

以上是生活随笔為你收集整理的java单链表例子_写一个java链表的例子?随便举例说一下。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。