本文目錄一覽:
java單鏈表根據內容刪除節點
代碼:
// 刪除下標為index的節點
public void remove(int index) {
if (index = modCount) {
// 拋異常
System.out.println(“indexOutOfBoundsException!”);// 最好自定義異常拋出,這裡演示
return;
}
Node node = head;
// 如果節點為第一個節點
if (index == 0) {
head = head.next; // 將頭節點指向第二個節點
modCount–;
return;
}
// 如果節點為最後一個節點,找到最後一個節點的前一個節點,不用管
if (index == modCount – 1) {
System.out.println(“ddd”);
// 找到最後一個節點的前一個節點
int j = 0;
while (node != null j index – 1) {
// 最後一個節點的前一個節點
node = node.next;
j++;
}
last = node; // 最後一個節點設置為前一個節點
modCount–;
return;
}
// 如果節點為中間節點
int j = 0;
while (node != null j index – 1) {
// 查找被刪除的節點的前一個節點
node = node.next;
j++;
}
node.next = node.next.next; // 被刪除節點的下一個節點設置為被刪除節點的下下個節點
modCount–;
}
java 中單鏈表的equals方法
LinkedList對equals的定義大致是這樣的:(下文將equals 寫作 相等)
兩個鏈表相等當且僅當其大小相等,並且每個對應元素也相等。
談到相等,必須談及hashCode方法,關係如下:
hashCode相等的 可能相等(相等與否需進一步檢測)
hashCode不等的 一定不等(排除相等情況)
一般情況下,hashCode的計算一定比equals快,而且『不等』的情況較多,因此作為判斷相等的加速方案,java會先檢測hashCode,若不等,則對象一定不等。
那麼,對於java的常見類,例如:基本類型包裝類、集合類、字符串類等 其 equals 與 hashCode已經寫好,不用我們操心。
若是自定義類,一定要重新equals與hashCode方法,滿足上述hashCode的2個關係。
例如自己的User類有name和pass,一個簡單的方案如下:
public class User{
String name,pass;
//get/set….
public int hashCode() {
return name.hashCode + pass.hashCode();
}
public boolean equals(Object o) {
if(o instanceof User) {
User u = (User)o;
return (name.equals(u.name))(pass.equals(u.pass));
}
return false;
}
}
java單鏈表遍歷,最後會輸出一個0,這個零是什麼,頭指針的引用嗎
單鏈錶帶頭結點的遍歷,如果把temp!=null改成temp.next!=null遍歷就正常了,但是去掉.next就會多出一個0。這個0是一個未經初始化的內存中「殘存」的數字,這一次是零,可能在,下一次運行的時候,裏面出現的數字就可能不是0,而是其他不規則的數字。
用java單鏈表實現一元多項式相加的算法?
public class Test {
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println(“一元多項式的相加過程:”);
list1.listAll();
System.out.println(” + “);
list2.listAll();
System.out.println(” = “);
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 一元多項式的一般項類
*/
class Item{
private double coef; //一元多項式的一般項的係數
private int exp; //一元多項式的一般項的指數
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 鏈表結點類
*/
class Node{
private Item data;
private Node next; //鏈表結點的指針域,指向直接後繼結點
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 鏈表類
*/
class LinkList{
private Node head = null; //頭結點指針
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i–,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//刪除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i–,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size–;
return true;
}
java是一種可以撰寫跨平台應用軟件的面向對象的程序設計語言。Java 技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、遊戲控制台、科學超級計算機、流動電話和互聯網,同時擁有全球最大的開發者專業社群。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/160584.html