一、概念:单链表是一种顺序存取的结构,为找第 i 个数据元素,必须先找到第 i-1 个数据元素。
二、优点:链表的一种,机制灵活,用途广泛。它可以取代数组,用于其它数据结构的基础,例如栈,队列。除非需要频繁的用下标来访问各个数据,否则在很多数组的地方都可以用链表来代替。
三、参考 <<JAVA数据结构与算法>>
四、实例:关系图见附件。
Link.java
package com.dataconstruct.link; public class Link { public int iData; public double dData; public Link next; public Link(int id,double dd) { this.iData = id; this.dData = dd; } public void displayLink() { System.out.println("{" + this.iData + "," + this.dData + "}"); } }
LinkList.java
package com.dataconstruct.link; public class LinkList { private Link first; public LinkList() { this.first = null; } public boolean isEmpty() { return (this.first == null); } public void insertFirst(int id, double dd) { Link newLink = new Link(id, dd); newLink.next = first;//newLink --> old first this.first = newLink;//first --> newLink } public Link deleteFirst() { Link temp = this.first; this.first = this.first.next; return temp; } public void displayList() { System.out.println("List (first --> last)"); Link current = first; while(current != null) { current.displayLink(); current = current.next; } System.out.println(" "); }
public double find(int key) { Link1 current = this.first; double result = 0; while (current != null) { if (current.iData == key) { return current.dData; } else { current = current.next; } } return result; } public Link delete(int key) { Link previous = this.first; Link current = this.first; while (current.iData != key) { if (current.next == null) {//last return null; } else { previous = current; current = current.next; } } if (current == first) { this.first = this.first.next; } else { previous.next = current.next; } return current; }
}
LinkListApp.java
package com.dataconstruct.link; public class LinkListApp { public static void main(String[] args) { LinkList linkList = new LinkList(); int begin = 10; int end = 70; int k = end/begin; double append = 0.99; for(int i = 0; i < k; i++) { linkList.insertFirst(begin, begin + append); begin += 10; } linkList.displayList(); while(!linkList.isEmpty()) { Link alink = linkList.deleteFirst(); alink.displayLink(); } linkList.displayList(); } }
..........未完待续.....