java 链表数据结构实现

来源:互联网 发布:windows embedded 7.0 编辑:程序博客网 时间:2024/05/17 08:29

上次发表出现问题,这次更正后重新发表

java 没有指针,无法直接访问内存,这样的设计提高了安全,降低了编程的复杂度。虽然没有指针但是java依然可以实现链表数据结构,因为java有引用,可以引用一块指定的内存空间,可以用引用建立指针模型从而实现链表结构。

有了理论我们就可以开始实践。链表是有节点构成的,一个节点有两个成员,一个是该节点的数据,另一个是引向下一个节点的地址。可以用构造函数来初始化着两个成员。

public IntNode(int initialDate,IntNode initialLink){date = initialDate;link = initialLink;}


然后用一个方法添加节点。


<span style="font-family: Arial, Helvetica, sans-serif;">public void addNodeAfter(int element){ </span>
<span style="font-family: Arial, Helvetica, sans-serif;">link = new IntNode(element, link);</span>
}


现在已经可以构建一个链表,然后添加数据。但是仅有这些是不够的,我们还需要删除节点,查找节点,查看节点数据,查看节点指针,查看节点长度,以及修改节点数据指针等等。。。着就需要写很多的方法。方法都不复杂下面是完整的 IntNode类的代码,包含测试的main函数。参考图片在代码下面。


