实验二-线性表_第1页
实验二-线性表_第2页
实验二-线性表_第3页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验二-线性表实验报告二线性表实验性表的逻辑结构、两种存储结构和作算1(3)掌LA= ( 3,5,8,11)和 LB=I表示两个集合A和B,求新 现代出|:A 丫艮=并'操作2,6,相同元素不保留);预package Q1_1;public class Node<T> public publicT data ;Node<T> next ;publicNode(T data,Node<T> next) this . data=data;this . next=n ext;,null ); public Node() this ( nullpackage

2、 Q1_1; import Q1_2.Node; public class LList Node< In teger> LA =headA =LA;for (int i=0;i<A.new Node< In teger>();len gth ;i+)LA. next =newNode<I nteger>(Ai,null );publicstaticNode< In teger>headA;publicstaticNode< In teger>headB;publicstaticNode< In teger> Crea

3、teLA()intA=3,5,8,11;LA=LA. next ;return headA;public staticNode< In teger> CreateLB()int B=2,6,8,9,11,15,20;headB =LB;for (int i=0;i<B. length ;i+)LB. next =new Node<Integer>(Bi,null );LB=LB. next ;returnheadB ;public static boolea n Compare(Node< In teger> List,int t)Node<In

4、teger> p=List.next ;boolea n flag= false ;while (p!= null )if (p. data =t)flag= true ;p=p. next ;return flag;public staticNode< In teger> lin k(Node< In teger> LA,Node< In teger> LB)LB=LB. next ;LA=LA. next ;boolea n flag;Node< In teger> rear =n ull ;while (LA!= null )rear

5、=LA;LA=LA. next ;while (LB!= null )flag= Compare ( headA ,LB. data );if (flag= false )Node<Integer >temp =new Node<Integer>(LB.data ,null );rear. n ext =temp;rear=rear.next ;LB=LB. next ; elseLB=LB. next ;returnheadA ;public static void main( Str in g args) Node< In teger> t =new N

