import cj.util.*;class Link<E> ...{ public E data; public Link<E> next; public Link(E data) ...{ this.data = data; }}class LinkList<E> ...{ public boolean ismade; // must call firstMade() first public Link<E> head; public Link<E> tail; public int num; public LinkList() ...{ head = null; tail = null; ismade = false; num = 0; } public boolean isEmpty() ...{ return num == 0; } public void checkIsmadeTrue() throws Exception ...{ if(ismade) throw new Exception("don't call twice firstMade!"); } public void checkIsmadeFalse() throws Exception ...{ if(!ismade) throw new Exception("please call firstMade at first"); } public void display() ...{ Link<E> cur = head;// while (cur != tail) {// P.prn(cur.data + " ");// cur = cur.next;// } // If linklist isnot init, cannot execute if(cur == null) ...{ P.prn("do you want to die?"); } do ...{ P.pr(cur.data + " "); cur = cur.next; } while (cur != head); P.prnEnter(); } public void firstMade(E value) ...{ // check ismade , just call once Link<E> link = new Link<E>(value);// link.next = head; head = link; tail = link; head.next = tail; tail.next = head; ismade = true; num++; } // insert from head public void insertHead(E value) throws Exception ...{ // you can check ismade checkIsmadeFalse(); Link<E> link = new Link<E>(value); link.next = head; head = link; tail.next = link; num++; } // public void insertTail(E value) throws Exception ...{ // you can check itmade checkIsmadeFalse(); Link<E> link = new Link<E>(value); tail.next = link; tail = link; link.next = head; num++; } // delete head public Link<E> deleteHead() ...{ Link<E> temp = head; head = head.next; tail.next = head; num--; return temp; } // delete tail, difficult!! use a Prev Pointer is better public Link<E> deleteTail() ...{ Link<E> cur = head; Link<E> temp; // get the tail's prev one link while(cur.next.next != head) cur = cur.next; temp = cur.next; cur.next = head; tail = cur; return temp; } // get the Node through index public Link<E> get(int index) ...{ if(index > this.getNum() || index < 0) ...{ try ...{ throw new IndexOutOfBoundsException(); }catch (Exception e) ...{ e.printStackTrace(); } } Link<E> cur = head; // make the index from 0 and find the tail quickly if(index == this.getNum()) return tail; for (int i = 0; i < getNum(); i++) ...{ if (i == index) return cur; else cur = cur.next; } return null; } public Link<E> get(E data) ...{ if(data == null || head == null) try ...{ throw new Exception("data is null"); } catch (Exception e) ...{ e.printStackTrace(); } Link<E> cur = head; do...{ if(cur.data.equals(data)) return cur; else cur = cur.next; }while(cur != head); return null; } // remove a Node public void remove(int index) ...{ if(index > this.getNum() || index < 0) ...{ try ...{ throw new IndexOutOfBoundsException(); }catch (Exception e) ...{ e.printStackTrace(); } } else if(index == 0) ...{ this.deleteHead(); } else if(index == this.getNum()) ...{ this.deleteTail(); } else ...{ this.get(index - 1).next = this.get(index + 1); } } public LinkList<E> reverse() throws Exception ...{ LinkList<E> temp = new LinkList<E>(); Link<E> cur = head; temp.firstMade(cur.data); cur = cur.next; for(int i=1; i<this.getNum() && cur != null; i++, cur = cur.next) ...{ temp.insertHead(cur.data); } return temp; } public LinkList<E> clone() ...{ LinkList<E> temp = new LinkList<E>(); Link<E> cur = head; temp.firstMade(cur.data); cur = cur.next; for(int i=1; i<this.getNum() && cur != null; i++, cur = cur.next) ...{ try ...{ temp.insertTail(cur.data); } catch (Exception e) ...{ e.printStackTrace(); } } return temp; } public Link<E> getHead() ...{ return head; } public void setHead(Link<E> head) ...{ this.head = head; } public int getNum() ...{ return num; } public void setNum(int num) ...{ this.num = num; } public boolean isIsmade() ...{ return ismade; } public void setIsmade(boolean ismade) ...{ this.ismade = ismade; } public Link<E> getTail() ...{ return tail; } public void setTail(Link<E> tail) ...{ this.tail = tail; }}public class SingleLink ...{ /** *//** * @param args * @throws Exception */ public static void main(String[] args) throws Exception ...{ // TODO Auto-generated method stub LinkList<Long> sl = new LinkList<Long>(); sl.firstMade((Long.valueOf(1))); for(int i=0; i<10; i++) ...{ Long value = (long)(Math.random()*100); sl.insertHead(value); } sl.display();// sl.deleteHead();// sl.deleteTail(); Link<Long> l = sl.get(9); Link<Long> l1 = sl.get(l.data);// sl.display(); P.prn(l.data); P.prn(l1.data);// sl.remove(9);// sl = sl.reverse(); LinkList<Long> sl1 = sl.clone(); sl1.display(); }} clone函数不能添加异常声明, 只能用try catch包裹