interfacepackage List; public interface IList { boolean isEmpty(); int size(); void add(ZNode newNode); void insert(int index , ZNode newNode) throws Exception; void delete(int index) throws Exception ; void replace(int index,Object obj) throws Exception; ZNode getByIndx(int index) throws Exception; int getIndex(ZNode node) throws Exception; void display() throws Exception; }
implementpackage List; public class ZList implements IList { private ZNode head = null; public ZList(){ } @SuppressWarnings("null") @Override public void add(ZNode newNode) { // TODO Auto-generated method stub ZNode z = head ; if(!isEmpty()){ while(z.getNext()!=null){ z = z.getNext(); } z.setNext(newNode); }else{ head = newNode ; } } @Override public void insert(int index, ZNode newNode) throws Exception { // TODO Auto-generated method stub ZNode frontNode = getByIndx(index-1) ; ZNode afterNode = getByIndx(index); frontNode.setNext(newNode); newNode.setNext(afterNode); } @Override public void delete(int index) throws Exception { // TODO Auto-generated method stub ZNode frontNode = getByIndx(index-1) ; ZNode currentNode = getByIndx(index) ; ZNode afterNode = currentNode.getNext() ; frontNode.setNext(afterNode) ; currentNode = null ; } @Override public void replace(int index,Object obj) throws Exception { // TODO Auto-generated method stub ZNode current = getByIndx(index); current.setData(obj); } /** * index >= 0 */ @Override public ZNode getByIndx(int index) throws Exception { // TODO Auto-generated method stub if(isEmpty()) throw new Exception("The List is null!"); ZNode z = head ; while(index>0 ){ if(z==null) throw new Exception("the index is out of index"); z = z.getNext(); index--; } return z; } @Override public int getIndex(ZNode node) throws Exception { // TODO Auto-generated method stub if(isEmpty()) throw new Exception("The List is null!"); ZNode z = head ; int index = 0 ; do{ if(z.equals(node)) break; index ++ ; z = z.getNext(); }while(z!=null); return index; } @Override public boolean isEmpty() { // TODO Auto-generated method stub return (null == this.head) ? true:false; } @Override public int size() { // TODO Auto-generated method stub ZNode z = head; int sz = 0; if(null != z) { do{ sz++; }while((z = z.getNext())!=null); } return sz; } @Override public void display() throws Exception { // TODO Auto-generated method stub if(isEmpty()) throw new Exception("The List is null!"); ZNode z = head ; int index = 0 ; do{ System.out .println("index:" + index + " " + z.getData().toString()); index++ ; z = z.getNext() ; }while(z!= null) ; } }
nodepackage List; public class ZNode { private Object data ; private ZNode next ; public ZNode(Object data, ZNode next) { super(); this.data = data; this.next = next; } public ZNode() { this.data = null ; this.next = null ; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public ZNode getNext() { return next; } public void setNext(ZNode next) { this.next = next; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((data == null) ? 0 : data.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ZNode other = (ZNode) obj; if (data == null) { if (other.data != null) return false; } else if (!data.equals(other.data)) return false; return true; } @Override public String toString() { // TODO Auto-generated method stub return "data are : " + data.toString() ; } }
posted on 2011-04-26 05:57 jack zhai 阅读(259) 评论(0) 编辑 收藏
Powered by: BlogJava Copyright © jack zhai