Java單鏈表的實現

2021-06-05 01:04:37 字數 1970 閱讀 9683

/**

* */

package com.handy.ds;

/** * @author handy 2012-3-15

*/class node

public node(int data, node next)

}public class singlelinkedlist

/*** @param head

* the head to set

*/public void sethead(node head)

public singlelinkedlist()

public boolean isempty()

public boolean addtolast(int elem) else

node newnode = new node(elem, null);

prev.next = newnode;

return true;

} }public boolean addtofirst(int elem) else

} public void printlist()

for (node p = head.next; p != null; p = p.next)

system.out.print(p.data + ",");

system.out.println();

} public boolean insert(int posvalue, int elem) else

newnode.next = curr;

prev.next = newnode;

return true;

} }public int removefirst() else

} public int removelast() else

prev = curr;

curr = curr.next;

return temp;

}return temp;

} }public boolean remove(int elem)

prev = curr;

curr = curr.next;

}prev.next = curr.next;

return true;

} }// 返還重複節點 的個數

public int getsamenumber(int value)

return count;

} // 去掉重複節點

public boolean removesame()

}curr1 = curr1.next;

} return true;

} // 1.將單鏈表逆置

public boolean reverse1()

head.next.next = null;

head.next = p;

return true;

} // 2.將單鏈表逆置

public boolean reverse2()

// 判斷鍊錶是否存在迴圈

public boolean iscontainsloop()

return false;

} // 使鍊錶變為有迴圈的鍊錶

public void turntolooplink()

p.next = head.next;

} // 去掉鍊錶的迴圈

public void turntonolooplink()

p.next = null;

} // 返還鍊錶大小

public int getsize()

return size;

} // 單鏈表按資料從小到大排序

public void sortlist()

p = p.next;

}} }

}

java單鏈表實現

class node 非空節點 public node object obj description 注 在這裡鍊錶預設都是帶有頭節點 資料域為空 version 1.0 author meify 2013 7 29 下午3 36 19 public class linklist 往單鏈表頭部插入節...

Java實現單鏈表

1.定義兩個介面 定義線性表的介面 和順序表介面一樣 插入,刪除,獲取某個元素等一些基本操作 定義單鏈表節點的介面 獲取節點資料域 設定節點資料域 2.單鏈表節點類 兩個屬性 資料域element 指標域next 3.單鏈錶類的實現 插入操作 將資料元素e插入到object之後 插入前 obj節點 ...

java實現單鏈表

package com.tyxh.link 節點類 public class node 顯示此節點 public void display package com.tyxh.link 單鏈表 public class linklist 插入乙個頭節點 public void addfirstnode...