http://staff.science.uva.nl/~heck/JAVAcourse/ch4/sss1_2_3.html#listdemo
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlist;
import java.util.Iterator;
/**
*
* @author Ko Hyang Gu
*/
public class LinkedList {
LinkItem head;
LinkedList(){
this.head = new LinkItem(null);
this.head.previous = head;
this.head.next = head;
}
LinkCursor head(){
return new LinkCursor(this,head);
}
void addHead(Object data, LinkItem headpos){
if(headpos.next == headpos){
LinkItem item = new LinkItem(headpos,data,headpos.next);
headpos.next = item;
headpos.previous = item;
}
else{
LinkItem item = new LinkItem(headpos,data,headpos.next);
headpos.next.previous = item;
headpos.next = item;
}
}
void remove(Object data){
LinkItem item = find(data);
if(item != null){
System.out.println("found for remove "+(Integer)item.data);
item.previous.next = item.next;
item.next.previous = item.previous;
//System.out.println((Integer)item.previous.next.data);
//System.out.println((Integer)item.next.previous.data);
//printList(head);
}
}
LinkItem find(Object data){
LinkItem current = head.next;
while(current != null && !current.data.equals(data)){
current = current.next;
}
if(current != null)
return current;
else
return null;
}
void printList(LinkItem head){
LinkItem current = head.next;
while(current != null && current != head){
System.out.println((Integer)current.data);
current = current.next;
}
}
LinkIterator getIterator(){
return new LinkIterator(this);
}
class LinkItem{
LinkItem previous;
LinkItem next;
Object data;
LinkItem(Object data){
this.previous= null;
this.data = data;
this.next = null;
}
LinkItem(LinkItem previous, Object data, LinkItem next){
this.previous = previous;
this.data = data;
this.next = next;
}
}
class LinkCursor{
LinkItem head;
LinkItem pos;
LinkCursor(LinkedList start,LinkItem pos){
this.head = start.head;
this.pos = pos;
}
void next(){
pos= pos.next;
}
void previous(){
pos = pos.previous;
}
}
class LinkIterator implements Iterator{
LinkItem current;
LinkedList list;
LinkIterator(LinkedList list){
this.list = list;
current = list.head.next;
}
public boolean hasNext() {
return current != list.head;
}
public Object next() {
if(current != list.head){
LinkItem ret = current;
current = current.next;
return (Object)ret.data;
}
return null;
}
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LinkedList list = new LinkedList();
list.addHead(new Integer(1), list.head);
list.addHead(new Integer(2), list.head);
list.addHead(new Integer(3), list.head);
list.addHead(new Integer(4), list.head);
list.printList(list.head);
LinkItem temp = list.find(new Integer(2));
if(temp != null)
System.out.println("found "+(Integer)temp.data);
else
System.out.println("not found");
list.remove(new Integer(1));
list.remove(new Integer(2));
//list.printList(list.head);
Iterator itr =list.getIterator();
while(itr.hasNext()){
System.out.println("Iterator "+(Integer)itr.next());
}
}
}