java資料結構之手動實現單鏈表

2021-08-28 09:38:19 字數 2226 閱讀 2638

package com.example.demo;

// 1、定義要儲存物件的類:

class phone

public phone(string brand, double price)

public void setbrand(string brand)

public void setprice(double price)

public string getbrand()

public double getprice()

@override

public boolean equals(object obj) else if (!brand.equals(other.brand))

return false;

if (double.doubletolongbits(price) != double.doubletolongbits(other.price))

return false;

return true;

}//重寫hashcode

@override

public int hashcode()

@override

public string tostring()

}//2、定義鍊錶的操作標準:

inte***ce ilink

//3、鍊錶的具體實現:

class linkimpl implements ilink

public void addnode(node newnode) else

}public object getnode(int index) else

}public boolean containsnode(object obj) else

return false;}}

public void setnode(int index, object obj) else

}public void removenode(node previous, object obj) else

}public void toarraynode() }}

@override

public void add(object obj)

node newnode = new node(obj);// 封裝資料為一節點

if (this.root == null) else

this.count++;// 資料增加成功,count 自增

}@override

public int size()

@override

public object get(int index)

if (index >= this.count)

this.foot = 0;// 保證資料是從第乙個開始遍歷

return this.root.getnode(index);

}@override

public boolean isempty()

@override

public boolean contains(object obj)

return this.root.containsnode(obj);

}@override

public void set(int index, object obj)

if (index >= this.count)

this.foot = 0;

this.root.setnode(index, obj);

}@override

public void remove(object obj)

if (this.root.data.equals(obj)) else

this.count--;// 資料刪除成功,count 自減

}@override

public void clean()

@override

public object toarray()

this.foot = 0;

this.retarray = new object[this.count];

this.root.toarraynode();

return this.retarray;

}}//4、編寫測試資料:

public class testdemo

}}

Java資料結構之手寫LinkedList

linkedlist的實現本質為乙個雙向鍊錶,下面是簡單的實現增刪改查 public class linkedlist public void add e e public void change int index,e e nodetarget node index target.item e 刪...

java資料結構 單鏈表實現

節點類 package linkedlist public class node 單鏈錶類 package linkedlist 鍊錶 author administrator param public class linkedlist else curr.next node length retu...

資料結構 單鏈表的java實現

單鏈表實現鍊錶的列印及元素刪除操作,鍊錶的實現主要是next屬性的定義,將一堆節點關聯起來的。實現簡單的鍊錶如下 public class linknode public linknode getnext public void setnext linknode next public int ge...