1. Implementation of bidirectional linked list
Four pointers can be used to insert from the header, the tail of the linked list and the specified position. The above methods can also be realized by deleting. Display data can also be displayed from front to back, from back to front. More flexible.
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. iterator
An iterator is a reference encapsulated in a class object that points to a chain node in an associated list.
The iterator method allows the consumer to move the iterator along the list and access the currently indicated chain node.
Iterator can be used to traverse the list and perform certain operations on the selected link points.
The following procedure is an iterative class
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; } }