1、设计一个顺序表,内有建表、打印
import java.util.Scanner;
class Seqlist{
int []a=new int[20];//创建数组空间int len;void create(){
Scanner Sc=new Scanner(System.in);//输入int x,i=0;System.out.print("请输入一组数,以0结束:");x=Sc.nextInt();while(x!=0){
a[i]=x;i++;x=Sc.nextInt();}len=i;}void show(){
for(int i=0;i<len;i++)System.out.print(a[i]+" ");}
}
class App{
public static void main( String[] args){
Seqlist s=new Seqlist();//造出对象,相当于由图纸造出汽车,才能驾驶汽车,即可以调用其函数方法s.create();s.show();}
}
1.1 设计一个顺序表,内有建表、打印、a.merge(b):将升序表a、b合并成降序表;
import java.util.Scanner;
class Seqlist{
int[] a=new int [20];//分配相应的数组空间 //顺序表=数组+表长int len;void create(){
Scanner Sc=new Scanner(System.in);//输入int x,i=0;System.out.print("请输入一组数,以0结束:");x=Sc.nextInt();while(x!=0){
a[i]=x;i++;x=Sc.nextInt();}len=i;}void merge(Seqlist L1){
//所谓“面向对象”我认为这里即是我(this)面向对象(L1)进行merge操作int[] x=new int[100];//新创建一个数组对象xint i,j,k;i=0;j=0;k=0;while(i<L1.len&&j<this.len)if(L1.a[i]<this.a[j])//此处this可省掉{
x[k]=L1.a[i];i++;k++;}else{
x[k]=this.a[j];j++;k++;}while(i<L1.len){
x[k]=L1.a[i];i++;k++;}while(j<this.len){
x[k]=this.a[j];j++;k++;}this.len=L1.len+this.len;this.a=x;//或者a=x;}void show(){
for(int i=len-1;i>=0;i--)System.out.print(a[i]+" ");}
}
class App{
public static void main(String[] args){
Seqlist s1=new Seqlist();Seqlist s2=new Seqlist();s1.create(); //s1.show();System.out.print("\n");s2.create(); //s2.show();System.out.print("\n");s2.merge(s1);s2.show(); }
}
2、设计一个单链表,内有建表、打印
import java.util.Scanner;
class LinkedList{
//带头结点的单链表int data;LinkedList next,p;LinkedList (int x){
data=x;}//参数传递,将值x传给数据域void create(){
LinkedList tail,q;int x;tail=this;//this是头结点System.out.print("请输入一组数,以0结束:");Scanner sc=new Scanner(System.in);//输入一个x对象x= sc.nextInt();while(x!=0){
//尾插法建表q= new LinkedList(x);//x是货物 new出车厢 赋值连接上列车tail.next=q;tail=q;x=sc.nextInt();} }void show(){
for(p=this.next;p!=null;p=p.next)System.out.print(p.data+" ");}
}
class App{
public static void main(String[] args){
LinkedList c=new LinkedList(0);c.create();c.show();}
}
2.2、设计一个单链表,内有建表、打印、a.merge(b):将升序表a、b合并成降序表;
import java.util.Scanner;
class LinkedList{
int data;LinkedList p,q,tail;LinkedList next;LinkedList(int x){
data=x;}void create(){
//LinkedList tail,p;int x;tail=this;Scanner sc=new Scanner(System.in); System.out.print("请输入一组数,以0结束:\n"); x=sc.nextInt();//输入 while(x!=0){
p=new LinkedList(x);tail.next=p;tail=p;x=sc.nextInt(); } }
void merge(LinkedList h1){
LinkedList p,q,this,sign;p=h1.next;q=this.next;this=new LinkedList(0); //sign用来标记取下的结点、this用来头插法排序while(p!=null&&q!=null){
if(p.data<q.data){
sign=p;p=p.next;}else{
sign=q;q=q.next;} sign.next=this.next; //头插法将两升序链表合并为降序链表this.next=sign; }while(p!=null){
sign=p;p=p.next;sign.next=this.next;this.next=sign;}while(q!=null){
sign=q;q=q.next;sign.next=this.next;this.next=sign;}this.next=this.next; //由于this无法修改,故将this.next赋值给this.next;
}
void show(){
System.out.print("链表的内容是:");for(q=this.next;q!=null;q=q.next) System.out.print(q.data+" ");System.out.println(); //换行
}
}
class App {
public static void main(String args[]){
LinkedList h1=new LinkedList(0);//new一个带参数的对象LinkedList h2=new LinkedList(0);h1.create();h1.show();h2.create();h2.show();h1.merge(h2);h1.show();}
}
2.3、带头结点单链表的选择排序
//选择排序算法是算法枚举的应用,就是反复从未排列的数列中取出最小的元素,加入到另一个数列中,最后的结果即为已经排好序的数列。
import java.util.Scanner;
class LinkedList{
//带头结点的单链表int data;LinkedList next;LinkedList (int x){
data=x;}//参数传递,将值x传给数据域void create(){
LinkedList tail,q;int x,temp;tail=this;//this是头结点System.out.print("请输入一组数,以0结束:");Scanner sc=new Scanner(System.in);//输入一个x对象x= sc.nextInt();while(x!=0){
//尾插法建表q= new LinkedList(x);//x是货物 new出车厢 赋值连接上列车tail.next=q;tail=q;x=sc.nextInt();} }void SelectSort(){
LinkedList p,q;if(this.next==null || this.next.next == null){
return ;}q=this.next;p=this.next.next;//this是头结点int temp=0;int t;while(q!=null){
temp = q.data;while(p!=null){
if(p.data<temp)//交换data域{
t=temp;temp=p.data;p.data=t;}p=p.next;//p继续指向下一结点,直到p为null}q.data = temp;//q开始移动q=q.next; if(q.next!=null) p = q.next;else break;//this.show();}}void show(){
LinkedList p; for(p=this.next;p!=null;p=p.next)System.out.print(p.data+" ");System.out.printf("\n");}
}
class App{
public static void main(String[] args){
LinkedList c=new LinkedList(0);LinkedList h;c.create();System.out.print("单链表为:");c.show();c.SelectSort();System.out.print("选择排序后的结果为:");c.show();}
}
//3、设计二叉树,内有建树、前/中/后序递归遍历、前/中序非递归遍历、层次遍历
import java.util.Scanner;
class Queue{
final int max=20;BTree[] a=new BTree[max];int f,r;boolean isEmpty(){
return f==r; }//队列为空void enQueue(BTree x ){
a[r]=x; r=(r+1)%max; }//进队BTree outQueue(){
BTree x=a[f]; f=(f+1)%max; return x;}//出队
}
class BTree{
int data; BTree L,R;BTree(){
;}BTree(int x){
data =x;}BTree crea( Scanner sc ){
//建树=造根int x=sc.nextInt();if(x==0) return null; //建空树 BTree t=new BTree(x); //树不空t.L=crea(sc);//造一颗树,交给t.Lt.R=crea(sc);//造一颗树,交给t.Rreturn t;}void pre(){
//面象对象的方法不传参System.out.print(this.data+" ");if(L!=null)this.L.pre();if(R!=null)this.R.pre();}class Stack{
BTree [] a=new BTree[20];int top;boolean isEmpty(){
return top==0; }void push(BTree x){
a[top]=x; top++; }//进栈BTree pop(){
top--; return a[top]; }//出栈}void preN(){
//非递归Stack s=new Stack(); BTree t=this;while(t!=null || s.isEmpty()==false)//t不空或栈不空if(t!=null){
System.out.print(t.data+" "); s.push(t); t=t.L; }//进栈else{
t=s.pop(); t=t.R; }//出栈}void in(){
if(L!=null)this.L.in();System.out.print(this.data+" ");if(R!=null)this.R.in();}void inN(){
//非递归Stack s=new Stack(); BTree t=this;while(t!=null || s.isEmpty()==false)if(t!=null){
s.push(t); t=t.L;}//访问左子树else{
t=s.pop(); System.out.print(t.data+" "); t=t.R; }}void post(){
if(L!=null)this.L.post();//注:this定不空,但this.L可能为nullif(R!=null)this.R.post();System.out.print(this.data+" ");}void level(){
Queue q=new Queue(); BTree t=this;q.enQueue(t);//根入队while(q.isEmpty()==false){
//队列不空t=q.outQueue(); //出队元素System.out.print(t.data+" ");//访问tif(t.L!=null)q.enQueue(t.L);if(t.R!=null)q.enQueue(t.R);}}
}
class App{
public static void main(String[] x){
BTree t=new BTree();Scanner sc=new Scanner(System.in);System.out.print("请输入二叉树的前序遍历,0表示空:");t=t.crea(sc);//建二叉树System.out.print("\n pre:");t.pre();System.out.print("\npreN:");t.preN();System.out.print("\n in:");t.in();System.out.print("\n inN:");t.inN();System.out.print("\npost:");t.post();System.out.print("\nlevel:");t.level();}
}