1.雙向鏈表的實現java
能夠利用四個指針進行從表頭插入,鏈表尾部插入以及從指定位置插入。刪除一樣能夠實現以上方式。顯示數據也能夠從前日後顯示,從後往前顯示。更加靈活。this
class Link { public long ddata; public Link next; public Link previous; public Link(long d) { ddata = d; } public void displayLink() { System.out.println(ddata + " "); } } class DoublyLinkedList { private Link first; private Link last; public DoublyLinkedList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public void insertFirst(long dd) { Link newLink = new Link(dd); if(isEmpty()) last = newLink; else first.previous = newLink; newLink.next = first; first = newLink; } public void insertLast(long dd) { Link newLink = new Link(dd); if(isEmpty()) first = newLink; else { last.next = newLink; newLink.previous = last; } last = newLink; } public Link deleteFirst() { Link temp = first; if(first.next == null) last = null; else first.next.previous = null; first = first.next; return temp; } public Link deleteLast() { Link temp = last; if(first.next == null) first = null; else last.previous.next = null; last = last.previous; return temp; } public boolean insertAfter(long key,long dd) { Link current = first; while(current.ddata != key) { current = current.next; if(current == null) return false; } Link newLink = new Link(dd); if(current==last) { newLink.next = null; last = newLink; } else { newLink.next = current.next; newLink.next.previous = newLink; } newLink.previous = current; current.next = newLink; return true; } public Link deleteKey(long key) { Link current = first; while(current.ddata != key) { current = current.next; if(current == null) return null; } if(current==first) first = current.next; else current.previous.next = current.next; if(current==last) last = current.previous; else current.next.previous = current.previous; return current; } public void displayForward() { System.out.print("list"); Link current = first; while(current!=null) { current.displayLink(); current = current.next; } System.out.println(" "); } public void displayBackward() { System.out.print("last-first"); Link current = last; while(current!=null) { current.displayLink(); current = current.previous; } System.out.println(" "); } } class DoublyLinkedApp { public static void main(String[] args) { DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(22); theList.insertFirst(55); theList.insertFirst(55); theList.insertLast(11); theList.insertLast(44); theList.insertLast(66); theList.displayForward(); theList.displayBackward(); theList.deleteFirst(); theList.deleteLast(); theList.deleteKey(11); theList.displayForward(); theList.insertAfter(22,66); theList.insertAfter(55, 44); theList.displayForward(); } }
list55 55 22 11 44 66 last-first66 44 11 22 55 55 list55 22 44 list55 44 22 66 44
2.迭代器指針
迭代器是一個引用,它被封裝在類對象中,這個引用指向相關聯的鏈表中的鏈結點。code
迭代器方法容許使用者沿鏈表移動迭代器,並訪問當前指示的鏈結點。對象
能用迭代器遍歷鏈表,在選定的鏈結點上執行某些操做。get
如下程序爲迭代使用的類io
import java.io.IOException; import java.util.ListIterator; class Link { public Link next; public long ddata; public Link(long dd) { ddata = dd; } public void displayLink() { System.out.print(ddata+" "); } } class LinkList { private Link first; public LinkList() { first = null; } public Link getFirst() { return first; } public void setFirst(Link f) { first = f; } public boolean isEmpty() { return first == null; } public ListIterator getIterator(){ return new ListIterator(this); } public void displayList() { Link current = first; while(current != null) { current.displayLink(); current = current.next; } System.out.println(" "); } } class ListIterator { private Link current; private Link previous; private LinkList ourList; public ListIterator(LinkList list) { ourList = list; reset(); } public void reset() { current = ourList.getFirst(); previous = null; } public boolean atEnd() { return (current.next==null); } public void nextLink() { previous = current; current = current.next; } public Link getCurrent() { return current; } public void insertAfter(long dd) { Link newLink = new Link(dd); if(ourList.isEmpty()) { ourList.setFirst(newLink); current = newLink; } else { newLink.next = current.next; current.next = newLink; nextLink(); } } public void insertBefore(long dd) { Link newLink = new Link(dd); if(previous == null) { newLink.next = ourList.getFirst(); ourList.setFirst(newLink); reset(); } else { newLink.next = previous.next; previous.next = newLink; current = newLink; } } public long deleteCurrent() { long value = current.ddata; if(previous == null) { ourList.setFirst(current.next); reset(); } else { previous.next = current.next; if(atEnd()) reset(); else current = current.next; } return value; } }