资源描述:
《实验二线性表.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.实验报告二线性表一、实验目的:(1)理解线性表的逻辑结构、两种存储结构和数据操作;(2)应用顺序表的基本算法实现集合A=AUB算法,应用顺序表的基本算法实现两有序顺序表的归并算法;(单链表的归并算法)(3)掌握单链表的遍历、插入和删除等操作算法,实现多项式相加。二、实验内容:1、设有线性表LA=(3,5,8,11)和LB=(2,6,8,9,11,15,20);①若LA和LB分别表示两个集合A和B,求新集合A=AUB(‘并’操作,相同元素不保留);预测输出:LA=(3,5,8,11,2,6,9,15,20)实现代码:packag
2、eQ1_1;publicclassNode{publicTdata;publicNodenext;publicNode(Tdata,Nodenext){this.data=data;this.next=next;}publicNode(){this(null,null);}}packageQ1_1;importQ1_2.Node;publicclassLList{publicstaticNodeheadA;publicstaticNodeheadB;publicstaticN
3、odeCreateLA(){int[]A={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
4、();headB=LB;for(inti=0;i(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;}publicstati
5、cNodelink(NodeLA,NodeLB){LB=LB.next;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=tem
6、p;rear=rear.next;LB=LB.next;}elseLB=LB.next;}returnheadA;}publicstaticvoidmain(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;}}..}粘贴运行结果:②将LA与LB
7、表归并,要求仍有序(相同元素要保留)预测输出:LC=(2,3,5,6,8,8,9,11,11,15,20)packageQ1_2;publicclassNode{publicTdata;publicNodenext;publicNode(Tdata,Nodenext){this.data=data;this.next=next;}publicNode(){this(null,null);}}packageQ1_2;importjava.util.*;publicclassLList{publicstaticNo
8、deheadA;publicstaticNodeheadB;publicstaticNodeCreateLA(){int[]A={3,5,8,11};NodeLA=newNode