Java中单链表的实现

注释比较详细,直接上代码!

package XiaoMi;import java.util.HashSet;
import java.util.Set;public class SingleLinkList {//节点class Element {                      //自定义数据结构public Object value = null;      //元素属性public Element nextNode = null;  //元素指针}//节点内容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;}}//链表头节点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());}//添加一个节点public void add(Object node) {if (header == null) {    //若为空,创建链表头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个节点元素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;}//删除一个节点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;  //指向它的下一个节点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;}/*** 检查环状单链表* * @return*/public boolean checkLoop() {if (header == null)return false;int size = this.size();if (size == 0)return false;Set set = new HashSet();for (int i = 0; i < size; i++) {Element el = getElement(i);if (!set.contains(el)) {set.add(el);}if (set.contains(el.nextNode)) {  //如果和set中已有节点重复,则认为有环return true;}}return false;}
}



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部