資源描述:
《實(shí)驗(yàn)二 線性表》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、實(shí)驗(yàn)報(bào)告二線性表一、實(shí)驗(yàn)?zāi)康模海?)理解線性表的邏輯結(jié)構(gòu)、兩種存儲結(jié)構(gòu)和數(shù)據(jù)操作;(2)應(yīng)用順序表的基本算法實(shí)現(xiàn)集合A=AUB算法,應(yīng)用順序表的基本算法實(shí)現(xiàn)兩有序順序表的歸并算法;(單鏈表的歸并算法)(3)掌握單鏈表的遍歷、插入和刪除等操作算法,實(shí)現(xiàn)多項(xiàng)式相加。二、實(shí)驗(yàn)內(nèi)容:1、設(shè)有線性表LA=(3,5,8,11)和LB=(2,6,8,9,11,15,20);①若LA和LB分別表示兩個集合A和B,求新集合A=AUB(‘并’操作,相同元素不保留);預(yù)測輸出:LA=(3,5,8,11,2,6,9,15,20)實(shí)現(xiàn)代碼:packageQ1_1;publicclas
2、sNode{publicTdata;publicNodenext;publicNode(Tdata,Nodenext){this.data=data;this.next=next;}publicNode(){this(null,null);}}packageQ1_1;importQ1_2.Node;publicclassLList{publicstaticNodeheadA;publicstaticNodeheadB;publicstaticNodeCreateLA(){int[]A={
3、3,5,8,11};NodeLA=newNode();headA=LA;for(inti=0;i(A[i],null);LA=LA.next;}returnheadA;}publicstaticNodeCreateLB(){int[]B={2,6,8,9,11,15,20};NodeLB=newNode();headB=LB;for(inti=0;i4、=newNode(B[i],null);LB=LB.next;}returnheadB;}publicstaticbooleanCompare(NodeList,intt){Nodep=List.next;booleanflag=false;while(p!=null){if(p.data==t)flag=true;p=p.next;}returnflag;}publicstaticNodelink(NodeLA,NodeLB){LB=LB.next;
5、LA=LA.next;booleanflag;Noderear=null;while(LA!=null){rear=LA;LA=LA.next;}while(LB!=null){flag=Compare(headA,LB.data);if(flag==false){Nodetemp=newNode(LB.data,null);rear.next=temp;rear=rear.next;LB=LB.next;}elseLB=LB.next;}returnheadA;}publicstaticvoidmain
6、(String[]args){Nodet=newNode();headA=CreateLA();headB=CreateLB();t=link(headA,headB);while(t.next!=null){System.out.printf("%-3d",t.next.data);t=t.next;}}}粘貼運(yùn)行結(jié)果:②將LA與LB表歸并,要求仍有序(相同元素要保留)預(yù)測輸出:LC=(2,3,5,6,8,8,9,11,11,15,20)packageQ1_2;publicclassNode{publicTdata
7、;publicNodenext;publicNode(Tdata,Nodenext){this.data=data;this.next=next;}publicNode(){this(null,null);}}packageQ1_2;importjava.util.*;publicclassLList{publicstaticNodeheadA;publicstaticNodeheadB;publicstaticNodeCreateLA(){int[]A={3,5,8,11};Node8、eger>LA=newNode()