2 gunitunit gunitunit 于 2013.05.08 19:37 提问

java编程,我这段代码是在ecplise上实现的,但DEBUG都没问题,运行后却什么都没有弹出

Dubug上显示,

<terminated>SLinkedList [Java Application]
<terminated, exit value: 0>C:\Program Files\Java\jdk1.7.0_07\bin\javaw.exe (2013-5-7 下午8:46:10)

系统变量的Path为C:\Program Files\Java\jdk1.7.0_07\bin;
classpath.;%JAVA_HOME%\lib;
以下为代码:

@author Administrator

 *
 */ 

//java ADT

import java.awt.*;               


     class SLinkedList{//插入和删除操作分为头尾和中间,对于头,前一个为空,对于尾,后一个结点为空,需要分别对待.

            public class book{

                protected int number;
                protected String title;
                protected String writer;
                protected double pricing;
                protected String publishinghouse;
                book next;  //这便是一个指针,只不过它是引用.

                public book(int number,double pricing,String title,String writer,String publishinghouse)
                { 
                this.number=number;
                this.pricing=pricing;
                this.title=title;
                this.writer=writer; 
                this.publishinghouse=publishinghouse; 
                this.next=null;       
                }


            }
            protected book head,tail;//链表类的保护数据成员

            public SLinkedList() {     
                head = tail = null;     
            }     //创建空链表

            public boolean isEmpty() {     
                return head == null;     
            }      //判断链表是否为空

            public void addToHead(int number,double pricing,String title,String writer,String publishinghouse)
            {
                head=new book(number,pricing,title,writer,publishinghouse);  //创建头结点
                if(tail==null) tail=head;
            }

            public void addToTail(int number,double pricing,String title,String writer,String publishinghouse)
            {
                if(!isEmpty()) {//若链表非空那么将为指针的next初始化为新元素
                    tail.next=new book(number,pricing,title, writer, publishinghouse);
                    tail=tail.next;  //以书号number作为标识符
                }else { //如果为空则创建新的,并将头尾指向它
                    head=tail=new book(number,pricing,title,writer,publishinghouse);
                }

            }

             public void addFirst(int number,double pricing,String title,String writer,String publishinghouse) {     
                    book newNode=new book(number,pricing,title,writer,publishinghouse);     
                    newNode.next = head;     
                    head = newNode;     
                }    //表头插入节点,高效率,这是插入操作之一

             public void addLast(int number,double pricing,String title,String writer,String publishinghouse) {     
                    book newNode = new book(number,pricing,title,writer,publishinghouse);     
                    book p = head;     
                    while (p.next != null) p = p.next;//一直到next到表尾     
                    p.next = newNode;     
                    newNode.next = null;     
                }     //表尾插入结点,在表尾接一个


             public void removeFirst() {     
                    if (!isEmpty()) head = head.next;     
                    else System.out.println("The list have been emptied!");     
                }     //表头删除结点,只需让表头指向下一个结点

             public void removeLast() { //prev为前一个结点的指针,curr为当前的指针    
                    book prev = null, curr = head;     //删除表尾的结点
                    while(curr.next != null) {     
                        prev = curr;     
                        curr = curr.next;     
                        if(curr.next == null) 
                            prev.next=null;      //使前一个结点指向空,则原本的表尾结点便断开了   
                            }     
                        }//removeLast

             public boolean insert(int appointednumber,int number,double pricing,String title,String writer,String publishinghouse) {//结点插入到链表某一位置
                        book  prev = head, curr = head.next;
                        book newNode;
                        newNode = new book(number, pricing, title, writer, publishinghouse);     
                        if(!isEmpty()) {      
                            while(   (curr!= null) && (appointednumber==curr.number) ) 
                            { //两个判断条件不能换       当前不为空,且指定num一致.
                                prev = curr;     
                                curr = curr.next;     //appointednumber为指定插入的位置
                            }     
                            newNode.next = curr;//新结点的next指针指向当前cur,即要插入的位置的下一个结点    
                            prev.next = newNode;     //cur指向了要插入的结点
                            return true;      
                        }     
                        return false; 
                    }     

             public void remove(int number){//删除指定位置的结点
                 book curr=head;
                 book prev=null;
                 boolean found=false;
                 while((curr!=null)&&(!found))
                 {
                     if(number==curr.number )
                     {
                         if(prev==null)
                         removeFirst();
                         else
                             prev.next=curr.next;
                            found=true;

                     }
                     else{
                         prev=curr;
                         curr=curr.next ;//未找到要删除的位置则继续找,指针后移

                        } 
                 }//while
              }//remove



            public book index(int number)//查询操作
                    {  //用index确定要找的number
                        book p;
                        for(p=head;p!=null;p=p.next ){
                            if(p.number==number)
                                return p;
                                number++;
                        }
                        System.out.print("Error!"); 
                        return null;//找不到,返回-1
                    }
            public int Length(SLinkedList L){//返回链表长度   
                        int l=0;
                        book p=head;
                        while(p.next!=null)
                        {   p=p.next ; l=l+1;}

                        return l;             
                    }

            public void Insertsort(SLinkedList L)
                    {   //按关键字即number非递减排序
                        //对链表L作直接插入排序(不需要移动元素)
                        book p=L.head.next;
                        book q=L.head;//q为前面的,p为后面的
                        book ptr=null;

                        if(!isEmpty()){ 
                            for(int j=2;j<=Length(L);j++)
                            {
                            if(p.number>q.number)
                                p=p.next;
                            else
                            {   ptr=L.head ;
                                if(q==L.head)
                                {   book ptr0=head;
                                    while(ptr0.next!=q)
                                        {ptr0=ptr0.next ;}
                                    ptr0.next=p.next;
                                    p.next=q;  //找到p结点的后一个结点,将p结点的前一个结点的指针指向p之后的那个结点
                                }//q为头结点,第一趟
                                while(ptr.next!=q)
                                    {ptr=ptr.next ;}//while
                                book ptr1=q;
                                while(ptr1.next !=p)
                                    {ptr1=ptr1.next; }
                                    ptr.next =p;
                                    ptr1.next=p.next;  
                                    p.next=q; //换了指针,结点的next域没变,故结点位置也没变
                                    book m=q;
                                    q=p;
                                    p=m;
                                    p=p.next;q=q.next;  
                            }//else

                            }//for 
                        }//if

                        else
                            System.out.print("排序失败,记录为空");//-1表示为空,错误,不能排序
                    }//insert   
            public book SelectMinKey(SLinkedList L,int i){//为了简单选择排序,返回最小的number
                     book pt=L.head;
                     book p1;
                    while(i>1){ ; pt=pt.next; i--; }
                    p1=pt.next;
                    while(p1.next!=null){
                    if(pt.number>p1.number)
                        {book s=p1;p1=pt;pt=s; }
                        p1=p1.next;
                    }//比pt所指number小的则交换,p1与ptr一直比较下去直到链表全部都已经比较过了
                    return pt;
            }//SelectMinKey


            public void Selectionsort(SLinkedList L) 
                    {//简单选择排序,堆排序是一维数组存储才比较方便
                        if(L.head==null)
                            System.out.print("Error,linkedlist is empty!");
                        if(L.head.next==null)
                            System.out.print("Okay!There is only one book");
                        //以上需要修饰,给出友好互动界面
                        for(int i=1;i<=Length(L);i++){
                                //仍以number作为关键字进行排序
                                book j=null;book p=L.head ;
                                while(i>1){  ; p=p.next; i--; }
                                j=SelectMinKey(L,i);
                                 if(p!=j) //链表结点的交换,只需改变指针,改变next域
                                 {  book j1=head;
                                    book j2=head;
                                     while(j1.next!=p){j1=j1.next ;}//利用两个指针j1j2储存指向p和j的next域
                                     while(j2.next!=j){j2=j2.next ;}
                                     j1.next=p.next ;j2.next=j.next ;p.next=j2.next;j.next=j1.next ;  }
                                }
                    }

            public static void main(String[] args)
            {   
                Frame frm=new Frame("SlinkedList!");
                frm.setSize(300,200);
                frm.setLocation(500, 400);
                Panel pan=new Panel();
                pan.setSize(150,100);
                pan.setLocation(50,50);
                pan.setBackground(Color.gray );
                Button bun=new Button("yes"); 
                bun.setSize(80, 20);
                bun.setLocation(50, 50);
                bun.setBackground(Color.yellow );
                frm.setLayout(null);
                pan.setLayout(null);
                pan.add(bun);
                frm.add(pan);
                SLinkedList L=new SLinkedList();
                L.addToHead(00,80.2,"AB", "Steven", "xx publishinghouse");
                L.index(00);
            }
    }//class SLinkedList

1个回答

lucumu
lucumu   2013.05.08 22:40
已采纳

main方法最后添加

frm.pack();
frm.show();
gunitunit
gunitunit 可以了! 谢谢大神
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片