Java数据结构及算法_第1页
Java数据结构及算法_第2页
Java数据结构及算法_第3页
Java数据结构及算法_第4页
Java数据结构及算法_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、package paixu;/冒泡排序class arraybubprivate longa;private int nelems;public arraybub(int max)a=new longmax;nelems=0;public void insert(long value)anelems=value;nelems+;public void display()for(int j=0;j<nelems;j+)system.out.println(aj+" ");system.out.println("");public void bubbl

2、esort()int out,in;for(out=nelems-1;out>1;out-)for(in=0;in<out;in+)if(ain>ain+1)swap(in,in+1);private void swap(int one,int two)long temp=aone;aone=atwo;atwo=temp;public class bubblesortapppublic static void main(string args)int maxsize=100;arraybub arr;arr=new arraybub(maxsize);arr.insert(7

3、7); arr.display();arr.bubblesort();arr.display();package paixu;/选择排序class arrayselprivate long a;private int nelems;public arraysel(int max)a=new longmax;nelems=0;public void insert(long value)anelems=value;nelems+;public void display()for(int j=0;j<nelems;j+)system.out.println(aj+" ");

4、system.out.println("");public void selectionsort()int out,in,min;for(out=0;out<nelems-1;out+)min=out;for(in=out+1;in<nelems;in+)if(ain<amin)min=in;swap(out,min);private void swap(int one,int two)long temp=aone;aone=atwo;atwo=temp;public class selectsortapp public static void main(

5、string args) int maxsize=100;arraysel arr;arr=new arraysel(maxsize);arr.insert(77); arr.display();arr.selectionsort();arr.display();package paixu;/插入排序class arrayinsprivate long a;private int nelems;public arrayins(int max)a=new longmax;nelems=0;public void insert(long value)anelems=value;nelems+;pu

6、blic void display()for(int j=0;j<nelems;j+)system.out.println(aj+" ");system.out.println("");public void insertionsort()int out,in;for(out=1;out<nelems;out+)long temp=aout;in=out;while(in>0 && ain-1>=temp)ain=ain-1;-in;ain=temp;public class insertsortapp publi

7、c static void main(string args) int maxsize=100;arrayins arr;arr=new arrayins(maxsize);arr.insert(77); arr.display();arr.insertionsort();arr.display();package paixu;/栈class stackxprivate int maxsize;private long stackarray;private int top;public stackx(int s)maxsize=s;stackarray=new longmaxsize;top=

8、-1;public void push(long j)stackarray+top=j;public long pop()return stackarraytop-;public long peek()return stackarraytop;public boolean isempty()return (top=-1);public boolean isfull()return (top=maxsize-1);public class stackapp public static void main(string args) stackx thestack=new stackx(10);th

9、estack.push(20);thestack.push(40);thestack.push(60);thestack.push(80);while(!thestack.isempty()long value=thestack.pop();system.out.println(value);system.out.println(" ");system.out.println("");package paixu;/队列class queueprivate int maxsize;private long quearray;private int fron

10、t;private int rear;private int nitems;public queue(int s)maxsize=s;quearray=new longmaxsize;front=0;rear=-1;nitems=0;public void insert(long j)if(rear=maxsize-1)rear=-1;quearray+rear=j;nitems+;public long remove()long temp=quearrayfront+;if(front=maxsize)front=0;nitems-;return temp;public long peekf

11、ront()return quearrayfront;public boolean isempty()return (nitems=0);public boolean isfull()return (nitems=maxsize);public int size()return nitems;public class queueapp public static void main(string args) queue thequeue=new queue(5); thequeue.insert(10); thequeue.insert(20); thequeue.insert(30); th

12、equeue.insert(40); thequeue.remove(); thequeue.remove(); thequeue.remove(); thequeue.insert(50); thequeue.insert(60); thequeue.insert(70); thequeue.insert(80); while(!thequeue.isempty() long n=thequeue.remove(); system.out.println(n); system.out.println(" "); system.out.println(""

13、;);package paixu;/link类,链接点class linkpublic int idata;public double ddata;public link next;public link(int id,double dd)idata=id;ddata=dd;public void displaylink()system.out.print(""+idata+","+ddata+"");class linklistprivate link first;public linklist()first=null;public

14、 boolean isempty()return (first=null);public void insertfirst(int id,double dd)link newlink=new link(id,dd);newlink.next=first;first=newlink;public link deletefirst()link temp=first;first=first.next;return temp;public void displaylist()system.out.println("list (first->last);");link curr

15、ent=first;while(current!=null)current.displaylink();current=current.next; system.out.println("");public class linklistapp public static void main(string args) linklist thelist=new linklist();thelist.insertfirst(22, 2.99);thelist.insertfirst(44, 4.99);thelist.insertfirst(66, 6.99);thelist.i

16、nsertfirst(88, 8.99);thelist.displaylist();while(!thelist.isempty()link alink=thelist.deletefirst();system.out.println("deleted ");alink.displaylink();system.out.println("");thelist.displaylist();package paixu;/链栈class linkpublic long ddata;public link next;public link(long dd)dd

17、ata=dd;public void displaylink()system.out.println(ddata+" ");class linklistprivate link first;public linklist()first=null;public boolean isempty()return (first=null);public void insertfirst(long dd)link newlink=new link(dd);newlink.next=first;first=newlink;public long deletefirst()link te

18、mp=first;first=first.next;return temp.ddata;public void displaylist()link current=first;while(current!=null)current.displaylink();current=current.next; system.out.println("");class linkstackappprivate linklist thelist;public linkstackapp()thelist=new linklist();public void push(long j)thel

19、ist.insertfirst(j);public long pop()return thelist.deletefirst();public boolean isempty()return (thelist.isempty();public void displaystack()system.out.print("stack(top->bottom);");thelist.displaylist();public class linkstackapppublic static void main(string args) linkstackapp thestack=

20、new linkstackapp();thestack.push(20);thestack.push(40);thestack.displaystack();thestack.push(60);thestack.push(80);thestack.displaystack();thestack.pop();thestack.pop();thestack.displaystack();package paixu;/链队列class linkpublic long ddata;public link next;public link(long d)ddata=d;public void displ

21、aylink()system.out.println(ddata+" ");class firstlastlistprivate link first;private link last;public firstlastlist()first=null;last=null;public boolean isempty()return first=null;public void insertlast(long dd)link newlink=new link(dd);if(isempty() first=newlink;else last.next=newlink;last

22、=newlink;public long deletefirst()long temp=first.ddata;if(first.next=null)last=null;first=first.next;return temp;public void displaylist()link current=first;while(current!=null)current.displaylink();current=current.next;system.out.println("");class linkqueueprivate firstlastlist thelist;p

23、ublic linkqueue()thelist=new firstlastlist();public boolean isempty()return thelist.isempty();public void insert(long j)thelist.insertlast(j);public long remove()return thelist.deletefirst();public void displayqueue()system.out.println("queue (front->rear);");thelist.displaylist();publi

24、c class linkqueueapp public static void main(string args) linkqueue thequeue=new linkqueue();thequeue.insert(20);thequeue.insert(40);thequeue.displayqueue();thequeue.insert(60);thequeue.insert(80);thequeue.displayqueue();thequeue.remove();thequeue.remove();thequeue.displayqueue();package paixu;/链栈cl

25、ass linkpublic long ddata;public link next;public link(long dd)ddata=dd;public void displaylink()system.out.println(ddata+" ");class linklistprivate link first;public linklist()first=null;public boolean isempty()return (first=null);public void insertfirst(long dd)link newlink=new link(dd);newlink.next=first;firs

温馨提示

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

评论

0/150

提交评论