1)简单链表Java代码1.packageChapterFive;2.classLinkE{3.publicEdata;4.publicLinkEnext;5.publicLink(Edata){6.this.data=data;7.}8.}9.10.classLinkListE{11.publicLinkEfirst;12.//链表中数据项的个数13.publicintsize;14.publicLinkList(){15.first=null;16.size=0;17.}18.//在表头插入新的数据19.publicvoidinsertFirst(Evalue){20.LinkElink=newLinkE(value);21.link.next=first;22.first=link;23.size++;24.}25.//判断链表是否为空26.publicbooleanisEmpty(){27.returnsize==0;28.}29.//删除表头30.publicLinkEdeleteFirst(){31.LinkEtemp=first;32.first=first.next;33.size--;34.returntemp;35.}36.//输出链表中的所有数据37.publicvoiddisplay(){38.LinkEcurr=first;39.while(curr!=null){40.System.out.print(curr.data+);41.curr=curr.next;42.}43.System.out.println();44.}45.//返回链表中数据项的个数46.publicintsize(){47.returnsize;48.}49.//获取从头至尾的第i个数据项50.publicLinkEget(inti){51.if(isize()-1||i0)52.try{53.thrownewIndexOutOfBoundsException();54.}catch(Exceptione){55.e.printStackTrace();56.}57.LinkEcurr=first;58.for(intn=0;nsize();n++){59.if(n==i)60.returncurr;61.else62.curr=curr.next;63.}64.returnnull;65.}66.//输出从头至尾的第i个数据项67.publicvoidremove(inti){68.if(i==0)69.deleteFirst();70.elseif(i==size()-1)71.get(i-1).next=null;72.else{73.get(i-1).next=get(i+1);74.}75.size--;76.}77.}78.79.publicclassLink_list{80.publicstaticvoidmain(String[]args){81.LinkListLongll=newLinkListLong();82.for(inti=0;i10;i++){83.Longvalue=(long)(Math.random()*100);84.ll.insertFirst(value);85.}86.ll.display();87.while(!ll.isEmpty()){88.ll.deleteFirst();89.ll.display();90.}91.System.out.println(Ok);92.}93.}(2)链栈Java代码1.packageChapterFive;2.3.classLinkStackE{4.5.LinkListElinkList;6.7.intsize;8.9.publicLinkStack(){10.size=0;11.linkList=newLinkListE();12.}13.//入栈14.publicvoidpush(Evalue){15.linkList.insertFirst(value);16.size++;17.}18.//出栈19.publicLinkEpop(){20.size--;21.returnlinkList.deleteFirst();22.}23.//返回栈顶元素24.publicLinkEtop(){25.returnlinkList.first;26.}27.//判断栈是否为空28.publicbooleanisEmpty(){29.returnsize==0;30.}31.//显示栈中的全部数据32.publicvoiddisplay(){33.linkList.display();34.}35.}36.37.publicclassLink_stack{38.publicstaticvoidmain(String[]args){39.LinkStackLongls=newLinkStackLong();40.for(inti=0;i10;i++){41.Longvalue=newLong((long)(Math.random()*100));42.ls.push(value);43.}44.while(!ls.isEmpty()){45.ls.pop();46.ls.display();47.}48.System.out.println(Ok);49.}50.}(3)有序表Java代码1.packageChapterFive;2.3.classSortedLink{4.5.publicLinkLongfirst;6.7.intsize;8.9.publicSortedLink(){10.first=null;11.size=0;12.}13.//向有序链表中插入数据14.publicvoidinsert(longvalue){15.LinkLongnewLink=newLinkLong(value);16.LinkLongprevious=null;17.LinkLongcurr=first;18.while(curr!=null&&(valuecurr.data)){19.previous=curr;20.curr=curr.next;21.}22.if(previous==null)//链表为空(在表头插入)23.first=newLink;24.else25.previous.next=newLink;//插入新的节点26.newLink.next=curr;27.size++;28.}29.//删除第一个节点30.publicLinkLongremove(){31.LinkLongtemp=first;32.first=first.next;33.size--;34.returntemp;35.}36.//判断链表是否为空37.publicbooleanisEmpty(){38.returnsize==0;39.}40.//输出链表的所有数据41.publicvoiddisplay(){42.LinkLongcurr=first;43.while(curr!=null){44.System.out.print(curr.data+);45.curr=curr.next;46.}47.System.out.println();48.}49.}50.51.publicclassSortedLinkApp{52.publicstaticvoidmain(String[]args){53.SortedLinksl=newSortedLink();54.for(inti=0;i10;i++){55.sl.insert((long)(Math.random()*100));56.}57.while(!sl.isEmpty()){58.sl.remove();59.sl.display();60.}61.}62.}(4)双向链表Java代码1.packageChapterFive;2.3.classDoubleLinkE{4.5.publicLinkEfirst;6.7.publicLinkElast;8.9.intsize;10.11.@SuppressWarnings(hiding)12.classLinkE{13.publicEdata;14.15.publicLinkEnext;//链表的下一项16.17.publicLinkEprevious;//链表的前一项18.19.publicLink(Evalue){20.this.data=value;21.}22.}23.24.publicDoubleLink(){25.first=null;26.last=null;27.size=0;28.}29.30.//在链表的首部插入一项31.publicvoidinsertFirst(Evalue){32.LinkEnewLink=newLinkE(value);33.if(isEmpty())//如果链表为空则first==last34.last=newLink;35.else36.first.previous=newLink;//确定原first与newLink的前后关系37.newLink.next=first;38.first=newLink;//设置新的first值39.size++;40.}41.42.//在链表的尾部插入一项43.publicvoidinsertLast(Evalue){44.LinkEnewLink=newLinkE(value);45.if(isEmpty())//如果链表为空则last==first46.first=newLink;47.else{48.last.next=newLink;//确定原last与newLink的前后关系49.newLink.previous=last;50.}51.last=newLink;//设置新的last值52.size++;53.}54.55.//删除双向链表的表头56.publicLinkEdeleteFirst(){57.LinkEtemp=first;58.if(first.next==null)//链表中只有一项数据59.last=null;60.else61.first.next.previous=null;//销毁原链表的头部62.first=first.next;63.size--;64.returntemp;65.}66.67.//删除链表的最后一项68.publicLinkEdeleteLast(){69.LinkEtemp=last;70.if(first.next==null)//链表中只有一项数据71.first=null;72.else73.last.previous.next=null;//销毁原链表的尾部74.last=last.previous;75.size--;76.returntemp;77.}78.79.//判断链表是否为空80.publicbooleanisEmpty(){81.returnsize==0;82.}83.84.//输出链表中的所有数据项85.publicvoiddisplay(){86.LinkEcurr=first;87.while(curr!=null){88.System.out.print(curr.data+);89.curr=curr.next;90.}91.System.out.println();92.}93.}94.95.publicclassDoubleLinkApp{96.publicstaticvoidmain(String[]args){97.DoubleLinkIntegerdl=new