package packageA;
interface ILink<A> {
public void add(A data); // 增加節(jié)點
public int getLength(); // 獲取節(jié)點個數(shù)
public boolean isEmpty(); // 判斷鏈表是否為空
public Object[] toArray(); // 鏈表轉(zhuǎn)化為數(shù)組
public A getData(int index); // 根據(jù)序號獲取數(shù)據(jù)
public void setData(int index, A data); // 根據(jù)序號賦值
public boolean contains(A data); // 判斷數(shù)據(jù)是否在鏈表中
public void remove(A data); // 刪除節(jié)點
public void clean(); // 清空鏈表
}
class LinkImpl<A> implements ILink<A> {
class Node { // 將節(jié)點設(shè)為內(nèi)部類
private A data;
private Node next = null;
public Node(A data) {
this.data = data;
}
}
private Node root = null;
private int length = 0;
private Object[] array = null;
public void add(A data) {
Node newNode = new Node(data);
this.length++;
if (this.root == null) {
this.root = newNode;
} else {
Node temp = this.root;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public int getLength() {
return this.length;
}
public boolean isEmpty() {
return this.root == null;
}
public Object[] toArray() {
if (this.isEmpty()) {
return null;
} else {
array = new Object[this.length];
Node temp = this.root;
int counter = 0;
while (temp != null) {
array[counter++] = temp.data;
temp = temp.next;
}
return array;
}
}
public A getData(int index) {
Node temp = this.root;
while (index != 0) {
temp = temp.next;
index--;
}
return temp.data;
}
public void setData(int index, A data) {
Node temp = this.root;
while (index != 0) {
temp = temp.next;
index--;
}
temp.data = data;
}
public boolean contains(A data) {
Node temp = this.root;
while (temp != null) {
if (data.equals(temp.data)) {
return true;
} else
temp = temp.next;
}
return false;
}
public void remove(A data) {
if (this.root == null) {
return;
}
if (data.equals(this.root.data)) {
root = root.next;
}
Node previous = this.root;
Node temp = this.root.next;
while (temp != null) {
if (data.equals(temp.data)) {
previous.next = temp.next;
this.length--;
temp = null;
} else {
previous = temp;
temp = temp.next;
}
}
}
public void clean() {
this.root = null;
this.length = 0;
}
}
public class Main {
public static void main(String[] args) {
ILink<String> link = new LinkImpl<String>();
System.out.println("[當(dāng)前節(jié)點個數(shù)]" + link.getLength() + " 、[鏈表是否為空]" + link.isEmpty());
link.add("你好");
link.add("世界");
link.add("我是戰(zhàn)士k");
System.out.println("——————————轉(zhuǎn)化為數(shù)組,然后輸出——————————");
for (Object data : link.toArray()) {
System.out.println(data);
}
System.out.println("[當(dāng)前節(jié)點個數(shù)]" + link.getLength() + " 、[鏈表是否為空]" + link.isEmpty());
System.out.println();
System.out.println("[2號節(jié)點數(shù)據(jù)]" + link.getData(2));
link.setData(2, "我是fighterk");
System.out.println("[修改后的2號節(jié)點數(shù)據(jù)]" + link.getData(2));
System.out.println("[包含“世界”嗎]" + link.contains("世界"));
System.out.println("[包含“world”嗎]" + link.contains("world"));
link.remove("世界");
System.out.println("——————————刪除了“世界”之后的鏈表——————————");
for (Object data : link.toArray()) {
System.out.println(data);
}
link.clean();
System.out.println("[清空的鏈表isEmpty]"+link.isEmpty());
}
}
單向鏈表(2021-11-12)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。