package one.GD.IntNode;public class IntNode {private int date; private IntNode link;public IntNode(int initialDate,IntNode initialLink){date = initialDate;link = initialLink;}public void addNodeAfter(int element){ link = new IntNode(element, link);}public int getDate(){return date;}public IntNode getLink(){return link;}public void removeNodeAfter(){link = link.link;}public void setDate(int newDate){date = newDate;}public void setLink(IntNode newLink){link = newLink;}// static function //get length functionpublic static int listlength(IntNode head){IntNode cursor;int answer = 0;for(cursor=head;cursor!=null;cursor=cursor.link){answer++;}return answer;}//search functionpublic static IntNode listSearch(IntNode head,int target){IntNode cursor;for(cursor=head;cursor!=null;cursor=cursor.link){if(target==cursor.date){return cursor;}}return null;}public static IntNode listPosition(IntNode head,int position) {IntNode cursor;int i;if(position<0){System.out.println("position < 0");}cursor=head;for(i=1;(i<position)&&(cursor!=null);i++){cursor = cursor.link;}return cursor;}    public static IntNode listCopy(IntNode soure){    IntNode copyHead;    IntNode copyTail;    if(soure==null){return null;}    copyHead = new IntNode(soure.date,null);    copyTail = copyHead;    while(soure.link != null){    soure = soure.link;    copyTail.addNodeAfter(soure.date);    copyTail = copyTail.link;    }    return copyHead;        }    public static IntNode[] listCopywithTail(IntNode source){    IntNode copyHead;    IntNode copyTail;    IntNode[] answer = new IntNode[2];    if(source ==null){return answer;}    copyHead = new IntNode(source.date, null);    copyTail = copyHead;    while(source.link !=null){    source = source.link;    copyTail.addNodeAfter(source.date);    copyTail = copyTail.link;    }    answer[0] = copyHead;    answer[1] = copyTail;    return answer;    }    public static void toString(IntNode source){    System.out.println("*****************toString****************");    if(source == null){System.out.println("source is null");}    while(source!=null){    System.out.println("date: "+source.getDate()+"  link: "+source.getLink());    source = source.link;        }    System.out.println("\n");    }     //test IntNodepublic static void main(String[] args) {IntNode head;head = new IntNode(12, null);head.addNodeAfter(13);head.addNodeAfter(14);head.addNodeAfter(15);toString(head);//System.exit(0); //exit jvm 0 is normal exit else not normal exitIntNode temp = listPosition(head, 2);show(temp);temp.addNodeAfter(28);System.out.println("\n");toString(head);IntNode temp2 = listSearch(head, 14);show(temp2);temp2.addNodeAfter(66);toString(head);temp.removeNodeAfter();temp.removeNodeAfter();temp.removeNodeAfter();toString(head);System.out.println("the length is: "+listlength(head));}public static void show(IntNode source){System.out.println("--------------showtime---------");System.out.println(source.getDate());System.out.println(source.getLink());}public static void myPrintln(Object one,Object two){System.out.print(one);System.out.print(two);System.out.println();}}

运行参考图片:



通过上面构建的 IntNode 类我们已经有了链表的基本操作 方法,当然还可以添加更多有趣的操作方法,这里只提供基本的方法,接下来以IntNode类为基础创建一个IntLinkBag类。

如果把节点都看作是一个个int整型,那么IntNode类就相当于数组将这些整型数据储存起来提供索引访问,IntLinkBag就相当于集合,提供合并数组,排序,或者是在一个数组的基础之上追加一个数组。

通俗讲 IntNode 就是用链表为数据结构实现的数组,IntLinkBag就是在IntNode 基础之上用链表为数据结构实现的集合。比喻不恰当,大家理解就好。

下面是IntLinkBag类的代码,和运行参考图片。


package one.GD.IntLinkedBag;import java.util.Random;import one.GD.IntNode.IntNode;public class IntLinkBag implements Cloneable {private IntNode head;private int manyNodes;public IntLinkBag(){head = null;manyNodes = 0;}public void add(int elment){head = new IntNode(elment,head);manyNodes++;}public void addMany(int[] elments){for(int item:elments){add(item);}}public int size(){return manyNodes;}public void addAll(IntLinkBag addend){IntNode[] copyInfo;if(addend==null){throw new IllegalArgumentException("addend is null");}if(addend.manyNodes>0){copyInfo = IntNode.listCopywithTail(addend.head);copyInfo[1].setLink(head);head = copyInfo[0];manyNodes +=addend.manyNodes;}}public static IntLinkBag union(IntLinkBag b1,IntLinkBag b2){if(b1==null){throw new IllegalArgumentException("b1 is null");}if(b2==null){throw new IllegalArgumentException("b2 is null");}IntLinkBag answer= new IntLinkBag();answer.addAll(b1);answer.addAll(b2);return answer;}public IntLinkBag clone(){IntLinkBag answer=null;try{answer = (IntLinkBag)super.clone();}catch(CloneNotSupportedException e){System.out.println(e.getMessage());}answer.head = IntNode.listCopy(head);return answer;}public boolean remove(int target){IntNode targetIntNode;targetIntNode = IntNode.listSearch(head, target);if(targetIntNode==null){return false;}else{targetIntNode.setDate(head.getDate());head = head.getLink();manyNodes--;return true;}}public int countOccurrencess(int target){int answer;IntNode cursor;answer = 0;cursor = IntNode.listPosition(head, target);while(cursor!=null){answer++;cursor.getLink();cursor = IntNode.listPosition(cursor, target);}return answer;}public int grab(){int i ;Random r =new Random();IntNode cursor;if(manyNodes==0){throw new IllegalStateException("the manyNodes is zero");}else{i = r.nextInt(manyNodes);cursor = IntNode.listPosition(head, i);return cursor.getDate();}}public void toSay(){IntNode.toString(head); }//test IntLinkBagpublic static void main(String[] args){IntLinkBag myLinkBag = new IntLinkBag();int[] array= {12,13,14,15};myLinkBag.addMany(array);myLinkBag.toSay();IntLinkBag two = new IntLinkBag();two.add(21);two.add(23);two.add(25);two.add(29);two.toSay();myLinkBag.addAll(two);myLinkBag.toSay();IntLinkBag clone = myLinkBag.clone();clone.remove(12);clone.toSay();int x = clone.grab();System.out.println("x is: "+x);}}

参考图片如下:



有问题,欢迎留言讨论,吐槽也欢迎。

0 0