6、ode< In teger>();headA =CreateLA ();headB =CreateLB ();t= link ( headA , headB);while (t. next != null )System. out .printf("%-3d" ,t. next . data );t=t. next ;- - - ' -' - - - - - 枯贴运仃纟口果:要保留LA与LB表归并,要求仍有序(相同元素 要保输出 LC= (2,3,5,6,8,8,9,11,11,15,20)package Q1_2;public class No

7、de<T> public T data ;public Node<T> next ;public Node(T data,Node<T> next)this . data =data;this . next =next;public Node()this (null , null );package Q1_2;import ipublic class LList publicstaticNode< In teger>headA :publicstaticNode< In teger>headB :publicstaticNode<

8、; In teger> CreateLA()intA=3,5,8,11:Node< In teger> LA = new Node< In teger>(); headA =LA;for (int i=0;i<A. length :i+)LA.next =new Node<Integer>(Ai,null );LA=LA. next :returnheadA :public staticNode< In teger> CreateLB()int B=2,6,8,9,11,15,20;Node< In teger> LB =

9、 new Node< In teger>(); headB =LB;for (int i=0;i<B. length :i+)LB.next =new Node<Integer>(Bi,null ):return headB ;public staticNodev In teger> compare(Node< In teger> LA,Node< In teger> LB)LB=LB. next ;LA=LA. next ;while (LA!= null )if (LB. data <= LA. data &&

10、; LA. data <LB. next . data )Node<Integer >temp =new Node<Integer>(LA.data ,null );temp. next =LB. next ;LB. next =temp;LA=LA. next ; elseLB=LB. next ; returnheadB ;public static voidNode< In teger> t =headA =CreateLAmain( Stri ng args) new Node< In teger>();();();t= compa

11、re ( headA , headB);headB =CreateLBwhile (t. next != null )System. out .printf("%-3d" ,t.t=t. next ;next . data );llisi pavapii canonj vivasa s ? li ii 15 se粘贴运行结果:在 SinglyLinkedList。类中增加下列成员方计ic将指定单链表lic Node<E>search(E element) 若查找以查 移去創裁那将单链表中的acObj 两条单链表是否相等uals(Object obj)/实现代码:

12、package Q2;比较public class Node<T> publicpublicT data ;Node<T> next ;Node(T data,Node<T> next)this . data=data;this . next=n ext;public Node()(nullthis,null );packageQ2;classpublicNode<E>Si nglyLi nkedList<E> head ;public Sin glyL in kedList(E eleme nt)head = new Node<

13、;E>();Node<E> List =head ;/由指定数组中的多个对象构造单链表for(int i=0;i<element.length ;i+)List. next =new Node(elementil.null 丄;List=List.next ;public表,复制单链表Sin glyL in kedList(Si nglyLinkedList list)/以单链表list构造新的单链publichead =new Node<E>();Node<E> list_new = head ;Node<E> p=li ead ;

14、n ext ;if (p. data =null ) p=p. next ;list new=list new.while (p!= null )list_new.next = new Node<E>(p. data , null );list new=list new.next ;public void concat(SinqlyLinkedList list)/ 将指定单链表 list 链接在当前单链表之后Node<E> rear = n ull ;Node<E> p = head ;Node<E> q=list. head . next ;

15、if (p. data =null )p=p. next ;while (p!= null )rear=p;p=p. next ;if (q= null )q=q. next ;rear. n ext =q;public Node<E> search(E element)/若查找到指定,则返回结点,否则返回nullNode<E> p= this . head ;Node<E> temp= n ull ;if (p= null )p=p next ;while (p. next != null )if (p. data =element)temp=p;p=p.

16、 next ;return temp;publicboolea n con tain (E eleme nt)/以查找结果判断单链表是否包含指定对象boolea n flag= false ;Node<E> p= this . head ;Node<E> temp= n ull ;if (p= null )p=a next ;while (p!= null )if (p. data =element)flag= true ;retur nflag;publicboolea n remove (E eleme nt)Node<E> p= this . head

17、 ;/移去首次出现的指定对象Node<E> temp=Node<E> front=nullhead ;false ;boolea n flag=if (p= null )p=Q next ;while (p!= null && temp= null )if (p. data =element)temp=p; flag= true ; break ;p=p. next ;front=front. next ;front=p.next ;return flag;publicboolea n replace (Object obj, E eleme nt)对象

18、 element/将单链表中的obj对象替换为boolea n flag= if (obj!=Node<E> p=whilefalseifnull&& eleme nt!= null ) this . head ;null )(p!=(obj.equals(p.data )p.flag =P = P.return flag;publicboolea ndata = eleme nt;true ;n ext ;equals(Object obj) boolean flag= true ; SinglyLinkedList x=(SinglylinkedList) ob

19、j; Node t=x. head . next ;Node s= this . head . next ;/比较两条单链表是否相等while (t!= null &&s!= null )if (t. data !=s. data ) flag= false ; break ;t=t.next ;s=s. next ;return flag; package Q2; class Test staticIn tegerx=3,5,8,11;staticIn tegerx1 =3,5,8,11;staticIn tegerx2 =2,6,8,9,11,15,20;staticSin

20、 glyL in kedListv In teger>List1= new Sin glyL in kedList< In teger>(staticSin glyL in kedList< In teger>List2= new SinglyLinkedList<Integer>(staticSin glyL in kedList< In teger>List3= new SinglyLinkedList<Integer>(publicstaticvoid disList(SinglyLinkedList List)forx)

21、;x1);x2);(Node temp=List. head . next ;temp!= null ;temp=temp.next )System. out .printf("%-5d" ,temp. data );System. out .println();public static void concat()List1.con cat(List3 );public static void Find()System. out .println("请输入需要查找的数:");Scanner scan= new Scann er(System.in);I

22、n teger eleme nt=sca n.n ext In t();Node t= List1.search(element);out .println(t.data );if (t!= null ) System.elseSystem.out .println("List1中找不到指定的数。“);public staticSystem. outvoid Con tai n().println( "请输入所需包含的数:");Scanner scan=new Scanner(System.in );In teger eleme nt=sca n.n ext In

23、 t();if ( List3 .contain(element)System. out .printf("List3 包含指定的数 -5dn" ,element);elseSystem. out .printf( "List3不包含指定的数 %-5dn" ,element);public static void remove()System. out .println("请输入指定移除的数:");Scanner scan= new Scann er(System. in);In teger eleme nt=sca n.n ext

24、In t();if ( List3 .remove(element)System. out .printf( "%-5d 已在 List3 中移除 n" ,element);else System. out .printf( "%-5d 无法在 List3 中找到,无法移除 n" ,element); public static void replace()System. out .println("请输入所需交换的两个数:”);Scanner scan= new Scann er(System. in);In teger obj=sca n.

25、n extI nt();In teger eleme nt=sca n.n ext In t();if ( List3 .replace(obj, element) System. out .printf( else System. out .println( public static void equals()if (List1 .equals( List2 System. out .println( else System. out .println( if (List1 .equals( List3 System. out .println( else System. out .pri

26、ntln( "%-5d 与%-5d 两个数成功交换 n" ,obj,element);"无法改动“);)"List1"List1"List1"List1与List2两个单链表相等“);与List2两个单链表不相等“);与List3两个单链表相等“);与List3两个单链表不相等“);public static void main( Str in g args) disList(List1disList(List2disList(List3con cat ()-disList(List1Find ();Con tai n();

27、remove ()-replace();););););equals ();点包含三链表可项示加薮域和后继结点链。结head式,实现两个多项 系数指数链链式相fe法所示。给-6> -10 0实现代码:package Q3;public class Node<T> publicT base ;publicT index ;public Node<T> next ;public Node(T base,T in dex,Node<T> n ext)this . base =base;this .index =index;this . next =next;

28、public Node()this (null , null , null );package Q3;public class Poly no mial public staticNode<Integer>head ;public staticNode< In teger> Create(int t)Node< In teger> p=new Node< In teger>();head =p;for (int i=0;i<t.length ;i+)p. next = new Node<Integer>(tiO,ti1,null

29、 );p=p. next ;print (head );returnhead ;public static void print(Node<Integer> a)a=a. next ;while (a. next != null )System. out .print(a.base +"A" +a. index +" + " );a=a. next ;System. out .print(a. base +"A" +a. index );System. out .println();public staticNode< In teger> Add(Node< In teger> a,Node< In teger> b)Node< In teger> p=new Node< In teger>();head =p;a=a. nextb=b. nextwhile (a!= null &&b!= null )if (a. index <=b. index )if (a. index =b. index )ifif els

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论