Java中单链表的实现
生活随笔
收集整理的這篇文章主要介紹了
Java中单链表的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
注釋比較詳細(xì),直接上代碼!
package XiaoMi;import java.util.HashSet; import java.util.Set;public class SingleLinkList {//節(jié)點(diǎn)class Element { //自定義數(shù)據(jù)結(jié)構(gòu)public Object value = null; //元素屬性public Element nextNode = null; //元素指針}//節(jié)點(diǎn)內(nèi)容class Value {public String code;public String name;public Value() {}public Value(String code, String name) {this.code = code;this.name = name;}@Overridepublic String toString() {return code + "-" + name;}}//鏈表頭節(jié)點(diǎn)private Element header = null; //引用public static void main(String[] args) {SingleLinkList list = new SingleLinkList();Value value1 = list.new Value("1", "java");Value value2 = list.new Value("2", "c++");Value value3 = list.new Value("3", "c#");Value value4 = list.new Value("4", "vb");list.add(value1);list.add(value2);list.add(value3);list.add(value4);System.out.println("remove vb ?" + list.remove(value4));System.out.println("have c++ ?" + list.contains(value2));System.out.println("have vb ?" + list.contains(value4));System.out.println("list is emptry ? " + list.isEmpty());System.out.println(list);list.clear();System.out.println("list is emptry ? " + list.isEmpty());}//添加一個節(jié)點(diǎn)public void add(Object node) {if (header == null) { //若為空,創(chuàng)建鏈表頭header = new Element();header.value = null;header.nextNode = null;}Element element = new Element(); //把鏈表頭給加上element.value = node;element.nextNode = header.nextNode;header.nextNode = element;}//清空鏈表public void clear() {header = null;}//是否包含某個元素public boolean contains(Object o) {if (header == null)return false;Element eqEl = header.nextNode;while (eqEl != null) {if (eqEl.value == o) {return true;}eqEl = eqEl.nextNode;}return false;}//重寫toString方法,拼接字符串public String toString() {int size = this.size();String print = "";if (size == 0)return print;for (int i = 0; i < size; i++) {print = "," + this.get(i) + print;}print = "[" + print.substring(1) + "]";return print;}//取第i個元素public Object get(int index) {if (header == null)return null;int size = this.size();if (index > (size - 1) || index < 0) {return null;}Element temp = header.nextNode;int i = 0;while (temp != null) {if (index == i) {return temp.value;}i++;temp = temp.nextNode;}return null;}//返回第i個節(jié)點(diǎn)元素private Element getElement(int index) {if (header == null)return null;int size = this.size();if (index > (size - 1) || index < 0) {return null;}Element temp = header.nextNode;int i = 0;while (temp != null) {if (index == i) {return temp;}i++;temp = temp.nextNode;}return null;}//鏈表是否為空public boolean isEmpty() {if (header == null)return true;elsereturn false;}//刪除一個節(jié)點(diǎn)public boolean remove(Object o) {if (header == null)return false;Element eqPreEl = header;Element eqEl = header.nextNode;while (eqEl != null) {if (eqEl.value == o) {eqPreEl.nextNode = eqEl.nextNode; //指向它的下一個節(jié)點(diǎn)return true;}eqPreEl = eqEl; //指針向后移動eqEl = eqEl.nextNode;}return false;}//鏈表的長度public int size() {if (header == null)return 0;Element temp = header.nextNode;int i = 0;while (temp != null) {i++;temp = temp.nextNode;}return i;}/*** 檢查環(huán)狀單鏈表* * @return*/public boolean checkLoop() {if (header == null)return false;int size = this.size();if (size == 0)return false;Set<Element> set = new HashSet<Element>();for (int i = 0; i < size; i++) {Element el = getElement(i);if (!set.contains(el)) {set.add(el);}if (set.contains(el.nextNode)) { //如果和set中已有節(jié)點(diǎn)重復(fù),則認(rèn)為有環(huán)return true;}}return false;} }與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的Java中单链表的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中外部类访问内部类的方法
- 下一篇: 【小米面试题】-给定一个具体时间,计算时